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
| extension Array { | |
| func chunked(by chunkSize: Int) -> [[Element]] { | |
| return stride(from: 0, to: self.count, by: chunkSize).map { | |
| Array(self[$0..<Swift.min($0 + chunkSize, self.count)]) | |
| } | |
| } | |
| } | |
| let arr = [0,1,2,3,4,5,6,7,8,9] | |
| print(arr.chunked(by: 2)) // [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]] |
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
| var nums = (10...19) | |
| let result = nums.enumerated().flatMap { [count = nums.count, x = 10] (offset: Int, element: Int) -> [Int] in | |
| if offset < count / 2 { | |
| return [] | |
| } | |
| return [element, element-x] | |
| } | |
| print(result) // [15, 5, 16, 6, 17, 7, 18, 8, 19, 9] |
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
| quicksort :: (Ord a) => [a] -> [a] | |
| quicksort [] = [] | |
| quicksort (x:xs) = | |
| let smallOrEqual = filter (<= x) xs | |
| larger = filter (> x) xs | |
| in quicksort smallOrEqual ++ [x] ++ quicksort larger |