, @Benjamin Hodgson , , f
. - , , , . :
import Control.Monad.State
indexed :: (Traversable t) => t a -> (t (Int, a), Int)
indexed t = runState (traverse addIndex t) 0
where addIndex x = state (\k -> ((k, x), k+1))
scanMap :: (Traversable t) => (a -> a) -> t a -> [t a]
scanMap f t =
let (ti, n) = indexed (fmap (\x -> (x, f x)) t)
partial i = fmap (\(k, (x, y)) -> if k < i then y else x) ti
in map partial [1..n]
indexed
, ( "", ):
> indexed ['a','b','c']
([(0,'a'),(1,'b'),(2,'c')],3)
, , , mapAccumL
:
indexed = swap . mapAccumL (\k x -> (k+1, (k, x))) 0
scanMap
, fmaps /, indexed
partial
, partial i
"afters" " i
" befores " .
> scanMap (*2) [1,2,3]
[[2,2,3],[2,4,3],[2,4,6]]
- , , :
func :: (Traversable t, Applicative f) => (a -> f a) -> t a -> f (t a)
:
func' :: (Traversable t) => (a -> [a]) -> t a -> [t a]
, .