Here's an interesting quandary using Java's `Optional.flatMap()`. I have a class that encapsulates a [CURIE](https://www.w3.org/TR/curie/) in the form `"foo:bar"` or just `"bar"`. I also have a class for registering prefixes to namespaces, as well as for keeping track of the default namespace. It has a method `findNamespaceByPrefix()` returning an `Optional<URI>` (as the prefix may not be registered) and a method `getDefaultNamespace()`, also returning an `Optional<URI>` (as there may not be a default namespace).

Given some CURIE I want to find the namespace. It's tempting to use this:

```java
Optional<URI> namespace = curie.getPrefix()
    .flatMap(this::findNamespaceByPrefix)
    .or(this::getDefaultNamespace);

```

But that is wrong. The problem is that it returns the default namespace, even if there is a prefix but no namespace was associated with that prefix. We only want to get back the default namespace (if any) if there is no prefix indicated at all in the CURIE.

Here is a solution:

```java
Optional<String> prefix = curie.getPrefix();
Optional<URI> namespace = prefix.isPresent()
    ? prefix.flatMap(this::findNamespaceByPrefix)
    : getDefaultNamespace();

```

And I'm fine with that, but it strikes me as somewhat non-functional with its use of the ternary operator, and using `Optional.isPresent()` usually indicates there is a better way to do things.

Do any `Optional` experts have a better, more functional solution to "short-circuit" the flat mapping? (I have a hunch I could do something with several layers of wrapping and unwrapping `Optional` using `Optional:of` and `Optional:get` in the pipeline, but is there something perhaps more elegant?)