Skip to content

LoopRecur - simple, safe, recursive JavaScript functions using trampolines.

Notifications You must be signed in to change notification settings

arronmabrey/looprecur

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LoopRecur - simple, safe, recursive JavaScript functions using trampolines.

LoopRecur is small higher-order function that can be used to help simplify writing safe recursive JavaScript functions.

It borrows its style from loop/recur found in Clojure. Although its implementation is very different, as it uses functional trampolines to avoid growing stack frames.

The main idea is you call loop providing a recursive function, loop returns your function wrapped in a trampoline.

Later when your wrapped function is called, the special recur function is inserted as the first argument. You simply call recur with updated arguments for the next iteration and return the result. The trampoline will execute your function again with an updated arguments, this continues until your function returns a value that is not a function.

import loop from "looprecur"
// var loop = require("looprecur").default

const inc = loop((recur, x, n) => {
    if (x == n) {
        return x
    } else {
        return recur(x   1, n)
    }
})

inc(0, 1000000) == 1000000

About

LoopRecur - simple, safe, recursive JavaScript functions using trampolines.

Resources

Stars

Watchers

Forks

Packages

No packages published