MCPcopy Create free account
hub / github.com/Effect-TS/effect / merge

Function merge

packages/effect/src/internal/stream.ts:8031–8117  ·  view source on GitHub ↗
(
      leftChunk: Chunk.Chunk<readonly [K, A]>,
      rightChunk: Chunk.Chunk<readonly [K, A2]>
    )

Source from the content-addressed store, hash-verified

8029 }
8030 }
8031 const merge = (
8032 leftChunk: Chunk.Chunk<readonly [K, A]>,
8033 rightChunk: Chunk.Chunk<readonly [K, A2]>
8034 ): readonly [
8035 Chunk.Chunk<[K, A3]>,
8036 ZipAllState.ZipAllState<readonly [K, A], readonly [K, A2]>
8037 ] => {
8038 const hasNext = <T>(chunk: Chunk.Chunk<T>, index: number) => index < chunk.length - 1
8039 const builder: Array<[K, A3]> = []
8040 let state:
8041 | ZipAllState.ZipAllState<
8042 readonly [K, A],
8043 readonly [K, A2]
8044 >
8045 | undefined = undefined
8046 let leftIndex = 0
8047 let rightIndex = 0
8048 let leftTuple = pipe(leftChunk, Chunk.unsafeGet(leftIndex))
8049 let rightTuple = pipe(rightChunk, Chunk.unsafeGet(rightIndex))
8050 let k1 = leftTuple[0]
8051 let a = leftTuple[1]
8052 let k2 = rightTuple[0]
8053 let a2 = rightTuple[1]
8054 let loop = true
8055 while (loop) {
8056 const compare = options.order(k1, k2)
8057 if (compare === 0) {
8058 builder.push([k1, options.onBoth(a, a2)])
8059 if (hasNext(leftChunk, leftIndex) && hasNext(rightChunk, rightIndex)) {
8060 leftIndex = leftIndex + 1
8061 rightIndex = rightIndex + 1
8062 leftTuple = pipe(leftChunk, Chunk.unsafeGet(leftIndex))
8063 rightTuple = pipe(rightChunk, Chunk.unsafeGet(rightIndex))
8064 k1 = leftTuple[0]
8065 a = leftTuple[1]
8066 k2 = rightTuple[0]
8067 a2 = rightTuple[1]
8068 } else if (hasNext(leftChunk, leftIndex)) {
8069 state = ZipAllState.PullRight(pipe(leftChunk, Chunk.drop(leftIndex + 1)))
8070 loop = false
8071 } else if (hasNext(rightChunk, rightIndex)) {
8072 state = ZipAllState.PullLeft(pipe(rightChunk, Chunk.drop(rightIndex + 1)))
8073 loop = false
8074 } else {
8075 state = ZipAllState.PullBoth
8076 loop = false
8077 }
8078 } else if (compare < 0) {
8079 builder.push([k1, options.onSelf(a)])
8080 if (hasNext(leftChunk, leftIndex)) {
8081 leftIndex = leftIndex + 1
8082 leftTuple = pipe(leftChunk, Chunk.unsafeGet(leftIndex))
8083 k1 = leftTuple[0]
8084 a = leftTuple[1]
8085 } else {
8086 const rightBuilder: Array<readonly [K, A2]> = []
8087 rightBuilder.push(rightTuple)
8088 while (hasNext(rightChunk, rightIndex)) {

Callers 5

collectAllToMapFunction · 0.70
collectAllToMapNFunction · 0.70
stream.tsFile · 0.70
pullFunction · 0.70
passthroughFunction · 0.70

Calls 3

hasNextFunction · 0.85
unsafeGetMethod · 0.80
pipeFunction · 0.70

Tested by

no test coverage detected