by sillysaurusx · 6y ago · 0 comments
[x for x in foo if x not in y]
Turns out, this can be expressed in Lisp: (list x for x in foo if x not in y)
I've implemented this into my fork of Lumen that runs on Python. https://github.com/shawwn/pymen It feels very nice to use: > (list x for x in (range 10) if (= (% x 2) 0))
"""
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
"""
[0, 2, 4, 6, 8]
>
It wasn't even that ugly to implement. It's ~20 lines: https://github.com/shawwn/pymen/blob/1c191c9e00e73303a6479f8...One other neat thing is that the REPL prints out the docstring of the last evaluated thing by default. It makes it nice to explore libraries:
> (import numpy as np)
https://imgur.com/1rpF95y > np.vsplit
https://imgur.com/OjIvTWtIt's like an automatic help() call on the last evaluated thing.
(If anyone happens to try it out, you can get into a REPL by running `rlwrap bin/pymen`, or just `bin/pymen`.)
But list comprehensions suffer from the same problem the author is taking about, they don't compose. Its even worse than CL remove-if variant, where one can reify the filter into a function that can then compose.
[x| x <- foo, not (elem x y)]
Here's an example finding consonants: [c| x <- ['a'..'z'], not (elem x) $ "aeiou"]A nice thing about Haskell list comprehensions is they're based on a trivial transformation to monad syntax, which makes it easier to factor out complex list transformations into multiple little bites. I've run into this problem in Python. Also, Python list comprehensions can often behave very unexpectedly due to mutability.
Here's an example of the monad syntax:
[(x,y) | x <- [1..10], y <- [1..x], odd (x + y)]
equiv do
x <- [1..10]
y <- [1..x]
guard $ odd (x + y)
return (x,y)
Lots of combinatorics problems can be elegantly solved using the list monad like that. CL-USER> (let ((foo '(1 2 3)) (y '(2 4)))
(loop for x in foo unless (member x y :test #'=) collect x))
;; -> (1 3)
It wouldn't be hard to write a macro that expands your expressions to CL's loop, with a bit of magic to parse the "if ... [not] in ...".> import whatever > filter(_ not in y, foo)
For me it's more readable. To each their own, of course.