choiceN runs an integer-valued parallel computation, waits for its result, and uses that integer to select and run one computation from a list. It generalizes Boolean choice from two alternatives to N alternatives. section 7.4 section 7.9
The combinator supports these related constructions:
- Implement choice by mapping true to index 0 and false to index 1, then passing the resulting computation and a two-element list to choiceN. section 7.9
- Replace the list with a keyed Map to obtain choiceMap; the lookup strategy changes while the sequencing pattern remains the same. section 7.4 section 7.9
- Generalize further with chooser, which accepts a function from the initial result to the selected parallel computation; choiceN becomes a list lookup function. section 7.4 section 7.9
A direct implementation
def choiceN[A](n: Par[Int])(choices: List[Par[A]]): Par[A] = es => val index = n.run(es).get % choices.size choices(index).run(es) The initial computation is evaluated first. Its result selects a list position, and the selected Par is then run with the same execution context. section 7.9
