Last active
December 11, 2024 12:13
-
-
Save imjhk03/1f64b30b875a0dfc1cd7e9fb2cfb30c4 to your computer and use it in GitHub Desktop.
Few examples from Swift and the Swift Algorithms core package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Algorithms | |
| // The ~= operator means "contains", and is used to test if a value is within a range. | |
| let seq = 9...13 | |
| print(seq ~= 9) // true | |
| print(seq ~= 50) // false | |
| // The .adjacentPairs() method lets you iterate through a sequence in pairs of current + next. | |
| let pairs = ["a", "b", "c", "d"].adjacentPairs() | |
| for pair in pairs { print(pair) } // ("a", "b"), ("b", "c"), ("c", "d") | |
| // The .combinations(ofCount:) method returns a collection of combination of specified count length from a sequence. | |
| let combinations = ["a", "b", "c", "d"].combinations(ofCount: 3) | |
| for combination in combinations { print(combination) } | |
| // ["a", "b", "c"], ["a", "b", "d"], ["a", "c", "d"], ["b", "c", "d"] | |
| // Source: https://x.com/SwiftLang/status/1863819687186116655 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment