I find the 'exercise for the user' quite confusing since no clear signature for those methods are given and the return type changes between the examples.
Anyway, if anyone is wondering, flatMap is used because getAddress returns an Option<Address> in one of the examples. map is used instead with 'getStreet' because it returns a plain String.
CTO, coder, and architect at Senacor Technologies in Germany. Coding in Java, JavaScript, Go, whatever gets the job done. Get in touch via @koenighotze on Bluesky.
Good point, thank you. I have added a small extra sentence, that hopefully reduced the confusion.
To add to your answer: flatMap expects a mapper that returns an Option. map on the other hand wraps the result of the applied mapper function into either some or none.
In short: map creates a new "Gift Box" and flatMap reuses the "Gift Box".
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I find the 'exercise for the user' quite confusing since no clear signature for those methods are given and the return type changes between the examples.
Anyway, if anyone is wondering,
flatMap
is used becausegetAddress
returns anOption<Address>
in one of the examples.map
is used instead with 'getStreet' because it returns a plainString
.Good point, thank you. I have added a small extra sentence, that hopefully reduced the confusion.
To add to your answer:
flatMap
expects a mapper that returns anOption
.map
on the other hand wraps the result of the applied mapper function into eithersome
ornone
.In short:
map
creates a new "Gift Box" andflatMap
reuses the "Gift Box".