- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
thanks
Wed, 2011-03-16, 00:18
Hi,
just wanted to say thanks
this group rocks:
a few days ago I did not know about
how Scala implemented delimited continuations
and now I have cool code that I can present next week
at the Brussels University !!!
every monad (having a flatMap method)
can be given a method:
def reflect[Y]: X @cps[C[Y]] = {
shift {
flatMap((_: X => C[Y]))
}
and it's surrounding
monad module (having a _return method)
can be given a method:
def _cartProd[X, Y]
(left: Monad[X], right: Monad[Y]) =
reset {
_return {
(left.reflect[(X, Y)],
right.reflect[(X, Y)])
}
}
and then (using my ListMonad monad instance) I can test
val cartProdTest: C[(Int, String)] =
_cartProd(
ListMonad[Int](1 :: 2 :: 3 :: Nil),
ListMonad[String]("a" :: "b" :: "c" :: Nil)
)
println {
cartProdTest.run(())
}
and run
$ run.sh UsingMonads
List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
cool!
thx
Luc
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
just wanted to say thanks
this group rocks:
a few days ago I did not know about
how Scala implemented delimited continuations
and now I have cool code that I can present next week
at the Brussels University !!!
every monad (having a flatMap method)
can be given a method:
def reflect[Y]: X @cps[C[Y]] = {
shift {
flatMap((_: X => C[Y]))
}
and it's surrounding
monad module (having a _return method)
can be given a method:
def _cartProd[X, Y]
(left: Monad[X], right: Monad[Y]) =
reset {
_return {
(left.reflect[(X, Y)],
right.reflect[(X, Y)])
}
}
and then (using my ListMonad monad instance) I can test
val cartProdTest: C[(Int, String)] =
_cartProd(
ListMonad[Int](1 :: 2 :: 3 :: Nil),
ListMonad[String]("a" :: "b" :: "c" :: Nil)
)
println {
cartProdTest.run(())
}
and run
$ run.sh UsingMonads
List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
cool!
thx
Luc
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination