I first used cats to solve day 1 of the appearance of the code, and I wonder if the situation can be improved.
Given the update method with the following signature def update(i: Instruction): PosAndDir => PosAndDir
I figured it out:
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
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, the questions:
- why do we need to call
.value (with a tale, it's transparent)? - Is there a way to write
update2 with a concept to improve readability? - Is there an
Applicative instance for Seq in cats (I know there isnβt in scalal).? - any idea to improve the code?
source share