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.

I can’t see a flatMap function or its equivalent which is typical for monads. It should work like Haskell’s >>=.

My suggested implementation:

infix inline fun <V, E, U> Result<V, E>.flatMap(transform: (V) -> Result<U, E>) : Result<U, E> {
    return when (this) {
        is Ok -> transform(value)
        is Err -> this
    }
}

Also flatMapError function could be implemented in simillar way. If you like the idea, I will implement them along with unit tests and create a pull request.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Raviraelcommented, Nov 22, 2017

You mention the extension function for Kotlin’s Mapclass. However there is also a version for Iterable with signature fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> . As you can see this is really similar to your andThen by parameters and return type. Both functions are defined for some Monad<T> take function (T) -> Monad<R> and return Monad<R>, that’s why this name seems reasonable for me.

Also in this alternative library that operation is called flatMap.

Of course using Elm’s convention also makes sense.

1reaction
Raviraelcommented, Nov 22, 2017

Well, there also is a flatMap method in Java’s Optional and similary flatMap in Scala’s Option. They are equivalent to your andThen as much as they can be!

I agree that the name itself is confusing, however it somehow shows a relationship with map. Still I believe this is a common name for this type of operation in Java’s world.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array.prototype.flatMap() - JavaScript - MDN Web Docs
The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening...
Read more >
What's the difference between map() and flatMap() methods in ...
The flatMap method lets you replace each value of a stream with another stream and then joins all the generated streams into a...
Read more >
The Difference Between map() and flatMap() - Baeldung
The map() method wraps the underlying sequence in a Stream instance, whereas the flatMap() method allows avoiding nested Stream<Stream<R>> ...
Read more >
JavaScript Array flatMap() By Practical Examples
The flatMap() method first maps each element in an array using a mapping function and then flattens the results into a new array....
Read more >
A Smarter JavaScript Mapper: array.flatMap() - Dmitri Pavlutin
array.flatMap() method is the way to go if you want to map an array to a new array, but also have control over...
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