-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid): WIP: rewrite grid things to use iterables and transducers
Transducers combined with iterables (generators mainly) are very efficient in terms of performance. Also, they simplified the Grid class a lot. This huge commit lays the foundation to rewrite the traversers that already exist and add more.
- Loading branch information
1 parent
c7a75c0
commit 3c97979
Showing
27 changed files
with
500 additions
and
391 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,15 @@ | ||
import { Hex } from '../../hex' | ||
import { HexGenerator, Traverser } from '../types' | ||
|
||
export function concat<T extends Hex>(...traversers: Traverser<T>[]): Traverser<T, HexGenerator<T>> { | ||
return function* concatTraverser(createHex, cursor) { | ||
let _cursor = cursor | ||
for (const traverser of traversers) { | ||
// don't use `_cursor = yield* traverser(createHex, _cursor)` doesn't work if `traverser()` returns an array, | ||
// because arrays always return `{ value: undefined, done: true }` when iterated | ||
for (const hex of traverser(createHex, _cursor)) { | ||
yield (_cursor = hex) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
export * from './concat' | ||
export * from './distance' | ||
export * from './flatTraverse' | ||
export * from './inStore' | ||
export * from './neighborOf' | ||
export * from './repeat' |
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,24 @@ | ||
import { Hex } from '../../hex' | ||
import { HexGenerator, Traverser } from '../types' | ||
|
||
export function repeat<T extends Hex>( | ||
times: number, | ||
traverser: Traverser<T>, | ||
{ max = 100 } = {}, | ||
): Traverser<T, HexGenerator<T>> { | ||
// todo: generalize this in all traversers that can potentially loop infinitely | ||
if (times > max) { | ||
console.warn(`Traverser created that outputs more hexes (${times}) than configured. Limiting iteration to ${max}.`) | ||
} | ||
|
||
const _times = Math.min(times, max) | ||
|
||
return function* repeatTraverser(createHex, cursor) { | ||
let _cursor = cursor | ||
for (let i = 0; i < _times; i++) { | ||
for (const hex of traverser(createHex, _cursor)) { | ||
yield (_cursor = hex) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.