Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

juxt

Applies every function in a list to the same value and collects the results.

List<R> Function(T a) juxt<T, R>(List<R Function(T a)> fns)

Lecture

Give juxt a list of functions that all accept the same input, and it gives you back one function that runs the input through every one of them and returns a List of the results, in order. It's a compact way to compute several independent views of a single value without repeating yourself.

FxTS's juxt is variadic and can accept functions with different argument counts. Dart has no variadic generics, so FxDart's version takes a single unary function T -> R per entry — still enough to cover the common case of "run these N read-only projections against this one value."

Demo 1 · Basics

The classic example from the dartdoc: computing the min and max of a list in one pass:

Demo 2 · Projecting several fields

Pulling multiple fields out of a record-like Map at once:

Try it yourself

Exercise: use juxt to compute the sum and average of this list in a single pass.

Related: apply — call a function with a dynamic argument list · cases — pick one function based on a predicate instead of running them all · min / max — used together in the demo above · zip — combine values from two sequences instead of one