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

Let's make Ternary able to have the same sticky value for each evaluation #1475

Closed
victornoel opened this issue Sep 27, 2020 · 2 comments
Closed
Assignees

Comments

@victornoel
Copy link
Collaborator

victornoel commented Sep 27, 2020

Since most of the cactoos classes are lary-evaluated, sometimes when using Ternary, each call to each of the Scalar it holds in its state will result of a different evaluation of the input value.

For example, the meaning of the following is not very clear:

final Scalar<Integer> scl = new ScalarOf<>(new AtomicInteger(0)::incrementAndGet);
new Ternary<String>(
    () -> scl.value() == 1,
    () -> scl.value() + " equals " + 1,
    () -> "else: " + scl.value()
).value() // returns "2 equals 1"

It is currently possible to write:

final Scalar<Integer> scl = new ScalarOf<>(new AtomicInteger(0)::incrementAndGet);
new Ternary<String>(
    scl.value(),
    i -> i == 1,
    i -> i + " equals " + 1,
    () -> "else: " + i
).value() // returns "1 equals 1"

But it forces us to evaluate scl, which goes against the idea of only composing objects. This means we can't use it in constructors for example where we want to avoid code in them.

Instead, we would like to be able to write:

final Scalar<Integer> scl = new ScalarOf<>(new AtomicInteger(0)::incrementAndGet);
new Ternary<String>(
    scl,
    i -> i == 1,
    i ->  i + " equals " + 1,
    i -> "else: " + i
).value() // return "1 equals 1"

With i being stable the whole value() evaluation.

It would be for example useful to tackle #1460.

@victornoel
Copy link
Collaborator Author

@0crat assign me

@victornoel
Copy link
Collaborator Author

@0crat wait for #1476

@0crat 0crat added the waiting label Sep 27, 2020
victornoel added a commit to victornoel/cactoos that referenced this issue Oct 4, 2020
victornoel added a commit to victornoel/cactoos that referenced this issue Dec 27, 2020
victornoel added a commit to victornoel/cactoos that referenced this issue Dec 28, 2020
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants