GithubHelp home page GithubHelp logo

What's the resume function? about koka HOT 4 CLOSED

koka-lang avatar koka-lang commented on May 18, 2024
What's the resume function?

from koka.

Comments (4)

Pauan avatar Pauan commented on May 18, 2024 1

@zaoqi-unsafe The resume function is like a callback in JavaScript.

Let's say you are using some Node.js APIs in JavaScript:

function foo(cb) {
  fs.readFile("foo", function (err, data) {
    fs.writeFile("bar", data, cb);
  });
}

When the readFile function is finished, it will call the callback with the file contents.

Now consider this Koka code:

fun foo(): asyncx () {
  val data = read-file("foo")
  write-file("bar", data)
}

The Koka compiler will translate the above code into this JavaScript:

function read_file(path, resume) {
  // Do some stuff then call resume
}

function write_file(path, data, resume) {
  // Do some stuff then call resume
}

function foo(resume) {
  read_file("foo", function (data) {
    write_file("bar", data, resume);
  });
}

In other words, Koka automatically added in the callbacks for you.

When you write an effect handler, resume is the callback.

So if read-file never calls resume, then the read-file function will never return.

And if read-file does call resume, then the first argument to resume is the return value for read-file

from koka.

Pauan avatar Pauan commented on May 18, 2024 1

@zaoqi-unsafe Handlers can have extra state (in this case i). When you call resume, the first argument is the new state, and the second argument is the return value.

So in this case i is the current state, and resume(i,i) keeps the current state, and resume(j,()) sets the state to j

from koka.

zaoqi-unsafe avatar zaoqi-unsafe commented on May 18, 2024

@Pauan

val state = handler(i) {
  return x -> x
  get()    -> resume(i,i)
  set(j)   -> resume(j,())
}

from koka.

b-studios avatar b-studios commented on May 18, 2024

Thanks for answering @Pauan -- I'll go ahead and close the issue.

from koka.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.