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 | 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 4x 4x 4x 17x 17x 17x 17x 17x 17x 7x 7x 7x 7x 7x 2x 7x 7x 17x 10x 10x 10x 10x 10x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 10x 10x 17x 17x 17x 17x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 3x 3x 1x 9x 9x 1x 1x 9x 1x | import {
getValue,
leaf,
mapEffect,
match,
tree,
treeCata,
type Tree,
} from '#tree'
import * as TreeF from '#treeF'
import {Array, flow, Function, pipe} from '#util'
import {
bimap as bimapThese,
Both,
Left,
match as matchThese,
pad as padThese,
Right,
unzipArray,
zipArraysWith,
type These,
} from '#These'
import {Effect, identity, type Option} from 'effect'
import {succeedBy} from 'effect-ts-folds'
/**
* Just like {@link zipThese} except the result is in an effect.
* @category ops
* @function
*/
export const zipTheseWithEffect = <A, B, C>(f: (these: These<A, B>) => C) => {
const [fromLeft, fromRight]: [
(self: Tree<A>) => Effect.Effect<Tree<C>>,
(self: Tree<B>) => Effect.Effect<Tree<C>>,
] = [
mapEffect(succeedBy(flow(Right.from, f))),
mapEffect(succeedBy(flow(Left.from, f))),
]
const withValue: (
value: C,
) => <E, R>(
self: Effect.Effect<Tree<C>[], E, R>,
) => Effect.Effect<Tree<C>, E, R> = value => Effect.map(tree.flipped(value))
const buildTree =
(value: C) =>
<T>(f: (self: Tree<T>) => Effect.Effect<Tree<C>>) =>
(forest: Array.NonEmptyReadonlyArray<Tree<T>>): Effect.Effect<Tree<C>> =>
pipe(forest, Effect.forEach(f), withValue(value))
return (self: Tree<A>, that: Tree<B>): Effect.Effect<Tree<C>> => {
const value: C = f(Both.from(getValue(self), getValue(that)))
return pipe(
self,
match({
onLeaf: () =>
pipe(
that,
match({
onLeaf: () => pipe(value, leaf, Effect.succeed),
onBranch: (_, thatForest) =>
buildTree(value)(fromRight)(thatForest),
}),
),
onBranch: (_, selfForest) =>
pipe(
that,
match({
onLeaf: () => buildTree(value)(fromLeft)(selfForest),
onBranch: (_, thatForest) =>
pipe(
zipArraysWith(
thatForest,
selfForest,
matchThese({
Left: ({left}) => fromLeft(left),
Right: ({right}) => fromRight(right),
Both: ({left, right}) =>
zipTheseWithEffect(f)(left, right),
}),
),
Effect.all,
withValue(value),
),
}),
),
}),
)
}
}
/**
* Unzip a single level in a tree of `These<A, B>` into a pair of potentially
* non-congruent optional trees of type `A` and `B`.
* @category fold
* @function
*/
export const unzipTheseFold: <A, B>(
self: TreeF.TreeF<These<A, B>, These<Tree<A>, Tree<B>>>,
) => These<Tree<A>, Tree<B>> = TreeF.match({
onLeaf: bimapThese(leaf, leaf),
onBranch: (value, forest) => {
const [leftForest, rightForest] = unzipArray(forest)
return pipe(
value,
bimapThese(tree.curried(rightForest), tree.curried(leftForest)),
)
},
})
/**
* Unzip a tree of {@link These} into a pair of optional trees.
*
* See also {@link zipThese} for the opposite operation.
* @category ops
* @function
*/
export const unzipThese = <A, B>(
self: Tree<These<A, B>>,
): [Option.Option<Tree<A>>, Option.Option<Tree<B>>] =>
pipe(self, treeCata(unzipTheseFold<A, B>), padThese)
/**
* Just like {@link zipThese} except the given function will be given the
* {@link These} value for the pair. If both are present, the function will
* receive a {@link Both}. Otherwise it will receive either a
* {@link Left} or a {@link Right}.
* @category ops
* @function
*/
export const zipTheseWith = <A, B, C>(
f: (these: These<A, B>) => C,
): ((self: Tree<A>, that: Tree<B>) => Tree<C>) =>
flow(zipTheseWithEffect(f), Effect.runSync)
/**
* Like {@link zip}, except does not crop to the shortest/shallowest branch of
* the zipped pair. Instead it _stretches_ the tree to the longest/deepest
* branch of the zipper pair, and is thus associative.
*
* To account for subtrees where only one of the sides is available, the
* tree is returned not as a `Tree<[A, B]>`, but instead as a
* `Tree<These<A, B>>`. See {@link These | the These API} for more information.
*
* See also:
*
* 1. {@link unzipThese} for the opposite operation.
* 2. {@link zipTheseWith} to run a function on the partial pair.
* @example
* import {These, zipThese, from, of} from 'effect-tree'
*
* const left = from(1, of(2))
* const right = from(3, of(4), of(5))
*
* expect(zipThese(left, right)).toEqual(
* from(
* These.both(3, 1),
* of(These.both(4, 2)),
* of(These.left(5)),
* )
* )
* @category ops
* @function
*/
export const zipThese: {
<A, B>(self: Tree<A>, that: Tree<B>): Tree<These<A, B>>
<B>(that: Tree<B>): <A>(self: Tree<A>) => Tree<These<A, B>>
} = Function.dual(2, <A, B>(self: Tree<A>, that: Tree<B>) =>
zipTheseWith(identity<These<A, B>>)(self, that),
)
|