Last active
January 15, 2019 23:55
-
-
Save kesava/470666154d2b1dbc3fceb2007f49d495 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LazySeq { | |
constructor(low, high) { | |
this.low = low; | |
this.high = high; | |
} | |
head() { | |
return this.low; | |
} | |
tail() { | |
return new LazySeq(this.low + 1, this.high); | |
} | |
isEmpty() { | |
return (this.low > this.high); | |
} | |
} | |
const seqToList = (seq, acc=[]) => { | |
if (seq.isEmpty()) { | |
return acc; | |
} else { | |
return seqToList(seq.tail(), acc.concat([seq.head()])); | |
} | |
} | |
const take = (seq, len, acc=[]) => { | |
if (seq.isEmpty() || len <= 0) { | |
return acc; | |
} else { | |
return take(seq.tail(), len - 1, acc.concat([seq.head()])); | |
} | |
} | |
console.log(seqToList(new LazySeq(5, 9))); // [ 5, 6, 7, 8, 9 ] | |
console.log(take(new LazySeq(5, 9), 2)); // [5, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment