Skip to content

Commit

Permalink
Merge pull request #1 from EECOLOR/simplifications
Browse files Browse the repository at this point in the history
Simplified usage by making the `Return` implicit.
  • Loading branch information
earldouglas committed Mar 13, 2014
2 parents a2f1275 + d42e007 commit 88b7599
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -19,14 +19,14 @@ case class Bar(x: String)

object SimpleProgram {

import com.versal.jellyfish.{program, Program, read, Return}
import com.versal.jellyfish.{program, read}

// create a program with some dependencies
val simpleProgram: Program =
val simpleProgram =
program {
val bar: Bar = read[Bar] // retrieve the `Bar` dependency
val foo: Foo = read[Foo] // retrieve the `Foo` dependency
Return("foo is " + foo.x + ", bar is " + bar.x)
"foo is " + foo.x + ", bar is " + bar.x
}

}
Expand Down Expand Up @@ -76,7 +76,7 @@ Ignoring some of the wrappers, this:
```scala
val bar: Bar = read[Bar] // retrieve the `Bar` dependency
val foo: Foo = read[Foo] // retrieve the `Foo` dependency
Return("foo is " + foo.x + ", bar is " + bar.x)
"foo is " + foo.x + ", bar is " + bar.x
```

becomes:
Expand All @@ -100,4 +100,4 @@ bar: Bar => {

which is a curried function with two dependencies.

An interpreter is then built to unwrap each nested `With`, extract the function of type `A => Program`, provide the appropriate instance of `A`, and continue until the program completes with a `Return`.
An interpreter is then built to unwrap each nested `With`, extract the function of type `A => Program`, provide the appropriate instance of `A`, and continue until the program completes with a `Return`.
2 changes: 2 additions & 0 deletions build.sbt
Expand Up @@ -16,5 +16,7 @@ scalacOptions += "-P:continuations:enable"

scalacOptions += "-deprecation"

scalacOptions += "-feature"

libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test"

23 changes: 13 additions & 10 deletions src/main/scala/jellyfish.scala
@@ -1,30 +1,33 @@
package com.versal.jellyfish
package com.versal

object `package` {
package object jellyfish {

import scala.util.continuations.cpsParam
import scala.util.continuations.cps
import scala.util.continuations.shift
import scala.util.continuations.reset
import scala.language.implicitConversions

type program = cpsParam[Program, Program]
type program = cps[Program]

def program(ctx: => Program @program): Program = reset[Program, Program](ctx)
def program[A](ctx: => A @program)(implicit ev: A => Program): Program =
reset(ev(ctx))

sealed trait Program
object Program {
implicit def toReturn(a: Any): Program = Return(a)
}
case class Return(a: Any) extends Program
case class With[A](c: Class[A], f: A => Program) extends Program

def read[A](implicit mx: Manifest[A]): A @program = shift { k: (A => Program) =>
With(mx.erasure.asInstanceOf[Class[A]], k)
With(mx.runtimeClass.asInstanceOf[Class[A]], k)
}

implicit def classy(x: Class[_]): Classy = new Classy(x)

}

class Classy(x: Class[_]) {
def isA[A](implicit m: Manifest[A]): Boolean = {
x.isAssignableFrom(m.erasure)
}
def isA[A](implicit m: Manifest[A]): Boolean =
x.isAssignableFrom(m.runtimeClass)
}

2 changes: 1 addition & 1 deletion src/test/scala/tests.scala
Expand Up @@ -12,7 +12,7 @@ object SimpleProgram {
def simpleProgram = program {
val bar: Bar = read[Bar]
val foo: Foo = read[Foo]
Return("foo is " + foo.x + ", bar is " + bar.x)
"foo is " + foo.x + ", bar is " + bar.x
}

}
Expand Down

0 comments on commit 88b7599

Please sign in to comment.