4 ms·
In Scala you can just do: employees.filter( _.isPartTime ) And it runs in the same virtual machine.
by muyyatin 16y ago
In Scala you can just do:
employees.filter( _.isPartTime )
And it runs in the same virtual machine.
- erikpukinskis 16y agoIn Ruby: employees.find_all &:isPartTime
- patio11 16y agoAnd I liked employees.select {|employee| employee.part_time?} Crikey, should buy a book...
- Confusion 16y agoPersonally, I prefer your version; the &: version starts reeking of Perl line noise. Although I believe Ruby would benefit from an _ for not explicitly named block arguments.
- mark_l_watson 16y agoI agree, I also prefer the slightly longer version. OK, enough small talk ... :-)
- steveklabnik 16y agoAs a random note for anyone who's wondering how this syntax came about, this is the Symbol#to_proc [1] method, which was actually a monkeypatch from Rails that ended up making its way back into Ruby proper. 1: http://ruby-doc.org/core-1.9/classes/Symbol.html#M000031 http://ruby-doc.org/core-1.9/classes/Symbol.html#M000031
- jarin 16y agoThank goodness Symbol#to_proc was introduced in Ruby 1.9, the Rails implementation of it was slow as hell: http://www.lukeredpath.co.uk/blog/optimising-symbol-to_proc.html http://www.lukeredpath.co.uk/blog/optimising-symbol-to_proc....
- wuputah 16y agoThat was fixed some time ago. It is very marginally slower to use Symbol#to_proc now (a constant time difference to actually create the Proc), but not enough to warrant a change in practical use.
- tednaleid 16y agoYeah, that improved java is still pretty ugly for a JVM language. In groovy its: employees.findAll { it.isPartTime() }
- ataggart 16y agoAnd in clojure it would be (filter :part-time? employees)
- didip 16y agoJoining the club, in Python: [e for e in employees if e.isPartTime()] You can even push it further: [e for e in employees if e.isPartTime() or e.lazy() or e.havePassiveIncomeAlready()] and further: [e for e in employees if e.isPartTime() or e.lazy() or e.havePassiveIncomeAlready() or e in [exec for exec in BoardofDirector.all() if exec.haveTooManyPortfolio()]] Above is your friendly neighborhood List Comprehension available since Jython 2.0