fork download
  1. import scala.collection.mutable.ArrayBuffer
  2.  
  3. object Main extends App {
  4. sealed trait Result[+R, +E] {
  5. import Result._
  6. def flatMap[R1, E1 >: E](f: R => Result[R1, E1]): Result[R1, E1] = this match {
  7. case Err(e) => Err(e)
  8. case Ok(r) => f(r)
  9. }
  10. def map[R1](f: R => R1) = flatMap(r => Ok(f(r)))
  11. }
  12. object Result {
  13. final case class Ok[R](r: R) extends Result[R, Nothing]
  14. final case class Err[E](e: E) extends Result[Nothing, E]
  15. }
  16.  
  17. sealed trait CliError
  18. object CliError {
  19. final case class Io(err: String) extends CliError
  20. final case class Parse(err: String) extends CliError
  21. }
  22.  
  23. trait File {
  24. def read_to_string(ab: ArrayBuffer[Char]): Result[Unit, CliError.Io] = ???
  25. }
  26. object File {
  27. def open(s: String): Result[File, CliError.Io] = ???
  28. }
  29.  
  30. def parse(ab: ArrayBuffer[Char]): Result[Int, CliError.Parse] = ???
  31.  
  32. def file_double(file_path: String): Result[Int, CliError] =
  33. for {
  34. file <- File.open(file_path)
  35. contents = ArrayBuffer[Char]()
  36. _ <- file.read_to_string(contents)
  37. n <- parse(contents)
  38. } yield 2 * n
  39.  
  40. println(file_double("x"))
  41. }
Runtime error #stdin #stdout #stderr 0.38s 322432KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" scala.NotImplementedError: an implementation is missing
	at scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)
	at Main$File$.open(Main.scala:27)
	at Main$.file_double(Main.scala:34)
	at Main$.delayedEndpoint$Main$1(Main.scala:40)
	at Main$delayedInit$body.apply(Main.scala:3)
	at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
	at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
	at scala.App$$anonfun$main$1.apply(App.scala:76)
	at scala.App$$anonfun$main$1.apply(App.scala:76)
	at scala.collection.immutable.List.foreach(List.scala:381)
	at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
	at scala.App$class.main(App.scala:76)
	at Main$.main(Main.scala:3)
	at Main.main(Main.scala)