3.1.7 pick
Usage:
include pick
import pick as ...
3.1.7.1 The Pick Datatype
The primary use of pick is as a way of obtaining values from sets. See the documentation of .pick.
However, nothing precludes other datatypes from also implementing the
Pick interface. For instance, here’s a simple queue definition that
provides a pick method:
import pick as P data Queue<T>: | queue(elts :: List<T>) with: method pick(self): cases (List) self.elts: | empty => P.pick-none | link(f, r) => P.pick-some(f, queue(r)) end end end
We can then write a function that uses that method to traverse the queue:
fun sum-queue(q :: Queue) -> Number: cases (P.Pick) q.pick(): | pick-none => 0 | pick-some(e, r) => e + sum-queue(r) end end
with the expected behavior:
Examples:
check: q = queue([list: 1, 2, 3]) sum-queue(q) is 6 end