Skip to content

Commit

Permalink
feat(grid/traversers): add repeatWith traverser
Browse files Browse the repository at this point in the history
It's very similar to branch() but has the option to omit the hexes from the source traverser. Also,
the name is a little better (hopefully)
  • Loading branch information
flauwekeul committed Jul 23, 2022
1 parent d7bb59b commit 8e400af
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/grid/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './concat'
export * from './distance'
export * from './neighborOf'
export * from './repeat'
export * from './repeatWith'
25 changes: 25 additions & 0 deletions src/grid/functions/repeatWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Hex } from '../../hex'
import { Traverser } from '../types'
import { concat } from './concat'

// todo: use in rays (if it still has right to exist)
// todo: probably move repeatWith, repeat and concat to grid/traversers/
export function repeatWith<T extends Hex>(
sources: Traverser<T> | Traverser<T>[],
targets: Traverser<T> | Traverser<T>[],
// todo: isn't there a more elegant way than a config?
{ includeSource = true } = {},
): Traverser<T, T[]> {
return function repeatWithTraverser(createHex, cursor) {
const hexes: T[] = []

for (const sourceCursor of concat(sources)(createHex, cursor)) {
if (includeSource) hexes.push(sourceCursor)
for (const hex of concat(targets)(createHex, sourceCursor)) {
hexes.push(hex)
}
}

return hexes
}
}
3 changes: 2 additions & 1 deletion src/grid/traversers/rays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { line } from './line'
import { ring } from './ring'

// todo:
// - remove this file? If not: refactor to use repeatWith()?
// - add option for arc in degrees?
// - add to docs that duplicate hexes are returned (or make this configurable? Or add transformer that dedupes?)
// - add to docs that duplicate hexes are returned (or make this configurable? Or add transducer that dedupes?)
export function rays<T extends Hex>(options: RaysWithLengthOptions): Traverser<T, T[]>
export function rays<T extends Hex>(options: RaysToHexOptions): Traverser<T, T[]>
export function rays<T extends Hex>({
Expand Down
4 changes: 2 additions & 2 deletions src/grid/traversers/rectangle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Compass, CompassDirection } from '../../compass'
import { Hex, HexCoordinates, hexToOffset, OffsetCoordinates } from '../../hex'
import { isOffset, isTuple, tupleToCube } from '../../utils'
import { branch } from '../functions'
import { repeatWith } from '../functions'
import { Traverser } from '../types'
import { line } from './line'

Expand All @@ -25,7 +25,7 @@ export function rectangle<T extends Hex>(
? optionsFromOpposingCorners(optionsOrCornerA as HexCoordinates, cornerB, createHex())
: (optionsOrCornerA as RectangleOptions)
const firstHex = createHex(start ?? cursor)
const hexes = branch<T>(
const hexes = repeatWith<T>(
line({ start: firstHex, direction: Compass.rotate(direction, 2), length: height }),
line({ direction, length: width - 1 }),
)(createHex, firstHex)
Expand Down
4 changes: 2 additions & 2 deletions src/grid/traversers/spiral.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CompassDirection } from '../../compass'
import { Hex, HexCoordinates } from '../../hex'
import { branch } from '../functions'
import { repeatWith } from '../functions'
import { RotationLike, Traverser } from '../types'
import { line } from './line'
import { ring } from './ring'

export function spiral<T extends Hex>({ radius, start, rotation }: SpiralOptions): Traverser<T, T[]> {
return function spiralTraverser(createHex, cursor) {
const center = createHex(start ?? cursor)
return branch<T>(line({ start, direction: CompassDirection.N, length: radius }), ring({ center, rotation }))(
return repeatWith<T>(line({ start, direction: CompassDirection.N, length: radius }), ring({ center, rotation }))(
createHex,
cursor,
)
Expand Down

0 comments on commit 8e400af

Please sign in to comment.