3 ms·
I loathe most shortcuts: too many things to remember and usually cryptic syntax. Maybe that's because I have to be a polyglot developer (Elixir, JavaScript, Rub
by pmontra 4y ago
I loathe most shortcuts: too many things to remember and usually cryptic syntax. Maybe that's because I have to be a polyglot developer (Elixir, JavaScript, Ruby, Python) and I can't remember all the tiny details of each language.
If you want to give a try to Ruby, get to understand blocks quickly. Instead of
for item in list:
work_on(item)
which would work (with a closing end and no : ), the idiomatic way is
list.each do |item|
work_on(item)
end
or the one liner
list.each {|item| work_on(item)}
Check also map, select, each_with_index, each_with_object, etc. Blocks are fundamental to Ruby for reason that only practice can tell.
- greenpeas 4y agoOr with numbered parameters (since ruby 3.0) list.each { work_on(_1) }
- pmontra 4y agoWhich is exactly the kind of shortcut I loathe.
- greenpeas 4y agoyou're not alone :) https://bugs.ruby-lang.org/issues/15723 https://bugs.ruby-lang.org/issues/15723 This syntax looks ugly, but it's really nice when writing short scripts. Combined with one line function declarations: `def foo(n)= bar`, it can make an irb session much faster/shorter.
- BiteCode_dev 4y agoSwift has this as well, and it's really nice. Doing map(lambda x: x + 1, stuff) or [x + 1 for x in stuff] adds nothing of value for those very simple cases. I wish we had map($1 + 1, stuff), or ever better, stuff |> $1 + 1. Python does a lot of things right, but for a language with a strong emphasis on the iterator design pattern, I wish it would provide more expressive primitives. However, it comes with the FP territory, which Python is not well suited for, and it would also imply a higher cost of entry to beginners.
- kofejnik 4y agoQuite horrible imo. Creeping closer and closer to Perl with each iteration
- jjgreen 4y agoSince "work_on" takes a single argument which is a instance of Item, you'd be better off implementing it as an instance method, so item.work_on. Then you have the even more compact list.each(&:work_on)