I have discovered that *areduce* is also very fast:

(areduce a i res 0 (+ res (aget a i)))

I feel that it preserves the functional programming spirit.

Any comment on that, David?



On Tue, Apr 8, 2014 at 9:44 AM, Yehonathan Sharvit <[email protected]> wrote:

> Thanks David!
>
> I see that ClojureScript can deal with js arrays both with:
> 1. aget and alength
> 2. get and count
>
> The first option is much much faster!!!
>
>
>
>
> On Tue, Apr 8, 2014 at 12:38 AM, David Nolen <[email protected]>wrote:
>
>> On Mon, Apr 7, 2014 at 5:26 PM, Yehonathan Sharvit <[email protected]>wrote:
>>
>>> (defn my-sum[a]
>>>   (def res 0)
>>>   (doseq [x a]
>>>     (set! res (+ res x )))
>>>   res)
>>>
>>
>> def's are always top-level. Do not put def's in def's like this.
>>
>> (defn my-sum [a]
>>   (loop [i 0 sum 0]
>>     (if (< i (alength a))
>>       (recur (inc i) (+ sum (aget a i))
>>       sum)))
>>
>> Should give similar performance to the version you wrote in JavaScript.
>>
>> David
>>
>> --
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> ---
>> You received this message because you are subscribed to a topic in the
>> Google Groups "ClojureScript" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/clojurescript/ex2GmRMQCXQ/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/clojurescript.
>>
>
>
>
> --
> "Are we what we become or do we become what we are?" - Prof. Beno Gross
>



-- 
"Are we what we become or do we become what we are?" - Prof. Beno Gross

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to