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 | 1x 1x 1x 1x 1x 16x 16x 16x 16x 15x 15x 15x 15x 15x 5x 5x 5x 15x 15x 15x 1x 3x | import type {TreeFolderK} from '#tree'
import {treeCata} from '#tree'
import * as TreeF from '#treeF'
import {Array} from '#util'
import {identity, pipe} from 'effect'
import type {TreeArray, TreeArrayLambda, ValidArrayTree} from './types.js'
/**
* Flatten a tree level into an `ArrayTree<A>`.
*
* This is unsafe in that any type can be substituted for `A`, yet at runtime,
* if an array is found in some node value an exception will be thrown, because
* we use `Array.isArray` to discriminate between leaves and branches.
*
* The function is perfectly safe if the type `A` is _not_ an array.
* @category fold
* @category codec
* @function
*/
export const unsafeEncodeFold: TreeFolderK<TreeArrayLambda> = <A>(
treeF: TreeF.TreeF<A, TreeArray<A>>,
): TreeArray<A> => {
if (Array.isArray(TreeF.getValue(treeF)))
throw new Error('Cannot encode a tree of arrays as an array tree.')
// To convert to nested arrays, at each node:
//
// • on leaf ⇒ return it
// • on branch ⇒ tuple it
//
return pipe(
treeF,
TreeF.match<A, TreeArray<A>, TreeArray<A>>({
onLeaf: identity,
onBranch: (node, forest) => [
node,
forest as Array.NonEmptyArray<TreeArray<A>>,
],
}),
)
}
/**
* Encode a tree as nested arrays.
* @example
* import {from, of, Codec} from 'effect-tree'
*
* const tree = from(1, of(2), of(3))
*
* const encoded = Codec.Arrays.encode(tree)
*
* expect(encoded).toEqual([1, [2, 3]])
* @category codec
* @function
*/
export const encode: <A>(tree: ValidArrayTree<A>) => TreeArray<A> = tree =>
pipe(tree, treeCata(unsafeEncodeFold))
|