def map_halves.go

unified-nsga-iii@0.2.2.0 · util.bend:275

import unified-nsga-iii@0.2.2.0/util.bend as Util
import Base

275# Balanced fork-join map. Singleton / empty stay sequential. Fuel 0 is a
276# total fallback (public callers pass |xs|). `~f` is a closed top-level.
277# Mid take/drop is inlined: a computed pair cannot be matched.
278def map_halves.go(
279  ~A: Data,
280  ~B: Data,
281  ~C: Data,
282  ~f: @+c:C -> @+x:A -> B,
283  +ctx: C,
284  +fuel: Nat,
285  xs: List<&2, A>
286) -> List<&2, B>:
287  match fuel xs:
288    case 0n Nil{}:
289      Nil{}
290    case 0n h <> t:
291      f(ctx, h) <> map_halves.go(~A, ~B, ~C, ~f, ctx, 0n, t)
292    case 1n+p Nil{}:
293      Nil{}
294    case 1n+p h <> Nil{}:
295      f(ctx, h) <> Nil{}
296    case 1n+p h <> t:
297      +all = {h <> t : List<&2, A>}
298      +mid = Nat.div(list_len(A, all), 2n)
299      a b = map_halves.go(~A, ~B, ~C, ~f, ctx, p, List.take(&2, A, all, mid)) map_halves.go(~A, ~B, ~C, ~f, ctx, p, List.drop(&2, A, all, mid))
300      List.append(&2, B, a, b)

Uses (7)

Used by (1)