Skip to content

Instantly share code, notes, and snippets.

@ashnur ashnur/takeWhile.js
Created Jun 13, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.