1

I have a class which accepts a function f: Option[Seq[Option[A]]] => Option[A]]

case class Function[A](operator: Option[Seq[Option[A]]] => Option[A], name: String, arity: Int)

What i would like is for the user of this class to be able to not have to worry about the Option framework. So the user would pass a function g: Seq[A] => A and under the hood i would lift that function. The lifting part is what i'm not being able to do.

This is what i have (using optionInstance.lift of scalaz) :

object Function {
  def apply[A](operator: Seq[A] => A) = new Function(optionInstance.lift(operator),"a", 1)
}

However this does not compile since the lift operator is returning Option[Seq[A]] instead of Option[Seq[Option[A]]]. How can this be done?

raul ferreira
  • 886
  • 7
  • 21
  • 1
    We don't how you want to do the mapping. Let's say you have the operator Seq[A] => A, how would handle the input in the lifted Seq(Some(a), None)) ? I can't think of a canonical way. – C4stor Jan 06 '17 at 13:56
  • if `Seq(Some(a), None))` would evaluate to `None` as a proper `Option[Seq[_]]` would have to behave right? – raul ferreira Jan 06 '17 at 14:09
  • basically the user would provide `Seq[A] => A` and under the hood i would lift it to `Option[Seq[Option[A]]] => Option[A]`. with `Seq(Some(a), None))` evaluating to `None` – raul ferreira Jan 06 '17 at 14:13

2 Answers2

1

Here's an example of how to convert between your two prototypes :

  def f[A]: Seq[A] => A = ???

  def g[A]: Option[Seq[Option[A]]] => Option[A] = {
    {
      case None => None
      case Some(seq) if seq.exists(_.isEmpty) => None
      case Some(seq) => Some(f(seq.flatten))
    }
  }

You can use this to transform your operator

C4stor
  • 8,355
  • 6
  • 29
  • 47
1

If you would like to return Option[Seq[Option[A]]] you have to first transform your Seq[A] into a Seq[Option[A]] before doing the lift

Othman Doghri
  • 69
  • 1
  • 5