How can I have a function that returns a subset copy of a collection of things
that always have a certain property? The following doesn't work, but shows my
intent with concepts. It works if I use `openArray[Agent]` explicitly instead
of a concept.
import sequtils, random, algorithm, itertools
type
Agent = object
score:float
Scorable = concept x
x.score is float
ScorableCollection = concept x
x[0].score is float
x is seq[Scorable]
proc roulette_wheel(agents:ScorableCollection, N:int):auto =
proc sum(a,b:float):float = a+b
var wheel = toSeq: agents . mapIt(it.score) . accumulate sum
result = N . newSeqWith agents[ wheel . lowerBound rand wheel[^1] ]
var agents = newSeqWith(10, Agent(score:random(1.0)))
echo roulette_wheel(agents, 2)
Run