Comments (11)
You mention the extension function for Kotlin's Map
class. 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.
from kotlin-result.
Yes, that's it. Thank you!
from kotlin-result.
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.
from kotlin-result.
Do you mean andThen?
from kotlin-result.
I don't see how that's semantically a flatMap
, when you look at Kotlin's definition of a flatMap
(applies a function to every element of a map, returning a single list of the elements). The naming andThen
comes from Elm's andThen
.
Anyway glad to help, let me know if there is anything else missing.
from kotlin-result.
The flatMap
on an Iterable<T>
conforms to Kotlin's definition of flatMap
as it returns a single List<R>
of elements yielded from the results of the transform
function being applied to each element (T
) in the Iterable<T>
. The signature for andThen
doesn't operate on an Iterable
nor a Map
- and does not yield a List
of Result
s, just a single Result
. I think the naming in that library is misleading.
from kotlin-result.
I think that's a stronger argument, however I still feel uneasy given Kotlin's definition of flatMap
. Happy to consider adding flatMap
as an alias of andThen
on the back of your points if people think it's necessary.
from kotlin-result.
FWIW, I also scratched my head looking for the missing flatMap
function.
I think it's okay for flatMap
to have a slightly different definition when applied to an Iterable or Collection as when applied to something like Result (or Either, or Option, etc...).
It's the same way in Scala land.
flatMap
is the Monad bind operation, and the Monad instance for an Iterable or Collection is different than for Result, yeah?
from kotlin-result.
Does Scala have a Result type with flatMap
on it? I'm under the impression that its existence on the Optional
class is due to the fact that Optional
can be argued as a unary "collection", which is much harder to argue for in the Result class.
Not to mention other functional languages have opted for andThen
as their naming scheme for this method:
- Rust: Result.and_then
- Elm: Result.andThen
If this is an issue for you due to some form of naming conflict, then you could always use import as
to alias andThen
to flatMap
.
from kotlin-result.
It has Either
, which is basically the same thing (except it's right-biased, whereas your Result is left-biased because it names the left and right as value/error).
I'm not disputing andThen
as an appropriate name, but this operation is fundamentally flatMap
. If anything, andThen
should alias flatMap
.
from kotlin-result.
On the back of flatMap
existing on Scala's Either
, I've added an alias function named andThen
to Map.kt
(44a4467).
Thanks to both of you for your input.
from kotlin-result.
Related Issues (20)
- Implement method into_ok_or_err for Result<T, T> HOT 5
- Please Refactor to Use Kotlin Result For 1.5+ HOT 4
- Master doesn't compile in local HOT 2
- Struggle to run Gradle Check locally HOT 3
- Use of runCatching and coroutines breaks structured concurrency HOT 6
- getOrThrow? HOT 4
- Migrate to new kotlinx-coroutines-test API
- iosSimulatorArm64 and macOsArm64 support HOT 22
- Rename Result to Either HOT 7
- Assertion helpers for tests HOT 3
- Feature request: mingwX86 target HOT 2
- Extensions for Kotlin Flow? HOT 7
- About component1() and 2 HOT 2
- Best practices for integrating with Flow<T> HOT 1
- ensureNotNull-alike binding scope extensions HOT 15
- e: Could not load module <Error module> HOT 6
- Recovery extension HOT 3
- Consider using consistent "error" naming over "failure"? HOT 2
- Consider adding zipOrAccumulate? HOT 13
- Documentation of order of elements in Iterable.kt HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-result.