Skip to content
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

#1408 withUnaryOperator style flag/naming template #1409

Merged
merged 8 commits into from Oct 15, 2022

Conversation

elucash
Copy link
Member

@elucash elucash commented Oct 12, 2022

Implementation of #1408 using naming template as enabling style flag

@elucash elucash added this to the 2.9.3 milestone Oct 12, 2022
@elucash
Copy link
Member Author

elucash commented Oct 12, 2022

walk-in reviews are welcome

ImmutableWithUnaryOperator t = m.mapA(a -> a + 1)
.mapB(b -> "[" + b + "]")
.mapL(String::toUpperCase)
.mapO(o -> o.replace('*', '_'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to have flatMap on optionals ?

Copy link
Member Author

@elucash elucash Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, idk. Because how this feature is activated (by a naming template), I see that flatmap will require separate style flag to enable, Style.withFlatmap for example.
Also it would be fair for collections to also have flatmap, but it will probably need 2 overloads, like Function<? super T, ? extends Iterable<? extends T>> and Function<? super T, Stream<? extends T>>.
BTW (or "did you know?") I've just learned couple of days ago that you can return null in the map function (JDK Optional)

assert Optional.of("X").map(x -> null).isEmpty(); // no NPE

I think if there would be any demand for flatMap, which I see as a separate flag, I can implement flatMap for optional / collections. Otherwise, Encodings should be used to customize any other specific container or collection.
I've already planned to add smth simple for map, there were at least couple of use cases where this is useful for us. Flatmap is more exotic in that sense, that while it's logical to have, we have never needed it on the surface of immutable object.

@asereda-gs
Copy link
Member

asereda-gs commented Oct 13, 2022

Eugene, a while ago you mentioned a more generic and functional approach to modifying Immutables: Lenses/Prisms/Traversables (see comment). Is it something you're still considering ?

@elucash
Copy link
Member Author

elucash commented Oct 13, 2022

Eugene, a while ago you mentioned a more generic and functional approach to modifying Immutables: Lenses/Prisms/Traversables (see comment). Is it something you're still considering ?

Yes, that still on the radar somewhere. Here, I've tried to address only simple a tool for mapping. Right now I'm not sure that this particular usecase would be simpler if lenses are used, it would be more scary looking for sure. But that is actually the API design exercise. I've got to designing something similar while working on Typescript codegen for the data models, the result was pretty interesting and not exactly what I thought it would be, some food for thought. So, the question is if there is demand for this?

The other thing on the radar is the idea of Immutables 3 with more manageable models and many plugin points, this is for some newer LTS (JDK 11+ or even JDK 17+). And any complex new functionality could be implemented on top of that.

@elucash
Copy link
Member Author

elucash commented Oct 15, 2022

thanks for the comments! I think if there's strong demand for related functionality we should discuss it further. Will merge this one. I wanted it to be a simple "no ambition" feature to update things with the function and not be on par with functor-ish (or monadic) map/flatMap. Like in React, you update state either with a value or a function which computes value from the previous value and that's what this is.
Albeit we want to have a separate, strong framework for updating immutable trees, a kind of "optics", but more java-esque (than haskell-like)

@elucash elucash merged commit e264c39 into master Oct 15, 2022
@elucash elucash deleted the 1408-with-unary-operator branch October 15, 2022 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Generate update(UnaryOperator)-methods along withers (new style flag?)
2 participants