Control.Applicative.optional allows to handle zero or one Applicatives. many & some allow for 0 or more, or 1 or more, respectively. I'd like to create a function that handles zero, one or two, specifically. The signature could be as for many/some, that is
zeroOneOrTwo :: Alternative f => f a -> f [a]
I feel this should be pretty straightforward, but I've been playing around with it for a while and cannot make it work. Any pointers would be greatly appreciated.
How about this one:
zeroOneOrTwo :: Alternative f => f a -> f [a]
zeroOneOrTwo a = go (2 :: Int)
where
go n
| n > 0 = ((:) <$> a <*> go (n - 1)) <|> pure []
| otherwise = pure []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With