Generalised Partition for all Fallible monads #1405
louthy
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In this release there are now generalised
Partition
,Succs
, andFails
methods (and their equivalentPrelude
functions:partition
,succs
, andfails
) that work with anyFoldable
ofFallible
monads.This is the main
Partition
extension:So, if your
F
is aIterable
and yourM
is anIO
(soIterable<IO<A>>
), then you can runPartition
on that to get aIO<(Seq<Error> Fails, Seq<A> Succs)>
. Obviously this will work with anyFoldable
types you have made too (as well as all the built-in ones:Arr
,Lst
,Iterable
,Seq
,Set
,HashSet
, etc.) -- with any effect type as long as itsFallible
and aMonad
. IncludingFallible
types with a bespokeE
error value (but you will have to specify the generics as it can't infer from the arguments alone).I have also added a
Partition
extension andpartition
prelude function that works with anyFoldable
structure. Unlike thePartition
function that works withFallible
types (which partitions on success or failure), this one takes a predicate which is used to partition based on thetrue
/false
return. It's likeFilter
but instead of throwing away thefalse
values, it keeps them and returns aTrue
sequence andFalse
sequence:All foldables get a default implementation, but it's possible to override in the trait-implementation (for performance reasons mostly).
Finally, I've added unit-tests for the
Foldable
default-implementations. Anybody who's been following along will know that theFoldable
trait only has two methods that need implementing and over 50 default implementations that we get for free. So, the unit test makes sure they work! (which should guarantee they work for all foldables as long as the two required method-implementations are correct).I'm considering how I can refactor those unit-tests into a
FoldableLaw
type that will work likeMonadLaw
,FunctorLaw
, etc. But that's not there yet.This discussion was created from the release Generalised Partition for all Fallible monads.
Beta Was this translation helpful? Give feedback.
All reactions