5 ms·
Try to build a class with it and you will understand.
by MrBra 11y ago
Try to build a class with it and you will understand.
- davexunit 11y agoI've built many a class in Ruby.
- MrBra 11y ago> I've built many a class in Ruby. I see..... Anyway, this is elegant to me: sum ||= (1..100000).to_a.inject(:+) Maybe you have a different aesthetical sense than the rest of us. That's perfectly fine, we don't have to like the same things. But since you questioned, now I'm curious and I would like to ask you: what language in your opinion has the most elegant syntax? Also if you have 2 extra minutes free, would you mind to code that same one-liner above in that language so we could compare the two?
- giovannibajo1 11y agoIn Python it's x = sum(range(100000)). You might say that's cheating because there is a specific sum function, but having smart builtins covering common programming cases is exactly what an elegant language offers.
- sandbags 11y agoHaving 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'.
- davexunit 11y ago>what language in your opinion has the most elegant syntax? The Lisp family, Scheme in particular. (define sum (reduce + 0 (iota 100000 1)))