types - Fixed size matrix and Maybe -
i writing board game in purescript involves matrix of exact size 2x7 (in variations can 4x7). package i’m using has matrix.getrow
function returns maybe (array a)
.
what best approach not have deal maybe
returns when know sure matrix.getrow 0
going return first row (because matrix of fixed size 2x7)?
currently have ugly code deal maybes not desirable:
notpossible :: array cell notpossible = [99, 99, 99, 99, 99, 99, 99] -- never used row n = frommaybe notpossible $ matrix.getrow n state.cells
purescript uses type system track partiality, partiality property function not produce return value possible inputs.
if want circumvent type system , guarantee not pass invalid inputs can use partial.unsafe.unsafepartial :: forall a. (partial => a) -> a
function purescript-partial
package.
by using partial function fromjust
data.maybe
data.maybe.fromjust :: forall a. partial => maybe ->
you can construct unsafe row function:
unsaferow n xs = unsafepartial fromjust (matrix.getrow n xs)
you can delay calling unsafepartial point @ can guarantee index never out of bounds, type system propagate automatically you.
Comments
Post a Comment