Arc Forumnew | comments | leaders | submitlogin
1 point by rincewind 5760 days ago | link | parent

this works for fractional weightings also

  (def reducecollect (sum list start)
    (if acons.list
        (let csum (sum start car.list)
            (cons csum (reducecollect sum cdr.list csum)))))

  (def zip args
   (when (all acons args) 
      (cons (map car args) (apply zip (map cdr args)))))

  (mac biased-choice choices
   (with (ranges (reducecollect + (map car pair.choices) 0)
          items (map cadr pair.choices)
          random (uniq))
          `(let ,random (* (rand) ,last.ranges)
               (if ,@(apply join (zip (map [list '< random _] ranges) items))))))


3 points by rincewind 5755 days ago | link

zip (like that in python) can be written shorter with map

  (def zip args (apply map list args))

-----

1 point by skenney26 5755 days ago | link

And here's a better definition of unzip:

  (def unzip (xs (o n 2))
    (apply map list (tuples xs n)))

  arc> (unzip '(a b c d e f) 3)
  ((a d) (b e) (c f))

-----