HACKER Q&A
📣 pipeline_peak

Can we just take a moment to appreciate how elegant C# is?


var list = Enumerable.Range(0, 20).Select(i => new { Foo = i, Bar = "hey" });

This is beautiful!


  👤 vips7L Accepted Answer ✓
Isn't this every language now? Apart from the anonymous type even Java can do it elegantly:

    var lst = IntStream.range(0, 20)
        .map(i -> Pair.of(i, "hey"))
        .toList();

👤 e_coli
~ 40 years ago, Smalltalk had:

(1 to: 20) collect: [ :each | Array with: each with: 'hey' ]

And Smalltalk got it from Lisp.

Just saying for comparison.

Edit: forgot to mention - the syntax is entirely consistent and unlike Linq does not seem bolted on to the rest of the language.


👤 neximo64
Javascript does that too with its ES6 chaining syntax, would you say it is elegant too?

var list = Array.from({length:20}, (_, i)=>({"Foo":i+1, "Bar": "hey"}))


👤 uejfiweun
For my understanding, what is happening in "new { Foo = i, Bar = "hey" }"? Is that some type of tuple or struct or something?

Anyway I agree, I like C# for the same reasons I like Java. Very readable but still powerful.


👤 jbjbjbjb
This is Linq (and anonymous types). There are ports of Linq for other languages and a lot languages have similar features. It is nice though.

👤 JohnCurran
In Elixir:

list = Enum.map(1..20, & %{foo: &1, bar: "hey"})

Not meant to be a comparison, just another example


👤 asadawadia
Kotlin to get a List>

val list = (1..20).map { Pair(it, "hey") }