-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(seq): add lazyseq() & cons(), tests, update readme
- Loading branch information
1 parent
b7b4724
commit d25584e
Showing
7 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ISeq } from "@thi.ng/api"; | ||
|
||
/** | ||
* Returns a new `ISeq` with `head` prepended to (optional) `seq`. | ||
* | ||
* @param head | ||
* @param seq | ||
*/ | ||
export const cons = <T>(head: T, seq?: ISeq<T>): ISeq<T> => ({ | ||
first() { | ||
return head; | ||
}, | ||
next() { | ||
return seq; | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from "./array"; | ||
export * from "./concat"; | ||
export * from "./cons"; | ||
export * from "./ensure"; | ||
export * from "./iterator"; | ||
export * from "./lazyseq"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Fn0, ISeq } from "@thi.ng/api"; | ||
|
||
/** | ||
* Returns a new lazily evaluated `ISeq` produced by given function | ||
* `fn`, which is only realized when values are requested. The function | ||
* is only called once (if at all) and its result cached. | ||
* | ||
* @example | ||
* ```ts | ||
* const rnd = () => lazyseq(() => cons(Math.random(), rnd())); | ||
* const a = rnd(); | ||
* | ||
* a.first(); | ||
* // 0.4421468479982633 | ||
* a.next().first(); | ||
* // 0.29578903713266524 | ||
* ``` | ||
* | ||
* @param fn | ||
*/ | ||
export const lazyseq = <T>(fn: Fn0<ISeq<T> | undefined>): ISeq<T> => { | ||
let called = false; | ||
let seq: ISeq<T> | undefined; | ||
const ensure = () => { | ||
if (!called) { | ||
seq = fn(); | ||
called = true; | ||
} | ||
return seq; | ||
}; | ||
return { | ||
first() { | ||
return ensure() !== undefined ? seq!.first() : undefined; | ||
}, | ||
next() { | ||
return ensure() !== undefined ? seq!.next() : undefined; | ||
} | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as assert from "assert"; | ||
import { cons } from "../src"; | ||
|
||
describe("cons", () => { | ||
it("cons", () => { | ||
assert.equal(cons(1).first(), 1); | ||
assert.equal(cons(1).next(), undefined); | ||
assert.equal( | ||
cons(1, cons(2)) | ||
.next()! | ||
.first(), | ||
2 | ||
); | ||
assert.equal( | ||
cons(1, cons(2)) | ||
.next()! | ||
.next(), | ||
undefined | ||
); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ISeq } from "@thi.ng/api"; | ||
import * as assert from "assert"; | ||
import { cons, lazyseq } from "../src"; | ||
|
||
describe("lazyseq", () => { | ||
it("lazyseq", () => { | ||
const fib = (a?: number, b?: number): ISeq<number> => | ||
a !== undefined | ||
? lazyseq(() => cons(a, fib(b, a + b!))) | ||
: fib(0, 1); | ||
assert.equal(fib().first(), 0); | ||
// prettier-ignore | ||
assert.equal(fib().next()!.first(), 1); | ||
// prettier-ignore | ||
assert.equal(fib().next()!.next()!.first(), 1); | ||
// prettier-ignore | ||
assert.equal(fib().next()!.next()!.next()!.first(), 2); | ||
// prettier-ignore | ||
assert.equal(fib().next()!.next()!.next()!.next()!.first(), 3); | ||
}); | ||
}); |