Giter VIP home page Giter VIP logo

js-tail-call's Introduction

tail-call

Build Status

Unfortunately does not has tail call optimization (a.k.a tail recursion), technique often used in functional languages. Recursions usually are way more expressive than imperative loops for iterative calculations and they helps avoid side-effects.

Unfortunately though lack of tail call optimization in JS makes it impossible to perform significant calculations in recursive style. Here is example demonstrating stack overflow:

var sum = function(x, y) {
  return y > 0 ? sum(x + 1, y - 1) :
         y < 0 ? sum(x - 1, y + 1) :
         x
}

sum(20, 100000) // => RangeError: Maximum call stack size exceeded

This module is provides a solution to overcome this limitation through trampolining. It's more or less a polifill for proper tail calls coming in ES.next. API is simple, it just requires wrapping of tali recursive function:

var recur = require('tail-call').recur
var sum = recur(function(x, y) {
  return y > 0 ? sum(x + 1, y - 1) :
         y < 0 ? sum(x - 1, y + 1) :
         x
})

sum(20, 100000) // => 100020

Be aware though that this will be way slower than imperative loops. There are even numbers from @jdalton.

Finally keep in mind that recur returns function that is not side-effect free and there for should be used with a great care. More precisly it should not be called from anywhere else other then itself during tail recursion. For example following code will misbehave:

var foo = recur(function(x, y) {
  // ....
  bar(x, y)
  // ...
})

var bar = function(x, y) {
  // ...
  foo(x + 1, y - 1)
  // ...
}

Don't worry, bar still can call foo but it in slightly different manner:

function f(x, y) {
  // ....
  bar(x, y)
  // ...
}

var foo = recur(f)

var bar = function(x, y) {
  // ...
  recur(f)(x + 1, y - 1)
  // ...
}

Install

npm install tail-call

js-tail-call's People

Contributors

gozala avatar

Stargazers

Steven Eberling avatar Ryan avatar Joomy Korkut avatar Joachim Wester avatar Logan Powell avatar Nick Deis avatar Doniyor Aliyev avatar Matt Morris avatar J.C. avatar Buckaroo Cheung avatar Mikey avatar  avatar Angus H. avatar clojj avatar Brad Pillow avatar Matt Mueller avatar Homam Hosseini avatar J. Ryan Stinnett avatar Li Xinqi avatar Sean Micklethwaite avatar Diogo Ribeiro avatar Matthew Elder avatar Mohammad Sadegh Khoeini avatar Maurice Rabb avatar Celile Fulwood avatar Mikko Koski avatar Jean-Pierre Pommet avatar Tony Pujals avatar Paul Grenier avatar Demyan Rogozhin avatar xkxx avatar  avatar Adam Crabtree avatar Sunny Gleason avatar  avatar  avatar James Long avatar

Watchers

 avatar Demyan Rogozhin avatar James Cloos avatar  avatar

js-tail-call's Issues

npm package problem?

After doing npm install tail-call, I found that I had to require('./node_modules/tail-call/core.js') to access the recur function. I suspect that your package.json may not be correct, but I don't really know how npm works.

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.