question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative implementation of Maybe#concat

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
evilsoftcommented, May 20, 2019

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.

1reaction
jonwhelancommented, May 9, 2019

@evilsoft Thanks for the thorough explanation! If I’m understanding it correctly, essentially all the Maybes are getting mapped over and the Nothings are defaulted to an empty Just(Assign.empty()) by virtue of alt, thus allowing the reduce to encounter all Justs.

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 using alt as you and @dalefrancis88 have suggested.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found