Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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?



In 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.


I wouldn't say it's cheating but without using that function maybe we could see more of the language syntax itself. That way we are just seeing method calling.


In Crystal we also have sum: http://crystal-lang.org/api/


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(:+)


I prefer J:

  +/>:i.100001
You could takeout the >: but then the range is 0 indexed and not 1-based:

  +/i.100001


Now that is incredibly terse. "/" is the J reducing function? Or is it not comparable?


It 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'.


>what language in your opinion has the most elegant syntax?

The Lisp family, Scheme in particular.

(define sum (reduce + 0 (iota 100000 1)))




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: