continuations in scala (incomplete version)

Post on 03-Jul-2015

1.797 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

a simple introduction to scala's continuation support, not thorough enough.

TRANSCRIPT

continuations in

fujohnwang@Twitter

@weibo

Saturday, July 9, 2011

what’s continuation?

Continuation capture the state of a computation(maybe to be invoked later).

Simple concept, but hard to get.

first-class support in schema language

Saturday, July 9, 2011

know CPS first

normal control flow structure

control flow structure after cps

PS.  code  sample  borrowed  from  http://www.slideshare.net/openbala/continuations

Saturday, July 9, 2011

WHat’s the big deal?

Saturday, July 9, 2011

usage scenarios

programming design pattern

coroutine

exception handling

web frameworks

backtracking?

Saturday, July 9, 2011

observer pattern with continuation

Saturday, July 9, 2011

Coroutine?

see the blog of jim mcbeatch

Saturday, July 9, 2011

Exception handling

No sample code yet

Saturday, July 9, 2011

Web Framework with continuation support

seaside / smalltalk

cocoon / Java

http://cocoon.apache.org/2.1/userdocs/flow/continuations.html

weblocks / common lisp

wee / ruby

ocsigen / ocaml

more...Saturday, July 9, 2011

Saturday, July 9, 2011

code pieces function calculator(){ var a, b, operator;

cocoon.sendPageAndWait("getA.html"); a = cocoon.request.get("a");

cocoon.sendPageAndWait("getB.html"); b = cocoon.request.get("b");

cocoon.sendPageAndWait("getOperator.html"); operator = cocoon.request.get("op");

try { if (operator == "plus") cocoon.sendPage("result.html", {result: a + b}); else if (operator == "minus") cocoon.sendPage("result.html", {result: a - b}); else if (operator == "multiply") cocoon.sendPage("result.html", {result: a * b}); else if (operator == "divide") cocoon.sendPage("result.html", {result: a / b}); else cocoon.sendPage("invalidOperator.html", {operator: operator}); } catch (exception) { cocoon.sendPage("error.html", {message: "Operation failed: " + exception.toString()}); }}Saturday, July 9, 2011

Continuation types

full continuation(aka. first-class cont.)

delimited continuation (aka. partial, composable cont.)

write async code as sequence one

mix cps code and normal code seamlessly

Saturday, July 9, 2011

What DC looks like?

  def  is123(n:Int):Boolean  =  {         reset  {             shift  {  k  :  (Int=>String)  =>                   (k(n)  ==  "123")             }.toString         }   }

reset  {        println(1)        shift  {  cont  =>  }        println(2)}//prints:  1

reset  {        println(1)        shift  {  cont  =>                println(2)        }        println(3)}//prints:  1  2

reset  {        println(1)        shift  {  cont  =>                cont()                println(2)        }        println(3)}//prints:  1  3  2

Saturday, July 9, 2011

first glance with dcreset and shift

reset sets up the boundary of dc(‘s cps)

shift captures the continuation

Result types

yield

return

Saturday, July 9, 2011

puzzle about yield and return

Saturday, July 9, 2011

scala cont features

compiler plugin + Library

compiler plugin does cps transformation

library supports control structures and directives

serializable (can be saved, distribute sys...)

see swarm?!

Others?

Saturday, July 9, 2011

available softwares

swarm

scalaflow

Akka’s DataFlow Concurrency library

nio actor implementation for akka

nioserver

Saturday, July 9, 2011

What I want to do with it

coroutine

combine with java nio

resource-efficient minimally-threaded networking layer framework

reduce hw cost finally

Saturday, July 9, 2011

Questions?

Saturday, July 9, 2011

Referenceshttp://www.scala-lang.org/node/2096

http://okmij.org/ftp/continuations/Continuations.html

http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html

http://lamp.epfl.ch/~rompf/continuations-icfp09.pdf

http://okmij.org/ftp/continuations/index.html

http://suereth.blogspot.com/2010/03/how-you-should-think-about-delimited.html

http://dcsobral.blogspot.com/2009/07/delimited-continuations-explained-in.html

http://blog.richdougherty.com/2009/02/delimited-continuations-in-scala_24.html

http://en.wikipedia.org/wiki/Seaside_(software)

http://en.wikipedia.org/wiki/Call-with-current-continuation

http://community.schemewiki.org/?call-with-current-continuation-for-C-programmers

http://community.schemewiki.org/?call-with-current-continuation

http://en.wikipedia.org/wiki/Coroutine

http://cocoon.apache.org/2.1/userdocs/flow/continuations.html

https://github.com/rschildmeijer/loft

Saturday, July 9, 2011

Thanks

Saturday, July 9, 2011

top related