Syntactic sugar for Swift:
Divide a string into an array of substrings using a separator:
let input = "red,green,blue"
let components = input / "," // => ["red", "green", "blue"]
Concatenate a single string multiple times using the *
infix operator:
"hello" * 2 // => "hellohello"
Find out if an integer is divisible by another with no remainder:
15.divisibleBy(3) // => true
let choice = 4
choice.divisibleBy(3) // => false
Check whether an integer is odd or even by accessing one of its odd
or even
properties:
3.odd // => true
7.even // => false
let diceRoll = 5
diceRoll.odd // => true
Generate a random integer between zero and the specified maximum:
Int.random(100) // => 47
Generate a random integer within a specified range:
Int.random(10...20) // => 14
Create an array of random integers with values between zero and the specified maximum:
Int.random(10, count: 4) // => [5, 2, 6, 1]
Create an array of random integers with values in the given range:
Int.random(50...100, count: 3) // => [72, 89, 53]
Returns an array of integers for the given range.
Int.series(0...5) // => [0, 1, 2, 3, 4, 5]
Repeat a block of code for each integer value between the receiver and the specified maximum:
1.to(3, repeat: { number in
println(number)
})
Prints:
1
2
3
Simplify things by using a trailing closure and shorthand argument syntax:
1.to(3) { println($0) }
You can even count in reverse:
3.to(1) { println($0) }
Prints:
3
2
1
Repeat a block of code for each value in an array:
let numbers = [1, 2, 3]
numbers.each({ value in
println(value)
})
Which outputs:
1
2
3
Or simplify things even further using a trailing closure and shorthand argument syntax:
let numbers = [1, 2, 3]
numbers.each { println($0) }
Select random elements from an array using the random
method:
let numbers = [1, 2, 3, 4, 5, 6]
let randomChoice = numbers.random(4) // => [3, 5, 3, 1]
Caveat: random array elements aren't uniqued, meaning you may receive duplicate values (as shown above). This also means you can request a number of random elements that exceeds the total number of elements in the array.
Unlike the .random(count: Int)
method which returns an array of random elements that may contain duplicate values, this method allows you specify whether elements should be unique or not.
When requesting unique elements, the returned array will be no greater in size than the total number of elements of the receiver. When specifying non-unique elements the functionality will be identical to that of the .random(count: Int)
method:
["north", "east", "south", "west"].random(2, unique: true) // => ["east", "north"]
["north", "east", "south", "west"].random(5, unique: true) // => ["west", "north", "east", "south"]
["north", "east", "south", "west"].random(3, unique: false) // => ["south", "north", "south"]
["north", "east", "south", "west"].random(5, unique: false) // => ["west", "north", "east", "north", "west"]
Send a pull request with your changes, including a brief description, and they will be considered for inclusion in Splendour.
Splendour
is provided under the terms of the MIT License.
Magic Wand icon by SuperAtic LABS from The Noun Project.
Email me at [email protected] or create an issue.