Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Church-encoded Maybe #3

@ktonga

Description

@ktonga

@jdegoes you in scalaz/scalaz#1798 specify that replacements for Maybe and \/ should be church-encoded. Why exactly? as opposed to sum types.

So far I've got this, but dunno how to get rid of B type parameter, it should be using some sort of Haskell's forall but I can't figure out how to express that in Scala.

final class Perhaps[A, B] private (val perhaps: (B, A => B) => B) extends AnyVal {
  def map[C](g: A => C): Perhaps[C, B] = new Perhaps((b, f) => perhaps(b, f.compose(g)))
  def toOption: Option[A] = perhaps(None.asInstanceOf[B], a => Some(a).asInstanceOf[B]).asInstanceOf[Option[A]]
}

object Perhaps {
  def present[A, B](a: A): Perhaps[A, B] = new Perhaps((_, f) => f(a))
  def absent[A, B]: Perhaps[A, B] = new Perhaps((b, _) => b)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions