3 ms·
Having migrated to Clojure I prefer: (reduce + (range 1 100001)) but you can make yours a little nicer by relying on a Range being enumerable, hence: (1..100
by sandbags 11y ago
Having migrated to Clojure I prefer:
(reduce + (range 1 100001))
but you can make yours a little nicer by relying on a Range being enumerable, hence:
(1..100000).inject(:+)
- eggy 11y agoI prefer J: +/>:i.100001 You could takeout the >: but then the range is 0 indexed and not 1-based: +/i.100001
- sandbags 11y agoNow that is incredibly terse. "/" is the J reducing function? Or is it not comparable?
- eggy 11y agoIt is J's 'insert' function. It inserts the verb on the left between the items on the right. I think I've see it called 'apply' for J too, and I think 'reduce' as defined in other functional languages would be correct; it's just that J calls it 'insert'.