hamzaremmal/amy

View on GitHub
library/Option.amy

Summary

Maintainability
Test Coverage
module O
  abstract class Option
  case class None() : Option
  case class Some(v: Int) : Option

  fn isDefined(o: Option): Boolean = {
    o match {
      case None() => false
      case _ => true
    }
  }

  fn get(o: Option): Int = {
    o match {
      case Some(i) => i
      case None() => error("get(None)")
    }
  }

  fn getOrElse(o: Option, i: Int): Int = {
    o match {
      case None() => i
      case Some(oo) => oo
    }
  }

  fn orElse(o1: Option, o2: Option): Option = {
    o1 match {
      case Some(_) => o1
      case None() => o2
    }
  }

  fn toList(o: Option): L.List = {
    o match {
      case Some(i) => L.Cons(i, L.Nil())
      case None() => L.Nil()
    }
  }
end O