arronmabrey/looprecur


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

https://github.com/arronmabrey/looprecur

Language: JavaScript


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

Project Statistics

Sourcerank 3
Repository Size 2.93 KB
Stars 1
Forks 0
Watchers 1
Open issues 0
Dependencies 1
Contributors 1
Tags 2
Created
Last updated
Last pushed

Top Contributors See all

Arron Mabrey

Packages Referencing this Repo

looprecur
LoopRecur - simple, safe, recursive JavaScript functions using trampolines.
Latest release 1.0.1 - Updated - 1 stars

Recent Tags See all

v1.0.1 September 12, 2016
v1.0.0 September 12, 2016

Something wrong with this page? Make a suggestion

Last synced: 2016-09-12 21:24:48 UTC

Login to resync this repository