I'm trying to refine my attempt at firstRepeat.
firstRepeat ::
Ord a =>
List a
-> Optional a
firstRepeat =
flip eval S.empty
. findM (\x -> State (\s -> (x `S.member` s, x `S.insert` s)))
(where S is Data.Set)
I noticed I am taking (S.member, S.insert) and applying `x` and `s` to both parts of the tuple, and I was interested in seeing if there was a way to express this. I looked at using (&&&) from Control.Arrow, which gives me:
(b -> c) -> (b -> d) -> b -> (c,d) -- specialised to (->)
So I can get this:
flip eval S.empty
. findM (\x -> State $ S.member x &&& S.insert x)
Is there a nice way to refine this, getting rid of the explicit `x` reference?
Cheers,
David