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)
type
ScorableCollection = concept x
x[0].score is float # This is redundant ...
x is seq[Scorable] # ... so we are left with this one, which means that the type definition ...
type ScorableCollection = seq[Scorable] # ... should look like this instead.
Also newSeqWith expects something of a concrete type. agents[i] isn't, because Scorable is a type class.
The following doesn't work, but shows my intent with concepts.
Most of the time, that's not enough. Submit example code which has a chance to compile modulo the thing that doesn't work.