emmanueltouzery / prelude-ts

Functional programming, immutable collections and FP constructs for typescript and javascript
ISC License
377 stars 21 forks source link

Lazy evaluation getOrElseGet pattern Option #3

Closed tosun-si closed 6 years ago

tosun-si commented 6 years ago

Hi @emmanueltouzery. I really like the idea of this API and like i am a FP fan.

I am using the VAVR in Java 8 projects.

I have a question, the API doesn't provide a "getOrElseGet" in the pattern Option, with a supplier in parameter (allows a lazy evaluation for the OR case).

Have you planned to add it in a futur release ?

Thanks for your help.

emmanueltouzery commented 6 years ago

that's a good idea! I'll add it. Any idea for another name than getOrElse? Maybe I can overload it, but overloading has been an annoyance for me with vavr sometimes, so I'm leaning towards giving it another name. I'm thinking maybe getOrCall but unsure.

tosun-si commented 6 years ago

Yes, you can give this name.

What do you think about a "Try monad" in this API too ?

emmanueltouzery commented 6 years ago

good question! Actually I've always been a little confused about Try vs Either in vavr. I don't really see the need for both, or maybe in the specific context of java, where there are checked exceptions. So my idea is to use Either for both uses in prelude. For instance, there is FunctionX.liftEither:

http://emmanueltouzery.github.io/prelude.ts/latest/apidoc/classes/function.function2static.html#lifteither

It'll catch exceptions and return them as Either.Left. I'm interested in any opinion on that topic!

emmanueltouzery commented 6 years ago

I just released 0.6.1 on npm, which adds Option.getOrCall!

tosun-si commented 6 years ago

Thanks @emmanueltouzery !

tosun-si commented 6 years ago

For the Try monad, i can open a topic about this, if you want.

I think a monad is a good thing for errors handling.

The value is put in a composition and computational context.

Then, a default value can be obtained or a throw can be performed.

I think, this type of monad can serve for errors in general, not only for the checked exceptions present in Java.

tosun-si commented 6 years ago

After getting a value, it is easily possible to apply some higuer order functions like map or filter...

emmanueltouzery commented 6 years ago

Do open a bug of course but I don't understand what Try gets you that Either doesn't. Right now it seems to me that Either gets us all we need, including everything you listed?

Cam you give an example, a use case? Something Try gives us that Either doesn't? I can't think of one.

emmanueltouzery commented 6 years ago

also see http://emmanueltouzery.github.io/prelude.ts/latest/apidoc/classes/either.right.html#getorthrow

though I wouldn't advise throwing, but it's available.

tosun-si commented 6 years ago

Ok I'll watch your implementation of the either and keep you posted.

tosun-si commented 6 years ago

Sorry for disturbe you. I tented to install the version 0.6.1 with npm, but it indicates me that the version doen't exist.

emmanueltouzery commented 6 years ago

You must have a typo somewhere, it's there, see:

https://www.npmjs.com/package/prelude.ts

tosun-si commented 6 years ago

Sorry, there was a proxy problem. Can i contact you in MP ? I want to use this API in a Angular 5 project, but i have some trouble.

emmanueltouzery commented 6 years ago

I don't know what you call MP? If you have twitter you could direct message me, i'm @emmanueltouzery

otherwise general web setup instructions are there: https://github.com/emmanueltouzery/prelude.ts#using-in-the-browser

tosun-si commented 6 years ago

Ok, thanks for your help. Sorry for inconvenience.

emmanueltouzery commented 6 years ago

Btw if you're still interested, regarding Try I now added Option.try_, Option.tryNullable and Either.try_ in prelude.

tosun-si commented 6 years ago

Hi Emmanuel,

Thanks for the info. And yes i am still interested :)