7 ms·
I like Kotlins solution for this. If there is only one argument in the lambda/block and the type is clear from the context you don’t have to declare it and you
by vbsteven 8y ago
I like Kotlins solution for this. If there is only one argument in the lambda/block and the type is clear from the context you don’t have to declare it and you can use the ‘it’ keyword instead.
- pkulak 8y agoAnd one parameter is 99% of the cases. If you have more than one, you should probably name them anyway.
- dragonwriter 8y ago> And one parameter is 99% of the cases. In Ruby, the two-parameter hash-iteration case alone is common enough that that's not true. > If you have more than one, you should probably name them anyway. In a single line block, I think that often impairs rather than enhances readability.
- pkulak 8y agok, v impairs readability?
- vbsteven 8y agoFor clarification, it looks like this people.filter { it.age > 42 }
- majewsky 8y agoNeeds more aliases: things.filter { its.age > 42 } people.filter { their.age > 42 } ;)
- deleted 8y ago[deleted]
- skrebbel 8y agoWow, that's splendid! In a sense, it's a very nice dual to "this", which is also a keyword everywhere and not something like $0 or something. I wish more languages had this. Or, it.
- ricardobeat 8y agoIf you go back and read the post and linked ticket, that is indeed the original proposal for Ruby, which somehow morphed into positional arguments.
- jimmychangas 8y agoKotlin is great, but I think the "it" syntax was derived from Groovy.
- zbentley 8y agoI'm not positive, but Groovy may in turn have been inspired by Perl's $_, as in 'map { $_ + 1 } (1..10);'. If I recall correctly, the canonical name for that parameter is 'it' as well (though I've never met a Perl programmer that calls it that . . . er, calls it 'it' . . . er, you get the idea).
- dchest 8y agoThe first time I saw `it` in a similar construction was in Arc language's aif macro: (mac aif (expr . body) `(let it ,expr (if it ,@(if (cddr body) `(,(car body) (aif ,@(cdr body))) body)))) HN uses it quite a lot, e.g.: (defop logout req (aif (get-user req) (do (logout-user it) (pr "Logged out.")) (pr "You were not logged in.")))
- bjoli 8y agoAnaphoric macros have been used in common lisp since forever.
- dchest 8y agoThanks for the name! Here's a wikipedia article about them: https://en.wikipedia.org/wiki/Anaphoric_macro https://en.wikipedia.org/wiki/Anaphoric_macro
- emidln 8y agoPGs On Lisp details these macros from common lisp. I'm unsure how old they are.
- zbentley 8y ago