continuation-passing

[a,b].reduce(f,x) code to [a,b].reduce(f) using transducer /CPS based functional references?

北战南征 提交于 2019-12-02 05:11:27
In my previous Quesion: Extracting data from a function chain without arrays @Aadit M Shah gave me astonishing solution as follows: https://stackoverflow.com/a/51420884/6440264 Given an expression like A(a)(b)(f) where f is a function, it's impossible to know whether f is supposed to be added to the list or whether it's the reducing function. Hence, I'm going to describe how to write expressions like A(a)(b)(f, x) which is equivalent to [a, b].reduce(f, x) . This allows us to distinguish when the list ends depending upon how many arguments you provide: const L = g => function (x, a) { switch

How to adapt trampolines to Continuation Passing Style?

亡梦爱人 提交于 2019-12-01 18:17:12
Here is a naive implementation of a right fold: const foldr = f => acc => ([x, ...xs]) => x === undefined ? acc : f(x) (foldkr(f) (acc) (xs)); This is non-tail recursion and hence we cannot apply a trampoline. One approach would be to make the algorithm iterative and use a stack to mimick the function call stack. Another approch would be to transform the recursion into CPS: const Cont = k => ({runCont: k}); const foldkr = f => acc => ([x, ...xs]) => Cont(k => x === undefined ? k(acc) : foldkr(f) (acc) (xs) .runCont(acc_ => k(f(x) (acc_)))); This is still naive, because it is insanely slow.

How to adapt trampolines to Continuation Passing Style?

有些话、适合烂在心里 提交于 2019-12-01 17:34:09
问题 Here is a naive implementation of a right fold: const foldr = f => acc => ([x, ...xs]) => x === undefined ? acc : f(x) (foldkr(f) (acc) (xs)); This is non-tail recursion and hence we cannot apply a trampoline. One approach would be to make the algorithm iterative and use a stack to mimick the function call stack. Another approch would be to transform the recursion into CPS: const Cont = k => ({runCont: k}); const foldkr = f => acc => ([x, ...xs]) => Cont(k => x === undefined ? k(acc) : foldkr

Rewriting code with continuations

依然范特西╮ 提交于 2019-11-30 21:00:06
I have some code that evaluates primitive programs. Program is a list of statements (expression, block, return statement). Result of evaluation is last evaluated expression. Also evaluator should properly treat return statement (i.e. stop evaluation after first occurrence of return ). To implement this logic I pass special callback function ( NextStep ) which make next evaluating step after current statement. I don't call next step when handling return statement: data Statement = Expr Int | Block [Statement] | Return Int deriving (Show, Eq) data Value = Undefined | Value Int deriving (Show, Eq

The Little Schemer evens-only*&co

眉间皱痕 提交于 2019-11-30 06:51:27
I'm having difficulty understanding what's going on with The Little Schemer's evens-only*&co example on page 145. Here's the code: (define evens-only*&co (lambda (l col) (cond ((null? l) (col '() 1 0)) ((atom? (car l)) (cond ((even? (car l)) (evens-only*&co (cdr l) (lambda (newl product sum) (col (cons (car l) newl) (opx (car l) product) sum)))) (else (evens-only*&co (cdr l) (lambda (newl product sum) (col newl product (op+ (car l) sum))))))) (else (evens-only*&co (car l) (lambda (newl product sum) (evens-only*&co (cdr l) (lambda (dnewl dproduct dsum) (col (cons newl dnewl) (opx product

continuation in common lisp by macros — regarding an implemetation in OnLisp

穿精又带淫゛_ 提交于 2019-11-30 05:28:52
In On Lisp , p. 267, Paul Graham provides an implementation of continuation passing macros: (setq *cont* #'identity) (defmacro =lambda (parms &body body) `#'(lambda (*cont* ,@parms) ,@body)) (defmacro =defun (name parms &body body) (let ((f (intern (concatenate 'string "=" (symbol-name name))))) `(progn (defmacro ,name ,parms `(,',f *cont* ,,@parms)) (defun ,f (*cont* ,@parms) ,@body)))) (defmacro =bind (parms expr &body body) `(let ((*cont* #'(lambda ,parms ,@body))) ,expr)) (defmacro =values (&rest retvals) `(funcall *cont* ,@retvals)) The following code to traverse a tree t2 for each leaf

Rewriting code with continuations

萝らか妹 提交于 2019-11-29 21:08:10
问题 I have some code that evaluates primitive programs. Program is a list of statements (expression, block, return statement). Result of evaluation is last evaluated expression. Also evaluator should properly treat return statement (i.e. stop evaluation after first occurrence of return ). To implement this logic I pass special callback function ( NextStep ) which make next evaluating step after current statement. I don't call next step when handling return statement: data Statement = Expr Int |

continuation in common lisp by macros — regarding an implemetation in OnLisp

最后都变了- 提交于 2019-11-29 04:09:42
问题 In On Lisp, p. 267, Paul Graham provides an implementation of continuation passing macros: (setq *cont* #'identity) (defmacro =lambda (parms &body body) `#'(lambda (*cont* ,@parms) ,@body)) (defmacro =defun (name parms &body body) (let ((f (intern (concatenate 'string "=" (symbol-name name))))) `(progn (defmacro ,name ,parms `(,',f *cont* ,,@parms)) (defun ,f (*cont* ,@parms) ,@body)))) (defmacro =bind (parms expr &body body) `(let ((*cont* #'(lambda ,parms ,@body))) ,expr)) (defmacro =values

Continuations and for comprehensions — what's the incompatibility?

我是研究僧i 提交于 2019-11-29 04:01:32
I am new to Scala and trying to wrap my head around continuations I'm trying to reproduce the yield return C# statement. Following this post , I have written the following code : package com.company.scalatest import scala.util.continuations._; object GenTest { val gen = new Generator[Int] { def produce = { yieldValue(1) yieldValue(2) yieldValue(3) yieldValue(42) } } // Does not compile :( // val gen2 = new Generator[Int] { // def produce = { // var ints = List(1, 2, 3, 42); // // ints.foreach((theInt) => yieldValue(theInt)); // } // } // But this works? val gen3 = new Generator[Int] { def

How to implement a stack-safe chainRec operator for the continuation monad?

梦想与她 提交于 2019-11-28 14:09:37
I am currently experimenting with the continuation monad. Cont is actually useful in Javascript, because it abstracts from the callback pattern. When we deal with monadic recursion, there is always the risk of a stack overflow, because the recursive call isn't in tail position: const chain = g => f => k => g(x => f(x) (k)); const of = x => k => k(x); const id = x => x; const inc = x => x + 1; const repeat = n => f => x => n === 0 ? of(x) : chain(of(f(x))) (repeat(n - 1) (f)); console.log( repeat(1e6) (inc) (0) (id) // stack overflow ); However, even if we are able to transform some cases into