I've used cats for the first time to solve day 1 of advent of code and I'm wondering if it's possible to improve things.
Given a method update
with the following signature
def update(i: Instruction): PosAndDir => PosAndDir
I've come up with :
val state: State[PosAndDir, List[Unit]] = instructions.map(i => State.modify(update(i))).toList.sequenceU
val finalState = state.runS(PosAndDir(Pos(0, 0), North)).value
And also
def update2(i: Instruction): State[PosAndDir, Option[Pos]] =
State.modify(update(i)).inspect(pad => if (i == Walk) Some(pad.pos) else None)
…
val state = instructions.map(update2).toList.sequenceU
val positions = state.runA(PosAndDir(Pos(0, 0), North)).value.flatten
More precisely, questions are :
- why do we need to call
.value
(with scalaz, it's transparent) ? - is there a way to write
update2
with a for comprehension to improve readability ? - is there an
Applicative
instance forSeq
in cats (I know there is not in scalaz). ? - any idea to improve the code ?