
Hi John,
this is a nice way of expressing it! I was hoping there would be some more
fundamental function that I would have missed though. By fundamental I mean
that I was hoping there was some function of a "core" typeclass like
Alternative that I had missed that was a general case of my function.
Thanks anyway, I learned about lens' partsOf :)
Paul
On Sat, Apr 8, 2017 at 11:20 PM John Wiegley
"PB" == Paul Brauner
writes: PB> Oops, there's a free variable in there, let me fix it: PB> first f [] = Nothing PB> first f (x:xs) = fmap (:xs) (f x) <|> fmap (x:) (first f xs)
If you break up the test from the modification (instead of fusing the operation into 'f'), this sort of thing becomes quite easy to express using lens:
[1..10] & partsOf (traverse.filtered (>2))._head +~ 3
If the list has no elements >2, the operation is a no-op.
-- John Wiegley GPG fingerprint = 4710 CF98 AF9B 327B B80F http://newartisans.com 60E1 46C4 BD1A 7AC1 4BA2