24
loading...
This website collects cookies to deliver better user experience
number -> Incomplete Board -> Either (Bingo Board) (Incomplete Board)
. Using sequence
we can turn our array of Eithers into Either a Bingo Board or an array of Incomplete Board. sequeunce:: Monad m => [m a] -> m [a]
show
function for debugging which enabled me to pick this error up quickly.export declare const fold: <E, A, B>(onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either<E, A>) => B
. I had previously used identity
as the onLeft function, however that sets the return type B
and this caused a type error when i returned an Either for my onRight function. After realising my error, I returned an Either for my onLeft function.E.fold(
(complete) => E.left(complete),
(incompletes) => play(rest)(incompletes)
)
sequence
in part one which eagerly evaluates the first Left. Now in Part 1, sequence
works because we stop immediately when we have found our winning bingo game so the return type of Either Complete Incomplete [] makes sense. However in part 2, it is not one or the other, we need both until we finish going through all the bingo numbers to find the last winning bingo board. Hence we need a return type of (Option<(number, Complete)>, Incomplete[]) representing the last winning bingo number and bingo board and the remaining list of incomplete boards. (This thought occurred to me several days later).playLast:: number -> Incomplete[] -> (Option<(number,Complete)>, Incomplete[])
partitionMap
function which has as similar signature to partitionEithers:: [Either a b] -> ([a], [b])
. I then return the head of the Completed games.won
. To do this, I created a Monoid over Optionsconst getLastSome = O.getMonoid<any>(S.last());
never
into there and it won't work with any Options. pipe
.