Skip to content

Instantly share code, notes, and snippets.

@calvinlfer
Created January 16, 2018 04:29
Show Gist options
  • Select an option

  • Save calvinlfer/7583b5069c19665f52cdc4c1660568d4 to your computer and use it in GitHub Desktop.

Select an option

Save calvinlfer/7583b5069c19665f52cdc4c1660568d4 to your computer and use it in GitHub Desktop.
A functional implementation of distributing Auto-Scaling-Groups to different AZs in the us-east-1 region
import scala.annotation.tailrec
sealed trait UsEast1AvailabilityZone
case object UsEast1a extends UsEast1AvailabilityZone
case object UsEast1b extends UsEast1AvailabilityZone
case object UsEast1c extends UsEast1AvailabilityZone
type Count = Int
def distribute(nrOfAsgs: Int, regions: List[UsEast1AvailabilityZone]): Map[UsEast1AvailabilityZone, Count] = {
@tailrec
def inner(remaining: Int, regions: Stream[UsEast1AvailabilityZone], acc: Map[UsEast1AvailabilityZone, Count]): Map[UsEast1AvailabilityZone, Count] =
if (remaining == 0) acc
else {
val region = regions.head
inner(remaining - 1, regions.tail, acc + (region -> (acc.getOrElse(region, default = 0) + 1)))
}
inner(nrOfAsgs, Iterator.continually(regions).flatten.toStream, Map.empty[UsEast1AvailabilityZone, Count])
}
distribute(nrOfAsgs = 5, List(UsEast1a, UsEast1b, UsEast1c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment