def pool
import bend-kit-concurrency@0.2.0.0/concurrency.bend as Concurrencyimport Base
230# Runs work(s, x) for every x in xs, on one worker per state in states. A
231# worker owns its state and threads it through its jobs, so an affine handle
232# such as a connection pool is never shared. Answers the final states in
233# worker order and the results in the order of xs.
234def pool(
235 ~S: Type, ~A: Type, ~B: Type, ~work: S -> A -> IO(S & B), states: List<S>, xs: List<A>
236) -> IO(List<S> & List<B>):
237 match states:
238 case Nil{}:
239 IO.die(List<S> & List<B>, 1, "Conc.pool: no workers")
240 case s <> rest:
241 pool.run(~S, ~A, ~B, ~work, s <> rest, count(A, xs))