Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Created June 27, 2015 08:13
Show Gist options
  • Save joanmolinas/4ddf8ce11191af829ed1 to your computer and use it in GitHub Desktop.
Save joanmolinas/4ddf8ce11191af829ed1 to your computer and use it in GitHub Desktop.
/* Example of SET Collection */
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7, 11, 13, 17]
let threeFirstNumbers : Set = [1, 2, 3]
let threeToOne : Set = [3, 2, 1]
let tenToFifteen : Set = [10, 11, 12, 13, 14, 15]
//I use var because after `zeroToNine` will be modified.
var zeroToNine = Set(oddDigits.union(evenDigits))
// Create a new SET with both SET
oddDigits.union(evenDigits).sort() //-> [1, 3, 5, 7, 9] + [0, 2, 4, 6, 8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
//Create a new SET with the same values in the both SET
oddDigits.intersect(evenDigits).sort() //-> []
//Create a new SET with the values from the first SET that not are in second SET
oddDigits.subtract(singleDigitPrimeNumbers).sort() //-> [1, 3, 5, 7, 9] - [2, 3, 5, 7, 11, 13, 17] = [1, 9]
//Create a new SET with the values that no contain the second SET
oddDigits.exclusiveOr(singleDigitPrimeNumbers).sort() //-> [1, 3, 5, 7, 9] - [2, 3, 5, 7, 11, 13, 17] = [1, 2, 9, 11, 13, 17]
//Check if subset is in SET in the correct order
threeFirstNumbers.isSubsetOf(zeroToNine) //-> [1, 2, 3] is in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] = true
//Check if SET contain subset
zeroToNine.isSupersetOf(threeFirstNumbers) //-> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] contain [1, 2, 3] = true
//Check if subset contain diferents elements from SET
threeFirstNumbers.isDisjointWith(singleDigitPrimeNumbers) //-> [1, 2, 3] contain diferent [2, 3, 5, 7, 11, 13, 17] = false
//Check if subset is in SET but not in correct order
threeToOne.isStrictSubsetOf(zeroToNine) //-> [3, 2, 1] is in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] = true
//Determina si un array contiene otro pero no en el orden correcto
zeroToNine.isStrictSupersetOf(threeToOne) //-> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] contain [3, 2, 1] = true
//Add values from second SET into the set
zeroToNine.unionInPlace(tenToFifteen) //-> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + [10, 11, 12, 13, 14, 15] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
//Remove matching elements in both SET
zeroToNine.exclusiveOrInPlace(singleDigitPrimeNumbers) //-> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] - [2, 3, 5, 7, 11, 13, 17] = [12, 10, 14, 5, 7, 15, 3, 0, 11, 13, 2, 9, 4, 6, 8, 1]
//Remove all elements in first Set which are the second SET
zeroToNine.subtractInPlace(singleDigitPrimeNumbers) //-> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] - [2, 3, 5, 7, 11, 13, 17] = [12, 10, 14, 15, 0, 9, 4, 6, 8, 1]
//Remove all elements that not contain second SET
zeroToNine.intersectInPlace(singleDigitPrimeNumbers) //-> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] - [2, 3, 5, 7, 11, 13, 17] = [5, 7, 2, 3, 11, 13]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment