문제

I am stumped again while trying to work with Edwards lens library. I try to snoc something onto the end of a vector in a state context:

data Foo = Foo {
  _vec :: Vector Int
}

makeLenses ''Foo

testCons x = vec <>= singleton x

While this works I'd like to use [cons][2] but I have no idea how. The documentation mentions [0,1,2] |> 3 === [0,1,2,3] but I have no idea how to do this in the state context.

도움이 되었습니까?

해결책

The (%=) combinator lets you apply a function to the target of a Lens; you want something like

stateSnoc :: MonadState Foo m => Int -> m ()
stateSnoc x = vec %= (|> x)

다른 팁

snoc seems to be a plain function defined for convenience's sake from the original Prism, which is _Snoc.

So why not use a plain MonadState function with it, like modify?

runState (modify $ flip snoc 'a') ['b']
-- ((),"ba")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top