Swiftpack.co - glessard/shuffle as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by glessard.
glessard/shuffle 2.3.0
Shuffle a Collection
⭐️ 3
🕓 3 years ago
.package(url: "https://github.com/glessard/shuffle.git", from: "2.3.0")

Shuffle Build Status

Adds a shuffled() method to any Collection, and a shuffle() method to any MutableCollection-- for Swift prior to 4.2

It shuffles the Collection as lazily as possible, without mutating the collection. The collection's indices are copied to an array, which in turn gets shuffled.

This allows the input collection to be indexed only. In general this should minimize memory allocation, for example in cases where the size of Element is greater than the size of Index.

For Swift 4.2 and up, the standard library's Collection.shuffle() and Collection.randomElement() will perform much better. While it may be possible to craft examples where using the approach in this package manages to perform better, they are undoubtedly few and far between.

Example:

import Shuffle

let a = Array(0...4)

var stats = Array<Array<Int>>(repeating: Array(repeating: 0, count: a.count), count: a.count)

(0..<8000).forEach {
  _ in
  for (i,j) in a.shuffled().enumerated()  // shuffled() used here
  {
    stats[i][j] += 1
  }
}

var sums = Array<Int>(repeating: 0, count: a.count)
for total in stats
{
  let t = total.reduce(0, +)
  for i in 0..<sums.count
  {
    sums[i] += total[i]
  }
  print(total, t)
}
print(sums)

GitHub

link
Stars: 3
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics