by saagarjha · 3y ago · 0 comments
* I couldn’t really make operators first class functions, but I just autoimported operator which has this but in words
* I wanted partial application and hated lambda syntax, so I hacked it together with some magic. “_0 + 1” is basically equivalent to lambda x: x + 1
* Python really likes to make everything a free function, which messes with the whole left-to-right thing. So I monkey-patched functional methods onto all the collections
Together this means that if I have like a comma separated list of numbers in str and I want to, idk, count how many are above five I’d do something like
str.split(",").map(int).filter(_0 > 5).len
which matches how my brain things about it far better than how Python would like me to write it. It uses some tricks but it’s not actually that bad of a hack IMO: https://github.com/saagarjha/advent-of-code/blob/main/aoc.pyThe example above would like this in modern javascript:
str.split(',').map(char => Number(char)).filter(num => num > 5).length str.split(',').filter(n => +n > 5).lengthIt's really incredible how much such a small feature in the grand scheme of things `(function() { })()` influences all API and library design. Right now passing around functions in Python is ugly and reads as such which is enough of a deterrent for most people.
"," split [ string>number 5 > ] count
or "," split [ string>number ] map [ 5 > ] count
or "," split [ string>number 5 > ] filter lengthBut I do do the following sort of thing in Python: ``` from chainzzz import C
C(some_list).group_by(lambda x: x + 1).map(lambda key: key % 2).size() ```
Where C(...) gives us the same object but with some methods added to it as you can see