3 ms·
Cool one liner! Here's some other languages: Python: 2 * sum([x for x in range(1, 13)]) Haskell: 2 * sum [1..12]
by Shoop 7y ago
Cool one liner! Here's some other languages:
Python: 2 * sum([x for x in range(1, 13)])
Haskell: 2 * sum [1..12]
- why_only_15 7y agoYou don't need the list comprehension for python - just 2 * sum(range(1, 13))
- cool-RR 7y agoAnd drop the 1, too
- Godel_unicode 7y agoI prefer the explicit example for being explicit, I find it more pythonic. GPs example is perfect Python; every character has meaning, and it's extremely readable even for non-programmers.
- gorgoiler 7y agoWhat’s quite elegant about Ruby (in and of itself, not in comparison to your examples) is how consistently it is implemented using its own simple set of core features, with very little magic or syntactic sugar. reduce, Enumerable, what the & operator is doing etc. What’s so challenging about Ruby is how the language can be abused by library vendors to make all kinds of surprising magic and homegrown syntactic sugar.
- bnegreve 7y ago> What’s quite elegant about Ruby is how consistently it is implemented using its own simple set of core features, with very little magic or syntactic sugar. Honestly, I don't think this has much to do with Ruby. This a consequence of using reduce or any other higher-order functions, which come from functional programming, and are now available in almost all modern multi-paradigm programming languages (including Python and Haskell). I you like this kind of constructs, you should definitely learn a functional programming language, you'll love it.