Alternative implementation of Maybe#concat
See original GitHub issueI think this may be just an alternative implementation of Maybe#concat rather than a bug, but the concat
method is biased towards Nothing
and will result in the expression evaluating to Nothing
if one is present.
Just([1]).concat(Nothing())
Actual: Nothing()
Expected: Just([1])
In haskell if we do the same: Just [1] <> Nothing
will evaluate to Just [1]
.
My particular use case for the proposed implementation is to build up an object from several Maybe Assign({})
and have it evaluate to a Just
even if a Nothing
is encountered.
E.g.
Just(Assign({a: 1})).concat(Just(Assign({b: 2}))).concat(Nothing())
Wanted: Just(Assign({a: 1, b: 2}))
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
2 Ways to Merge Arrays in JavaScript - DEV Community
Here are 2 ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need...
Read more >RxJava2: Alternative to rx.Observable method first(predicate)
Consider using Maybe instead of Observable of nullable type (it won't work with RxJava2). Then use Maybe.concat to get only emitted items as ......
Read more >Class: Funkr::Types::Maybe - RubyDoc.info
Includes: Alternative, Applicative, Functor, Categories, Monad, Monoid ... [a] The Maybe.concat function takes a list of Maybes and returns a list of all ......
Read more >Filterable algebra? · Issue #33 · fantasyland/fantasy-land
I just want to make Stream<Stream> invalid but still implement ... you can do it with (Alternative m, Monad m) => m constraint:....
Read more >io.reactivex.observable.Maybe.concatArray java code examples
<p> * <img width="640" height="422" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.concat.png" alt=""> * <dl> ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So in playing with this for a while now, I cannot get around the laws for anything like this. This issue is the empty or Monoid portion. It is okay with Semigroup because it speaks to the value the Maybe is wrapping, but adding implicit empty with
Nothing
, we would then to to transport the context of the inner Semigroup type to the Outer Maybe type.So because we have
alt
and can transport that inner context to the outer, I am going to have to pass on this alternative. Even though other libraries allow it, I see it as having other implications for such an non-common case.@evilsoft Thanks for the thorough explanation! If I’m understanding it correctly, essentially all the
Maybe
s are getting mapped over and theNothing
s are defaulted to an emptyJust(Assign.empty())
by virtue ofalt
, thus allowing thereduce
to encounter allJust
s.My solution was similar but I was just mapping over with a function like the following prior to reducing:
either(constant(Just(Assign.empty())), Just)
. But I like the elegance of usingalt
as you and @dalefrancis88 have suggested.