Skip to content

Instantly share code, notes, and snippets.

@ashnur
Created June 13, 2015 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashnur/3c291781d3e40ef9d557 to your computer and use it in GitHub Desktop.
Save ashnur/3c291781d3e40ef9d557 to your computer and use it in GitHub Desktop.
var iter = (f) => (x) => [x, () => iter(f)(f(x))]
var map = (f) => (a) => [f(a[0]), () => map(f)(a[1]())]
var takeWhile = (f) => (a) => f(a[0])? [a[0]].concat(takeWhile(f)(a[1]())) : []
var x = 100
var xs = iter(a=>~~(a/2))(x)
var ys = map(a=>x-a)(xs);
takeWhile(a=>a<x)([0,()=>ys])
@ashnur
Copy link
Author

ashnur commented Jun 15, 2015

var x = 100
var xp = 0
var rs = []
var i = x
while ( xp < x ) {
  rs.push(xp)
  i = ~~(i/2)
  xp = x - i
}
console.log('loopy', rs)

@ashnur
Copy link
Author

ashnur commented Jun 15, 2015

var x = 100
var xp = 0
var rs = []
var i = x
function halfs(x, i, xp, rs){
  if ( xp < x ) {
    i = ~~(i/2)
    return halfs(x, i, x - i, rs.concat(xp))
  } else {
    return rs
  }
}
console.log('recursive', halfs(x, x, 0, rs))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment