Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-resilience

Resilience pipelines for Pudu: retry, circuit breaker, timeout, fallback, hedging, rate limiting, and chaos injection

0.1.0Apache-2.01

InstallClose

Weighted.pudu

Pudu27 lines895 B

GitHub ↗
1/** @Chaos.Weighted.Module — picks one of several values by weight */2module PuduLangResilience.Chaos.Weighted34import PuduLangResilience.Chaos as Chaos5import PuduLangResilience.Randomizer as Randomizer67/// A generator answering each value with a chance in proportion to its weight. Weights below8/// one are never chosen; with no positive weight it answers nothing.9export fn choose[V](choices: &Array[(Int, V)]) -> fn(Chaos.Arguments) -> Option[V] {10  let options = *choices11  fn(given: Chaos.Arguments) -> Option[V] {12    var total = 013    for choice in options {14      if choice[0] > 0 { total = total + choice[0] }15    }16    if total == 0 { return None }17    var point = Randomizer.below(&given.randomizer, total)18    for choice in options {19      if choice[0] > 0 {20        if point < choice[0] { return Some(choice[1]) }21        point = point - choice[0]22      }23    }24    None25  }26}27