本页尚未翻译,因此以英文显示。 参与翻译

fxPipe

Typed left-to-right composition. The last .then is the function — no .build().

R Function(A) fxPipe<A, R>(R Function(A) f) S Function(A) (R Function(A)).then<S>(S Function(R) next) R Function(A) fxPipe2<A, M, R>(M Function(A) first, R Function(M) second) R Function(A) fxPipe3<A, M1, M2, R>(M1 Function(A) first, M2 Function(M1) second, R Function(M2) third) R Function(A) fxPipe4<A, M1, M2, M3, R>(…) R Function(A) fxPipe5<A, M1, M2, M3, M4, R>(…)

Lecture

pipe threads a value through a list of functions, but the list is dynamic. fxPipe is the typed form that returns a function:

final f = fxPipe3(parse, normalise, score);
f(line);
fx(lines).map(f);

Each .then returns the chain so far, so there is nothing to "finish." Call it, or pass it to map / parallel. fxPipe(parse) is just parse — the name marks the start. parse.then(normalise) works too.

Two .parallel calls copy every result back to this isolate and out again. One composed worker pays the hop once:

await fx(lines)
    .parallel(4, fxPipe3(parse, normalise, score), chunked: true)
    .toList();

Stages must be sendable when the result is a parallel worker. juxt is the other direction: several functions, one input, a list of results.

fxPipe2..fxPipe5 fuse the same stages into one closure, so a hot map or parallel worker does not pay a nested call per .then. Arity stops at 5, like zipOrAccumulate2..5. Longer than that, keep chaining .then or write the fused function yourself.

parallel is VM-only, so the playgrounds below run the same composed function through map.

Demo 1 · parse, normalise, score

Three stages, one function. On the VM you would pass this to parallel; here it runs in the playground.

Demo 2 · Same numbers as three maps

fxPipe is composition, not a new operator. Three maps and one composed function print the same list.

Try it yourself

Exercise: compose parsenormalisescore and keep only rows that score at least 4.

Related: pipe — the same idea, untyped, over a value · juxt — several functions, one input, a list of results · map — the same composition on this isolate · parallel — where composing workers saves a hop