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

Function foldWeightedDecomposeFold

packages/effect/src/internal/sink.ts:1170–1208  ·  view source on GitHub ↗
(
  input: Chunk.Chunk<In>,
  s: S,
  cost: number,
  dirty: boolean,
  max: number,
  costFn: (s: S, input: In) => number,
  decompose: (input: In) => Chunk.Chunk<In>,
  f: (s: S, input: In) => S
)

Source from the content-addressed store, hash-verified

1168
1169/** @internal */
1170const foldWeightedDecomposeFold = <In, S>(
1171 input: Chunk.Chunk<In>,
1172 s: S,
1173 cost: number,
1174 dirty: boolean,
1175 max: number,
1176 costFn: (s: S, input: In) => number,
1177 decompose: (input: In) => Chunk.Chunk<In>,
1178 f: (s: S, input: In) => S
1179): [S, number, boolean, Chunk.Chunk<In>] => {
1180 for (let index = 0; index < input.length; index++) {
1181 const elem = Chunk.unsafeGet(input, index)
1182 const prevCost = cost
1183 cost = cost + costFn(s, elem)
1184 if (cost <= max) {
1185 s = f(s, elem)
1186 dirty = true
1187 continue
1188 }
1189 const decomposed = decompose(elem)
1190 if (decomposed.length <= 1 && !dirty) {
1191 // If `elem` cannot be decomposed, we need to cross the `max` threshold. To
1192 // minimize "injury", we only allow this when we haven't added anything else
1193 // to the aggregate (dirty = false).
1194 return [f(s, elem), cost, true, Chunk.drop(input, index + 1)]
1195 }
1196 if (decomposed.length <= 1 && dirty) {
1197 // If the state is dirty and `elem` cannot be decomposed, we stop folding
1198 // and include `elem` in the leftovers.
1199 return [s, prevCost, dirty, Chunk.drop(input, index)]
1200 }
1201 // `elem` got decomposed, so we will recurse with the decomposed elements pushed
1202 // into the chunk we're processing and see if we can aggregate further.
1203 input = Chunk.appendAll(decomposed, Chunk.drop(input, index + 1))
1204 cost = prevCost
1205 index = -1
1206 }
1207 return [s, cost, dirty, Chunk.empty<In>()]
1208}
1209
1210/** @internal */
1211export const foldWeightedDecomposeEffect = <S, In, E, R, E2, R2, E3, R3>(

Callers 1

Calls 2

unsafeGetMethod · 0.80
fFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…