Skip to content

Instantly share code, notes, and snippets.

@kesava
Last active January 15, 2019 23:55
Show Gist options
  • Save kesava/470666154d2b1dbc3fceb2007f49d495 to your computer and use it in GitHub Desktop.
Save kesava/470666154d2b1dbc3fceb2007f49d495 to your computer and use it in GitHub Desktop.
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