10

I have 2 Optionals (or Maybe objects) that I would like to combine so that I get the following results:

                ||       first operand 
    second      ++-------------+-------------
    operand     ||    empty    | optional(x)
    ============||=============|=============
    empty       ||    empty    | optional(x)
    ------------++-------------+-------------
    optional(y) || optional(y) |optional(x+y)

In other words, a non-empty Optional always replaces/overwrites an empty one, and two non-empty Optionals are combined according to some + function.

Initially, I assumed that the standard monadic flatMap method would do the trick, but (at least in Java) Optional.flatMap always returns an empty optional when the original Optional was already empty (and I'm not sure if any other implementation would comply with the Monad Laws).

Then, as both operands are wrapped in the same monadic type, I figured that this might be a good job for an Applicative Functor. I tried a couple different functional libraries, but I couldn't implement the desired behavior with any of the zip/ap methods that I tried.

What I'm trying to do seems to me a fairly common operation that one might do with Optionals, and I realize that I could just write my own operator with the desired behavior. Still, I am wondering if there is a standard function/method in functional programming to achieve this common operation?

Update: I removed the java tag, as I'm curious how other languages handle this situation

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
raner
  • 1,175
  • 1
  • 11
  • 21
  • 1
    I've not kept up with Java after 9 but I don't see anything in the documentation that looks like `ap` to me. I suppose you can jury-rig it with `Stream.concat(a.stream(), b.stream())` which will give you a stream that's empty or has 1 or 2 items. You can then operate with it and collect it back to an Optional. But it still feels like hack. – VLAZ Jun 17 '22 at 20:46
  • Why not look at JVM-based functional languages instead (such as Scala or Clojure)? – Olivier Jun 17 '22 at 20:49
  • Thanks @VLAZ - using streams is a decent work-around, but I agree that it feels somewhat hacky. I was hoping that such a straightforward problem would have a simple solution. – raner Jun 17 '22 at 20:51
  • @Olivier I'm open to considering other JVM-based languages. Does Scala's or Clojure's library have a standard function that achieves what I'm looking for? – raner Jun 17 '22 at 20:53
  • Honestly I don't know, but Java is not a functional language (you never hear the word "monadic" when talking about Java). So I guess you will have better luck with some real functional languages. – Olivier Jun 17 '22 at 20:57
  • 1
    In haskell, this would be [`liftU2 (+)`](https://hackage.haskell.org/package/linear-1.21.10/docs/Linear-Vector.html#v:liftU2) – Johannes Kuhn Jun 27 '22 at 19:28

7 Answers7

6

In a functional language, you'd do this with pattern matching, such as (Haskell):

combine :: Maybe t -> Maybe t -> (t -> t -> t) -> Maybe t
combine (Some x) (Some y) f = Some (f x y)
combine (Some x) _ _ = (Some x)
combine _ (Some y) _ = (Some y)
combine None None _ = None

There are other ways to write it, but you are basically pattern matching on the cases. Note that this still involves "unpacking" the optionals, but because its built into the language, it is less obvious.

Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
4

It's not possible to combine optional objects without "unpacking" them.

I don't know the specifics of your case. For me, creating such a logic just in order to fuse the two optionals is an overkill.

But nevertheless, there's a possible solution with streams.

I assume that you're not going to pass optional objects as arguments (because such practice is discouraged). Therefore, there are two dummy methods returning Optional<T>.

Method combine() expects a BinaryOperator<T> as an argument and creates a stream by concatenating singleton-streams produced from each of the optional objects returned by getX() and getY().

The flavor of reduce(BinaryOperator) will produce an optional result.

public static <T> Optional<T> getX(Class<T> t) {
    return // something
}

public static <T> Optional<T> getY(Class<T> t) {
    return // something
}

public static <T> Optional<T> combine(BinaryOperator<T> combiner, 
                                      Class<T> t) {
    
    return Stream.concat(getX(t).stream(), getY(t).stream())
        .reduce(combiner);
}

If we generalize the problem to "how to combine N optional objects" then it can be solved like this:

@SafeVarargs
public static <T> Optional<T> combine(BinaryOperator<T> combiner,
                                      Supplier<Optional<T>>... suppliers) {
    
    return Arrays.stream(suppliers)
        .map(Supplier::get)           // fetching Optional<T>
        .filter(Optional::isPresent)  // filtering optionals that contain results to avoid NoSuchElementException while invoking `get()`
        .map(Optional::get)           // "unpacking" optionals
        .reduce(combiner);
}
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
4

In Haskell you can do this by wrapping any semigroup in a Maybe. Specifically, if you want to add numbers together:

Prelude> import Data.Semigroup
Prelude Data.Semigroup> Just (Sum 1) <> Just (Sum 2)
Just (Sum {getSum = 3})
Prelude Data.Semigroup> Nothing <> Just (Sum 2)
Just (Sum {getSum = 2})
Prelude Data.Semigroup> Just (Sum 1) <> Nothing
Just (Sum {getSum = 1})
Prelude Data.Semigroup> Nothing <> Nothing
Nothing

The above linked article contains more explanations, and also some C# examples.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 1
    This is a really interesting solution. Part of my difficulty with the original problem was that there is a lot of boilerplate code for dealing with all the empty/non-empty combination, and the real "meat" of the problem is how to combine two optionals that are non-empty. The wrapping in a semigroup seems like a nice way to convey which function to use for the combination, and it does not require passing an additional function object – raner Jun 18 '22 at 18:01
3

Here's one way:

a.map(x -> b.map(y -> x + y).orElse(x)).or(() -> b)

Ideone Demo

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 2
    This looks quite ugly but it does do what OP wants. I wish Java provided something more straight forward than having to jump through such hoops. – VLAZ Jun 17 '22 at 21:13
  • Nice! That's a very concise way of implementing this with just `Optional`s (as opposed to `Stream`s). I agree with @VLAZ that I was hoping to have this already implemented in a standard method somewhere. – raner Jun 17 '22 at 21:21
2
OptionalInt x = ...
OptionalInt y = ...

OptionalInt sum = IntStream.concat(x.stream(), y.stream())
    .reduce(OptionalInt.empty(),
        (opt, z) -> OptionalInt.of(z + opt.orElse(0)));

Since java 9 you can turn an Optional into a Stream. With concat you get a Stream of 0, 1 or 2 elements.

Reduce it to an empty when 0 elements,and for more add it to the previous OptionalInt, defaulting to 0.

Not very straight (.sum()) because of the need for an empty().

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
2

You can implement your function in Java by combining flatMap and map:

optA.flatMap(a -> optB.map(b -> a + b));

More general example:

public static void main(String[] args) {
    test(Optional.empty(), Optional.empty());
    test(Optional.of(3), Optional.empty());
    test(Optional.empty(), Optional.of(4));
    test(Optional.of(3), Optional.of(4));
}

static void test(Optional<Integer> optX, Optional<Integer> optY) {
    final Optional<Integer> optSum = apply(Integer::sum, optX, optY);
    System.out.println(optX + " + " + optY + " = " + optSum);
}

static <A, B, C> Optional<C> apply(BiFunction<A, B, C> fAB, Optional<A> optA, Optional<B> optB) {
    return optA.flatMap(a -> optB.map(b -> fAB.apply(a, b)));
}

Since flatMap and map are standard functions for Optional/Maybe (and monad types generally), this approach should work in any other language (though most FP languages will have a more concise solution). E.g. in Haskell:

combine ma mb = do a <- ma ; b <- mb ;  return (a + b)
jon hanson
  • 8,722
  • 2
  • 37
  • 61
1

In F#, i would call this logic reduce.

Reason:

  • The function must be of type 'a -> 'a -> 'a as it only can combine thinks of equal type.

  • Like other reduce operations, like on list, you always need at least one value, otherwise it fails.

With a option and two of them, you just need to cover four cases. In F# it will be written this way.

(* Signature: ('a -> 'a -> 'a) -> option<'a> -> option<'a> -> option<'a> *)
let reduce fn x y =
    match x,y with
    | Some x, Some y -> Some (fn x y)
    | Some x, None   -> Some x
    | None  , Some y -> Some y
    | None  , None   -> None

printfn "%A" (reduce (+) (Some 3) (Some 7)) // Some 10
printfn "%A" (reduce (+) (None)   (Some 7)) // Some 7
printfn "%A" (reduce (+) (Some 3) (None))   // Some 3
printfn "%A" (reduce (+) (None)   (None))   // None

In another lets say Pseudo-like C# language, it would look like.

Option<A> Reduce(Action<A,A,A> fn, Option<A> x, Option<A> y) {
    if ( x.isSome ) {
        if ( y.isSome ) {
            return Option.Some(fn(x.Value, y.Value));
        }
        else {
            return x;
        }
    }
    else {
        if ( y.isSome ) {
            return y;
        }
        else {
            return Option.None;
        }
    }
}
David Raab
  • 4,433
  • 22
  • 40