D
D
Denis Kostousov2016-05-31 13:30:55
Java
Denis Kostousov, 2016-05-31 13:30:55

Is there a good implementation of a lazy monad in Java?

Need a simple lazy evaluation library for java. Essentially one class. Initialized by the function T Lazy<T> of(Supplier<T> s)
Minimal interface - get method, on the first call starts the mentioned Supplier and caches its response. On the second call, it simply returns the value from the cache.
Is there something already ready?
PS Is it a monad at all?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
guras256, 2016-05-31
@sandello

1) kotlin
2) own implementation in 5 minutes

M
Maxim Moseychuk, 2016-05-31
@fshp

Is it even a monad?

No, this is an implementation of lazy evaluation. It has nothing to do with monads.

B
bromzh, 2016-05-31
@bromzh

You can take Rx. There is such a thing as BehaviorSubject . All new subscribers will get the value from the "cache". When the data is updated, all subscribers will receive it.
The only problem is that the BehaviorSubject needs to be initialized with initial data.
But there are also so-called "cold" Observables in Rx. There, the calculation/action chain will not be called until there are no subscribers. Those. this is the kind of laziness that is needed. And most likely, there is a way to do both laziness and cache using Rx, I just don't know this library so well. But Google should know.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question