3 ms·
I've always liked `mapAccumL`, and nowadays, it amuses that it is implemented by mapping with a state monad (and thus it works over arbitrary Traversables). Wi
by momentoftop 6y ago
I've always liked `mapAccumL`, and nowadays, it amuses that it is implemented by mapping with a state monad (and thus it works over arbitrary Traversables).
With that precedent, I'm happy to implement the given problem using State and traverse, so it's structured much the same as the obvious imperative version:
incr :: (MonadState s m, Num s) => m s
incr = get <* modify (+1)
annotate :: [Section x] -> [Section Int]
annotate ss = flip evalState 1 . forM (zip [1..] ss) $ \(i,Section title reset lessons _) -> do
when reset (put 1)
lessons <- (traverse . traverse) (const incr) lessons
pure $ Section title reset lessons i