News for hackers. Stuff that matters.
Re:Ruby 3.2.0 Is from Another Dimension by robertlagrant on Wednesday January 04, 2023 @07:18AM (#34244677)
The Python one looks fine to me, although I am a Pythonish person.

It uses what people already know: the for something in somethings syntax of the for loop, and the if syntax. Also it's nice that this works in dictionaries, generators and lists.

It also has the same narrative flow of Haskell's list comprehensions, which I think come from set theory:

  [x^2 | x <- [0..10], x `mod` 5 /= 0]
As for your Ruby examples: I think you could argue that the filter_map version is very readable, but not necessarily more so, but the select one looks pretty painful.
Re:Ruby 3.2.0 Is from Another Dimension by petre on Wednesday January 04, 2023 @08:27AM (#34245265)
> As for your Ruby examples: I think you could argue that the filter_map version is very readable, but not necessarily more so, but the select one looks pretty painful.

The select does two passes, which makes it quite inefficient. One does not even need filter_map, since the example is essentially a reduce operation.

   res = (1..10).reduce([]) { |a, x| x != 5 ? a.push(x**2) : a }
This works in ruby 2.5.1. Probably works in 1.9 and mruby as well.
Re:Ruby 3.2.0 Is from Another Dimension by robertlagrant on Wednesday January 04, 2023 @08:38AM (#34245368)
True, although that seems less readable than the comprehension versions. I might be just biased, though.
Re:Ruby 3.2.0 Is from Another Dimension by zem on Wednesday January 04, 2023 @05:30PM (#34252851)
you can define pretty much everything as a reduce operation though