New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is flatMap really necessary? #4
Comments
You're likely right on this one - simpler is better for proposals like this one. flatten can be tricky if it implements deeper flattening or single-level flattening, I'll do some research to get the more common intention and attempt to use that to simplify and remove the need for a specific flatMap method |
Please consider TypeScript users.
|
That depends on the depth of the flattening - the ability to flatten recursively is certainly up for debate. It can be useful, but it does add complexity. Simplifying it may also allow flatMap to become map().flatten() |
If |
@RangerMauve xstream
Example:
The only way
|
Tried to make a counter example, and now I see your point. E.g. const it = Iterator<Iterator<Number>>;
const flattened = it.flatten<Number>(); |
It looks like TypeScript can actually handle this pretty well. xstream is a poor example.
|
tc39/proposal-observable doesn't spec combinators, but how likely is it that |
The observable proposal has purposefully been avoiding defining any operators, I'd say that it's more likely that anything defined here is going to influence whatever gets added in an Observable-hof proposal later on. Unless it somehow gets made and passed first which is cool too. |
Although one can implement function filter(xs, predicate) {
return xs.flatMap(x => predicate(x) ? [x]: []);
}
function map(xs, projection) {
return xs.flatMap(x => [projection(x)]);
} The answer to this Stack Overflow question really hits the nail on the head. |
@aluanhaddad That's a really good point. Is there maybe a list of operators not in the spec that would be easy for users to define using flatMap? That might be compelling reason to include it as well. Personally, I still think that having a minimal set of operators that are different from each other would be easier for people to get their head around. But if it's true that it would provide a lot of utility for people, I can see the usefulness. Obviously, this is just my opinion so it's up to the spec authors and TC39 to decide in the long run. |
@RangerMauve technically all you need is In Scala, if your type has a In C# if your type has a These both, from what I understand borrow the notion from Haskell. And there are many others. This is an example in C# of a flatMappable option type that can be used in LINQ expressions. [Fact]
public void SelectOptionFromNullPropagatesOptionOfNoneIntoProjection()
{
var target = FromNullFactory<object>();
var projected =
from value in target
select 1;
Check.ThatCode(() => projected.Value).Throws<InvalidOperationException>();
} The source for the Option type is here |
On can achieve the exact same effect by chaining
map
andflatten
. When xstream (an Observable implementation) was being made, the creator, @staltz, omitted "flatMap" because [it was easier to understand]. I think this reasoning applies to higher order functions with Iterators.The text was updated successfully, but these errors were encountered: