Question: I have a function in a context, (in a Maybe / Option) and I want to pass it a value and get back the return value, directly out of the context.

Let's take an example in Scala :

```
scala> Some((x:Int) => x * x)
res0: Some[Int => Int] = Some(<function1>)

```

Of course, I can do

```
res0.map(_(5))

```

to execute the function, but the result is wrapped in the context.

Ok, I could do :

```
res0.map(_(5)).getOrElse(...)

```

but I'm copy/pasting this everywhere in my code (I have a lot of functions wrapped in Option, or worst, in Either...).

I need a better form, something like :

```
res0.applyOrElse(5, ...)

```

Does this concept of 'applying a function in a concept to a value and immediatly returning the result out of the context' exists in FP with a specific name (I'm lost in all those Functor, Monad and Applicatives...) ?

Answer: 
You can use `andThen` to move the default from the place where you call the function to the place where you define it:

```
val foo: String => Option[Int] = s => Some(s.size)
val bar: String => Int = foo.andThen(_.getOrElse(100))

```

This only works for `Function1`, but if you want a more generic version, Scalaz provides functor instances for `FunctionN`:

```
import scalaz._, Scalaz._

val foo: (String, Int) => Option[Int] = (s, i) => Some(s.size + i)
val bar: (String, Int) => Int = foo.map(_.getOrElse(100))

```

This also works for `Function1`—just replace `andThen` above with `map`.

More generally, as I mention above, this looks a little like `unliftId` on `Kleisli`, which takes a wrapped function `A => F[B]` and collapses the `F` using a comonad instance for `F`. If you wanted something that worked generically for `Option`, `Either[E, ?]`, etc., you could write something similar that would take a `Optional` instance for `F` and a default value.
You could write something like `applyOrElse` using `Option.fold`.

> 
> fold[B](ifEmpty: ⇒ B)(f: (A) ⇒ B): B 
> 
> 
> 

```
val squared = Some((x:Int) => x * x)
squared.fold { 
  // or else = ifEmpty
  math.pow(5, 2).toInt
}{
  // execute function
  _(5)
}

```

---

Using Travis Browns recent [answer](https://stackoverflow.com/questions/31481836/scala-function-tupled-and-function-untupled-equivalent-for-variable-arity-or-c/31485711#31485711) on another question, I was able to puzzle together the following `applyOrElse` function. It depends on Shapeless and you need to pass the arguments as an `HList` so it might not be exactly what you want.

```
def applyOrElse[F, I <: HList, O](
  optionFun: Option[F],
  input: I,
  orElse: => O
)(implicit
  ftp: FnToProduct.Aux[F, I => O]
): O = optionFun.fold(orElse)(f => ftp(f)(input))

```

Which can be used as :

```
val squared = Some((x:Int) => x * x)
applyOrElse(squared, 2 :: HNil, 10)
// res0: Int = 4

applyOrElse(None, 2 :: HNil, 10)
// res1: Int = 10

val concat = Some((a: String, b: String) => s"$a $b")
applyOrElse(concat, "hello" :: "world" :: HNil, "not" + "executed")
// res2: String = hello world

```