Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 1x 1x 1x 1x 1x 1x 1x 1x 35x 89x 1x 1x 304x 606x 606x 606x 606x 606x 606x 1x 1x 1x 11x 11x 11x 11x 11x 11x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 8x 293x 293x 293x 293x 293x 293x 293x 293x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 695x 695x 695x 695x 695x | import {
annotateEffectFolder,
annotateEffectUnfolder,
replaceEffectFolder,
treeAnaE,
treeCataEffect,
unfixTree,
type Branch,
type Tree,
type TreeEffectFolder,
type TreeEffectFolderOf,
} from '#tree'
import * as treeF from '#treeF'
import {type TreeF} from '#treeF'
import {Effect, Number, pipe, Ref, type Order} from 'effect'
import {constant} from 'effect/Function'
import {
annotateBreadthFirst,
replaceBreadthFirst,
} from './ordinal/breadthFirst.js'
/**
* Fold nodes of a tree level to their ordinal in depth-first pre-order.
*
* A function of the type:
*
* ```ts
* <A, E = never, R = never>(
* _: TreeF<A, number>
* ): Effect.Effect<number, E, R>
* ```
* @category fold
* @function
*/
export const nodeOrdinalFold =
(counter: Ref.Ref<number>): TreeEffectFolderOf<number> =>
<A, E = never, R = never>(_: TreeF<A, number>): Effect.Effect<number, E, R> =>
useCounter(counter)
/**
* Unfold nodes of a tree level to their ordinal in depth-first pre-order.
*
* @category fold
* @function
*/
export const nodeOrdinalUnfold =
(counter: Ref.Ref<number>) =>
<A, E = never, R = never>(
self: Tree<A>,
): Effect.Effect<TreeF<number, Tree<A>>, E, R> =>
pipe(
counter,
useCounter,
Effect.map(n => pipe(self, unfixTree, treeF.mapValue(constant(n)))),
)
/**
* Unfold a tree level and annotate the nodes with their ordinal.
* @category fold
* @function
*/
export const annotateOrdinalUnfold =
(counter: Ref.Ref<number>) =>
<A, E = never, R = never>(
pair: [Tree<A>, number],
): Effect.Effect<TreeF<[A, number], [Tree<A>, number]>, E, R> =>
pipe(
counter,
nodeOrdinalUnfold,
annotateEffectUnfolder<number, A, E, R>,
)(pair)
/**
* Replaces all tree nodes with their unique ordinal. The order is depth-first
* post-order, so that the root node value is the maximum. For example a tree
* shaped so:
*
*
* ```
* ┬?
* ├─?
* └┬?
* ├─?
* └─?
* ```
*
* Will become this tree:
* ```
* ┬5
* ├─1
* └┬4
* ├─2
* └─3
* ```
*
* Under the `pre` key you will find a version that does the same but in
* depth-first pre-order, and under the `breadthFirst` key you will find a version
* that does the same but in breadth-first order.
* @category ops
* @function
*/
export const asOrdinal =
(initialize: number) =>
(self: Tree<any>): Tree<number> => {
const counterEffect: Effect.Effect<Ref.Ref<number>> = Ref.make(initialize)
const replace = (
counter: Ref.Ref<number>,
): TreeEffectFolder<unknown, Tree<number>, never, never> =>
replaceEffectFolder(nodeOrdinalFold(counter))
return pipe(
counterEffect,
Effect.map(replace),
Effect.flatMap(φ => treeCataEffect(φ)(self)),
Effect.runSync,
)
}
asOrdinal.pre =
(initialize: number) =>
(self: Tree<any>): Tree<number> =>
pipe(
initialize,
Ref.make,
Effect.flatMap(counter =>
pipe(self, treeAnaE(nodeOrdinalUnfold(counter))),
),
Effect.runSync,
)
asOrdinal.breadthFirst =
<A>(order: Order.Order<A>) =>
(start: number): ((self: Tree<A>) => Tree<number>) =>
replaceBreadthFirst(order, start)
/**
* Annotate tree nodes with their post-order depth-first ordinal.
*
* Under the `pre` key you will find a version that does the same but in
* depth-first pre-order, and under the `breadthFirst` key you will find a
* version that does the same but in breadth-first order.
* @category ops
* @function
*/
export const withOrdinal =
(initialize = 1) =>
<A>(self: Tree<A>): Tree<[A, number]> => {
const counterEffect: Effect.Effect<Ref.Ref<number>> = Ref.make(initialize)
const annotate =
(counter: Ref.Ref<number>) => (self: treeF.TreeF<A, Tree<[A, number]>>) =>
pipe(self, annotateEffectFolder(nodeOrdinalFold(counter)))
return pipe(
counterEffect,
Effect.map(annotate),
Effect.flatMap(φ => treeCataEffect(φ)(self)),
Effect.runSync,
)
}
withOrdinal.pre =
(initialize: number) =>
<A>(self: Tree<A>): Tree<[A, number]> =>
pipe(
initialize,
Ref.make,
Effect.flatMap(counter =>
treeAnaE(annotateOrdinalUnfold(counter))([self, 0]),
),
Effect.runSync,
)
withOrdinal.breadthFirst =
<A>(order: Order.Order<A>) =>
(start: number): ((self: Tree<A>) => Tree<[A, number]>) =>
annotateBreadthFirst(order, start)
/**
* A version of {@link asOrdinal} specialized for _branches_.
* @category ops
* @function
*/
export const asOrdinalBranch =
(initialize: number) =>
(self: Branch<any>): Branch<number> =>
pipe(self, asOrdinal(initialize)) as Branch<number>
asOrdinalBranch.pre =
(initialize: number) =>
<A>(self: Branch<A>) =>
pipe(self, asOrdinal.pre(initialize)) as Branch<number>
const useCounter = (ref: Ref.Ref<number>): Effect.Effect<number> =>
pipe(
ref,
Ref.get,
Effect.tap(() => Ref.update(ref, Number.increment)),
)
|