Dollar.swift 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. //
  2. // ___ ___ __ ___ ________ _________
  3. // _|\ \__|\ \ |\ \|\ \|\ _____\\___ ___\
  4. // |\ ____\ \ \ \ \ \ \ \ \ \__/\|___ \ \_|
  5. // \ \ \___|\ \ \ __\ \ \ \ \ \ __\ \ \ \
  6. // \ \_____ \ \ \|\__\_\ \ \ \ \ \_| \ \ \
  7. // \|____|\ \ \____________\ \__\ \__\ \ \__\
  8. // ____\_\ \|____________|\|__|\|__| \|__|
  9. // |\___ __\
  10. // \|___|\__\_|
  11. // \|__|
  12. //
  13. // Dollar.swift
  14. // $ - A functional tool-belt for Swift Language
  15. //
  16. // Created by Ankur Patel on 6/3/14.
  17. // Copyright (c) 2014 Encore Dev Labs LLC. All rights reserved.
  18. //
  19. import Foundation
  20. #if os(Linux)
  21. import Dispatch
  22. #endif
  23. open class Dollar {
  24. /// ___ ___ _______ ___ ________ _______ ________
  25. /// |\ \|\ \|\ ___ \ |\ \ |\ __ \|\ ___ \ |\ __ \
  26. /// \ \ \\\ \ \ __/|\ \ \ \ \ \|\ \ \ __/|\ \ \|\ \
  27. /// \ \ __ \ \ \_|/_\ \ \ \ \ ____\ \ \_|/_\ \ _ _\
  28. /// \ \ \ \ \ \ \_|\ \ \ \____\ \ \___|\ \ \_|\ \ \ \\ \|
  29. /// \ \__\ \__\ \_______\ \_______\ \__\ \ \_______\ \__\\ _\
  30. /// \|__|\|__|\|_______|\|_______|\|__| \|_______|\|__|\|__|
  31. ///
  32. /// Creates a function that executes passed function only after being called n times.
  33. ///
  34. /// - parameter num: Number of times after which to call function.
  35. /// - parameter function: Function to be called that takes params.
  36. /// - returns: Function that can be called n times after which the callback function is called.
  37. open class func after<T, E>(_ num: Int, function: @escaping (T...) -> E) -> ((T...) -> E?) {
  38. var counter = num
  39. return { (params: T...) -> E? in
  40. typealias Function = ([T]) -> E
  41. counter -= 1
  42. if counter <= 0 {
  43. let f = unsafeBitCast(function, to: Function.self)
  44. return f(params)
  45. }
  46. return .none
  47. }
  48. }
  49. /// Creates a function that executes passed function only after being called n times.
  50. ///
  51. /// - parameter num: Number of times after which to call function.
  52. /// - parameter function: Function to be called that does not take any params.
  53. /// - returns: Function that can be called n times after which the callback function is called.
  54. open class func after<T>(_ num: Int, function: @escaping () -> T) -> (() -> T?) {
  55. let f = self.after(num) { (params: Any?...) -> T in
  56. return function()
  57. }
  58. return { f() }
  59. }
  60. /// Creates an array of elements from the specified indexes, or keys, of the collection.
  61. /// Indexes may be specified as individual arguments or as arrays of indexes.
  62. ///
  63. /// - parameter array: The array to source from
  64. /// - parameter indexes: Get elements from these indexes
  65. /// - returns: New array with elements from the indexes specified.
  66. open class func at<T>(_ array: [T], indexes: Int...) -> [T] {
  67. return self.at(array, indexes: indexes)
  68. }
  69. /// Creates an array of elements from the specified indexes, or keys, of the collection.
  70. /// Indexes may be specified as individual arguments or as arrays of indexes.
  71. ///
  72. /// - parameter array: The array to source from
  73. /// - parameter indexes: Get elements from these indexes
  74. /// - returns: New array with elements from the indexes specified.
  75. open class func at<T>(_ array: [T], indexes: [Int]) -> [T] {
  76. var result: [T] = []
  77. for index in indexes {
  78. result.append(array[index])
  79. }
  80. return result
  81. }
  82. /// Creates a function that, when called, invokes func with the binding of arguments provided.
  83. ///
  84. /// - parameter function: Function to be bound.
  85. /// - parameter parameters: Parameters to be passed into the function when being invoked.
  86. /// - returns: A new function that when called will invoked the passed function with the parameters specified.
  87. open class func bind<T, E>(_ function: @escaping (T...) -> E, _ parameters: T...) -> (() -> E) {
  88. return { () -> E in
  89. typealias Function = ([T]) -> E
  90. let f = unsafeBitCast(function, to: Function.self)
  91. return f(parameters)
  92. }
  93. }
  94. /// Creates an array of elements split into groups the length of size.
  95. /// If array can’t be split evenly, the final chunk will be the remaining elements.
  96. ///
  97. /// - parameter array: to chunk
  98. /// - parameter size: size of each chunk
  99. /// - returns: array elements chunked
  100. open class func chunk<T>(_ array: [T], size: Int = 1) -> [[T]] {
  101. var result = [[T]]()
  102. var chunk = -1
  103. for (index, elem) in array.enumerated() {
  104. if index % size == 0 {
  105. result.append([T]())
  106. chunk += 1
  107. }
  108. result[chunk].append(elem)
  109. }
  110. return result
  111. }
  112. /// Creates an array with all nil values removed.
  113. ///
  114. /// - parameter array: Array to be compacted.
  115. /// - returns: A new array that doesnt have any nil values.
  116. open class func compact<T>(_ array: [T?]) -> [T] {
  117. var result: [T] = []
  118. for elem in array {
  119. if let val = elem {
  120. result.append(val)
  121. }
  122. }
  123. return result
  124. }
  125. /// Compose two or more functions passing result of the first function
  126. /// into the next function until all the functions have been evaluated
  127. ///
  128. /// - parameter functions: - list of functions
  129. /// - returns: A function that can be called with variadic parameters of values
  130. open class func compose<T>(_ functions: ((T...) -> [T])...) -> ((T...) -> [T]) {
  131. typealias Function = ([T]) -> [T]
  132. return { (result: T...) -> [T] in
  133. var result = result
  134. for fun in functions {
  135. let f = unsafeBitCast(fun, to: Function.self)
  136. result = f(result)
  137. }
  138. return result
  139. }
  140. }
  141. /// Compose two or more functions passing result of the first function
  142. /// into the next function until all the functions have been evaluated
  143. ///
  144. /// - parameter functions: - list of functions
  145. /// - returns: A function that can be called with array of values
  146. open class func compose<T>(_ functions: (([T]) -> [T])...) -> (([T]) -> [T]) {
  147. return {
  148. var result = $0
  149. for fun in functions {
  150. result = fun(result)
  151. }
  152. return result
  153. }
  154. }
  155. /// Checks if a given value is present in the array.
  156. ///
  157. /// - parameter array: The array to check against.
  158. /// - parameter value: The value to check.
  159. /// - returns: Whether value is in the array.
  160. open class func contains<T: Equatable>(_ array: [T], value: T) -> Bool {
  161. return array.contains(value)
  162. }
  163. /// Create a copy of an array
  164. ///
  165. /// - parameter array: The array to copy
  166. /// - returns: New copy of array
  167. open class func copy<T>(_ array: [T]) -> [T] {
  168. var newArr: [T] = []
  169. for elem in array {
  170. newArr.append(elem)
  171. }
  172. return newArr
  173. }
  174. /// Cycles through the array indefinetly passing each element into the callback function
  175. ///
  176. /// - parameter array: to cycle through
  177. /// - parameter callback: function to call with the element
  178. open class func cycle<T, U>(_ array: [T], callback: (T) -> U) {
  179. while true {
  180. for elem in array {
  181. _ = callback(elem)
  182. }
  183. }
  184. }
  185. /// Cycles through the array n times passing each element into the callback function
  186. ///
  187. /// - parameter array: to cycle through
  188. /// - parameter times: Number of times to cycle through the array
  189. /// - parameter callback: function to call with the element
  190. open class func cycle<T, U>(_ array: [T], _ times: Int, callback: (T) -> U) {
  191. for _ in 0..<times {
  192. for elem in array {
  193. _ = callback(elem)
  194. }
  195. }
  196. }
  197. /// Delays the execution of a function by the specified DispatchTimeInterval
  198. ///
  199. /// - parameter by: interval to delay the execution of the function by
  200. /// - parameter queue: Queue to run the function on. Defaults to main queue
  201. /// - parameter function: function to execute
  202. open class func delay(by interval: DispatchTimeInterval, queue: DispatchQueue = .main, _ function: @escaping () -> Void) {
  203. queue.asyncAfter(deadline: .now() + interval, execute: function)
  204. }
  205. /// Debounce a function such that the function is only invoked once no matter how many times
  206. /// it is called within the delayBy interval
  207. ///
  208. /// - parameter delayBy: interval to delay the execution of the function by
  209. /// - parameter queue: Queue to run the function on. Defaults to main queue
  210. /// - parameter function: function to execute
  211. /// - returns: Function that is debounced and will only invoke once within the delayBy interval
  212. open class func debounce(delayBy: DispatchTimeInterval, queue: DispatchQueue = .main, _ function: @escaping (() -> Void)) -> () -> Void {
  213. var currentWorkItem: DispatchWorkItem?
  214. return {
  215. currentWorkItem?.cancel()
  216. currentWorkItem = DispatchWorkItem { function() }
  217. queue.asyncAfter(deadline: .now() + delayBy, execute: currentWorkItem!)
  218. }
  219. }
  220. /// Throttle a function such that the function is invoked immediately, and only once no matter
  221. /// how many times it is called within the limitTo interval
  222. ///
  223. /// - parameter limitTo: interval during which subsequent calls will be ignored
  224. /// - parameter queue: Queue to run the function on. Defaults to main queue
  225. /// - parameter function: function to execute
  226. /// - returns: Function that is throttled and will only invoke immediately and only once within the limitTo interval
  227. open class func throttle(limitTo: DispatchTimeInterval, queue: DispatchQueue = .main, _ function: @escaping (() -> Void)) -> () -> Void {
  228. var allowFunction: Bool = true
  229. return {
  230. guard allowFunction else { return }
  231. allowFunction = false
  232. function()
  233. queue.asyncAfter(deadline: .now() + limitTo, qos: .background) {
  234. allowFunction = true
  235. }
  236. }
  237. }
  238. /// Creates an array excluding all values of the provided arrays in order
  239. ///
  240. /// - parameter arrays: The arrays to difference between.
  241. /// - returns: The difference between the first array and all the remaining arrays from the arrays params.
  242. open class func differenceInOrder<T: Equatable>(_ arrays: [[T]]) -> [T] {
  243. return Dollar.reduce(self.rest(arrays), initial: self.first(arrays)!) { (result, arr) -> [T] in
  244. return result.filter() { !arr.contains($0) }
  245. }
  246. }
  247. /// Creates an array excluding all values of the provided arrays with or without order
  248. /// Without order difference is much faster and at times 100% than difference with order
  249. ///
  250. /// - parameter arrays: The arrays to difference between.
  251. /// - parameter inOrder: Optional Paramter which is true by default
  252. /// - returns: The difference between the first array and all the remaining arrays from the arrays params.
  253. open class func difference<T: Hashable>(_ arrays: [T]..., inOrder: Bool = true) -> [T] {
  254. if inOrder {
  255. return self.differenceInOrder(arrays)
  256. } else {
  257. var result: [T] = []
  258. var map: [T: Int] = [T: Int]()
  259. let firstArr: [T] = self.first(arrays)!
  260. let restArr: [[T]] = self.rest(arrays) as [[T]]
  261. for elem in firstArr {
  262. if let val = map[elem] {
  263. map[elem] = val + 1
  264. } else {
  265. map[elem] = 1
  266. }
  267. }
  268. for arr in restArr {
  269. for elem in arr {
  270. map.removeValue(forKey: elem)
  271. }
  272. }
  273. for (key, count) in map {
  274. for _ in 0..<count {
  275. result.append(key)
  276. }
  277. }
  278. return result
  279. }
  280. }
  281. /// Call the callback passing each element in the array
  282. ///
  283. /// - parameter array: The array to iterate over
  284. /// - parameter callback: function that gets called with each item in the array
  285. /// - returns: The array passed
  286. @discardableResult
  287. open class func each<T>(_ array: [T], callback: (T) -> ()) -> [T] {
  288. for elem in array {
  289. callback(elem)
  290. }
  291. return array
  292. }
  293. /// Call the callback passing index of the element and each element in the array
  294. ///
  295. /// - parameter array: The array to iterate over
  296. /// - parameter callback: function that gets called with each item in the array with its index
  297. /// - returns: The array passed
  298. @discardableResult
  299. open class func each<T>(_ array: [T], callback: (Int, T) -> ()) -> [T] {
  300. for (index, elem): (Int, T) in array.enumerated() {
  301. callback(index, elem)
  302. }
  303. return array
  304. }
  305. /// Call the callback on all elements that meet the when condition
  306. ///
  307. /// - parameter array: The array to check.
  308. /// - parameter when: Condition to check before performing callback
  309. /// - parameter callback: Check whether element value is true or false.
  310. /// - returns: The array passed
  311. @discardableResult
  312. open class func each<T>(_ array: [T], when: (T) -> Bool, callback: (T) -> ()) -> [T] {
  313. for elem in array where when(elem) {
  314. callback(elem)
  315. }
  316. return array
  317. }
  318. /// Checks if two optionals containing Equatable types are equal.
  319. ///
  320. /// - parameter value: The first optional to check.
  321. /// - parameter other: The second optional to check.
  322. /// - returns: true if the optionals contain two equal values, or both are nil; false otherwise.
  323. open class func equal<T: Equatable>(_ value: T?, _ other: T?) -> Bool {
  324. switch (value, other) {
  325. case (.none, .none):
  326. return true
  327. case (.none, .some(_)):
  328. return false
  329. case (.some(_), .none):
  330. return false
  331. case (.some(let unwrappedValue), .some(let otherUnwrappedValue)):
  332. return unwrappedValue == otherUnwrappedValue
  333. }
  334. }
  335. /// Checks if the given callback returns true value for all items in the array.
  336. ///
  337. /// - parameter array: The array to check.
  338. /// - parameter callback: Check whether element value is true or false.
  339. /// - returns: true if the given callback returns true value for all items in the array; false otherwise.
  340. open class func every<T>(_ array: [T], callback: (T) -> Bool) -> Bool {
  341. for elem in array {
  342. if !callback(elem) {
  343. return false
  344. }
  345. }
  346. return true
  347. }
  348. /// Returns Factorial of integer
  349. ///
  350. /// - parameter num: number whose factorial needs to be calculated
  351. /// - returns: factorial
  352. open class func factorial(_ num: Int) -> Int {
  353. guard num > 0 else { return 1 }
  354. return num * Dollar.factorial(num - 1)
  355. }
  356. /// Get element from an array at the given index which can be negative
  357. /// to find elements from the end of the array
  358. ///
  359. /// - parameter array: The array to fetch from
  360. /// - parameter index: Can be positive or negative to find from end of the array
  361. /// - parameter orElse: Default value to use if index is out of bounds
  362. /// - returns: Element fetched from the array or the default value passed in orElse
  363. open class func fetch<T>(_ array: [T], _ index: Int, orElse: T? = .none) -> T! {
  364. if index < 0 && -index < array.count {
  365. return array[array.count + index]
  366. } else if index < array.count {
  367. return array[index]
  368. } else {
  369. return orElse
  370. }
  371. }
  372. /// Fills elements of array with value from start up to, but not including, end.
  373. ///
  374. /// - parameter array: to fill
  375. /// - parameter withElem: the element to replace
  376. /// - parameter startIndex: start index
  377. /// - parameter endIndex: end index
  378. /// - returns: array elements chunked
  379. open class func fill<T>(_ array: inout [T], withElem elem: T, startIndex: Int = 0, endIndex: Int? = .none) -> [T] {
  380. let endIndex = endIndex ?? array.count
  381. for (index, _) in array.enumerated() {
  382. if index > endIndex { break }
  383. if index >= startIndex && index <= endIndex {
  384. array[index] = elem
  385. }
  386. }
  387. return array
  388. }
  389. /// Iterates over elements of an array and returning the first element
  390. /// that the callback returns true for.
  391. ///
  392. /// - parameter array: The array to search for the element in.
  393. /// - parameter callback: The callback function to tell whether element is found.
  394. /// - returns: Optional containing either found element or nil.
  395. open class func find<T>(_ array: [T], callback: (T) -> Bool) -> T? {
  396. for elem in array {
  397. let result = callback(elem)
  398. if result {
  399. return elem
  400. }
  401. }
  402. return .none
  403. }
  404. /// This method is like find except that it returns the index of the first element
  405. /// that passes the callback check.
  406. ///
  407. /// - parameter array: The array to search for the element in.
  408. /// - parameter callback: Function used to figure out whether element is the same.
  409. /// - returns: First element's index from the array found using the callback.
  410. open class func findIndex<T>(_ array: [T], callback: (T) -> Bool) -> Int? {
  411. for (index, elem): (Int, T) in array.enumerated() {
  412. if callback(elem) {
  413. return index
  414. }
  415. }
  416. return .none
  417. }
  418. /// This method is like findIndex except that it iterates over elements of the array
  419. /// from right to left.
  420. ///
  421. /// - parameter array: The array to search for the element in.
  422. /// - parameter callback: Function used to figure out whether element is the same.
  423. /// - returns: Last element's index from the array found using the callback.
  424. open class func findLastIndex<T>(_ array: [T], callback: (T) -> Bool) -> Int? {
  425. let count = array.count
  426. for (index, _) in array.enumerated() {
  427. let reverseIndex = count - (index + 1)
  428. let elem: T = array[reverseIndex]
  429. if callback(elem) {
  430. return reverseIndex
  431. }
  432. }
  433. return .none
  434. }
  435. /// Gets the first element in the array.
  436. ///
  437. /// - parameter array: The array to wrap.
  438. /// - returns: First element from the array.
  439. open class func first<T>(_ array: [T]) -> T? {
  440. if array.isEmpty {
  441. return .none
  442. } else {
  443. return array[0]
  444. }
  445. }
  446. /// Splits a collection into sets, grouped by the result of running each value through a callback.
  447. ///
  448. /// - parameter array: The array to group
  449. /// - parameter callback: Function whose response will be used as a key in the new string
  450. /// - returns: grouped collection
  451. open class func groupBy<T, U>(_ array: [T], callback: (T) -> U) -> [U: [T]] {
  452. var grouped = [U: [T]]()
  453. for element in array {
  454. let key = callback(element)
  455. if var arr = grouped[key] {
  456. arr.append(element)
  457. grouped[key] = arr
  458. } else {
  459. grouped[key] = [element]
  460. }
  461. }
  462. return grouped
  463. }
  464. /// Gets the second element in the array.
  465. ///
  466. /// - parameter array: The array to wrap.
  467. /// - returns: Second element from the array.
  468. open class func second<T>(_ array: [T]) -> T? {
  469. if array.count < 2 {
  470. return .none
  471. } else {
  472. return array[1]
  473. }
  474. }
  475. /// Gets the third element in the array.
  476. ///
  477. /// - parameter array: The array to wrap.
  478. /// - returns: Third element from the array.
  479. open class func third<T>(_ array: [T]) -> T? {
  480. if array.count < 3 {
  481. return .none
  482. } else {
  483. return array[2]
  484. }
  485. }
  486. /// Flattens a nested array of any depth.
  487. ///
  488. /// - parameter array: The array to flatten.
  489. /// - returns: Flattened array.
  490. open class func flatten<T>(_ array: [T]) -> [T] {
  491. var resultArr: [T] = []
  492. for elem: T in array {
  493. if let val = elem as? [T] {
  494. resultArr += self.flatten(val)
  495. } else {
  496. resultArr.append(elem)
  497. }
  498. }
  499. return resultArr
  500. }
  501. /// Maps a function that converts elements to a list and then concatenates them.
  502. ///
  503. /// - parameter array: The array to map.
  504. /// - parameter function: callback function that should return flattened values
  505. /// - returns: The array with the transformed values concatenated together.
  506. open class func flatMap<T, U>(_ array: [T], function: (T) -> ([U])) -> [U] {
  507. return array.map(function).reduce([], +)
  508. }
  509. /// Maps a function that converts a type to an Optional over an Optional, and then returns a single-level Optional.
  510. ///
  511. /// - parameter value: The array to map.
  512. /// - parameter function: callback function that should return mapped values
  513. /// - returns: The array with the transformed values concatenated together.
  514. open class func flatMap<T, U>(_ value: T?, function: (T) -> (U?)) -> U? {
  515. if let unwrapped = value.map(function) {
  516. return unwrapped
  517. } else {
  518. return .none
  519. }
  520. }
  521. /// Returns size of the array
  522. ///
  523. /// - parameter array: The array to size.
  524. /// - returns: size of the array
  525. open class func size<T>(_ array: [T]) -> Int {
  526. return array.count
  527. }
  528. /// Randomly shuffles the elements of an array.
  529. ///
  530. /// - parameter array: The array to shuffle.
  531. /// - returns: Shuffled array
  532. open class func shuffle<T>(_ array: [T]) -> [T] {
  533. var newArr = self.copy(array)
  534. // Implementation of Fisher-Yates shuffle
  535. // http://en.wikipedia.org/wiki/Fisher-Yates_Shuffle
  536. for index in 0..<array.count {
  537. let randIndex = self.random(index)
  538. if index != randIndex {
  539. newArr.swapAt(index, randIndex)
  540. }
  541. }
  542. return newArr
  543. }
  544. /// This method returns a sum of the elements in the array
  545. ///
  546. /// - parameter array: The array whose elements needs to be added
  547. /// - returns: Sum of the elements in the array
  548. open class func sum<T: Numeric>(_ array: [T]) -> T {
  549. return self.reduce(array, initial: 0) { $0 + $1 }
  550. }
  551. /// This method returns a dictionary of values in an array mapping to the
  552. /// total number of occurrences in the array.
  553. ///
  554. /// - parameter array: The array to source from.
  555. /// - returns: Dictionary that contains the key generated from the element passed in the function.
  556. open class func frequencies<T>(_ array: [T]) -> [T: Int] {
  557. return self.frequencies(array) { $0 }
  558. }
  559. /// This method returns a dictionary of values in an array mapping to the
  560. /// total number of occurrences in the array. If passed a function it returns
  561. /// a frequency table of the results of the given function on the arrays elements.
  562. ///
  563. /// - parameter array: The array to source from.
  564. /// - parameter function: The function to get value of the key for each element to group by.
  565. /// - returns: Dictionary that contains the key generated from the element passed in the function.
  566. open class func frequencies<T, U>(_ array: [T], function: (T) -> U) -> [U: Int] {
  567. var result = [U: Int]()
  568. for elem in array {
  569. let key = function(elem)
  570. if let freq = result[key] {
  571. result[key] = freq + 1
  572. } else {
  573. result[key] = 1
  574. }
  575. }
  576. return result
  577. }
  578. /// GCD function return greatest common denominator
  579. ///
  580. /// - parameter first: number
  581. /// - parameter second: number
  582. /// - returns: Greatest common denominator
  583. open class func gcd(_ first: Int, _ second: Int) -> Int {
  584. var first = first
  585. var second = second
  586. while second != 0 {
  587. (first, second) = (second, first % second)
  588. }
  589. return Swift.abs(first)
  590. }
  591. /// LCM function return least common multiple
  592. ///
  593. /// - parameter first: number
  594. /// - parameter second: number
  595. /// - returns: Least common multiple
  596. open class func lcm(_ first: Int, _ second: Int) -> Int {
  597. return (first / Dollar.gcd(first, second)) * second
  598. }
  599. /// The identity function. Returns the argument it is given.
  600. ///
  601. /// - parameter arg: Value to return
  602. /// - returns: Argument that was passed
  603. open class func id<T>(_ arg: T) -> T {
  604. return arg
  605. }
  606. /// Gets the index at which the first occurrence of value is found.
  607. ///
  608. /// - parameter array: The array to source from.
  609. /// - parameter value: Value whose index needs to be found.
  610. /// - returns: Index of the element otherwise returns nil if not found.
  611. open class func indexOf<T: Equatable>(_ array: [T], value: T) -> Int? {
  612. return self.findIndex(array) { $0 == value }
  613. }
  614. /// Gets all but the last element or last n elements of an array.
  615. ///
  616. /// - parameter array: The array to source from.
  617. /// - parameter numElements: The number of elements to ignore in the end.
  618. /// - returns: Array of initial values.
  619. open class func initial<T>(_ array: [T], numElements: Int = 1) -> [T] {
  620. var result: [T] = []
  621. if array.count > numElements && numElements >= 0 {
  622. for index in 0..<(array.count - numElements) {
  623. result.append(array[index])
  624. }
  625. }
  626. return result
  627. }
  628. /// Creates an array of unique values present in all provided arrays.
  629. ///
  630. /// - parameter arrays: The arrays to perform an intersection on.
  631. /// - returns: Intersection of all arrays passed.
  632. open class func intersection<T: Hashable>(_ arrays: [T]...) -> [T] {
  633. var map: [T: Int] = [T: Int]()
  634. for arr in arrays {
  635. for elem in arr {
  636. if let val: Int = map[elem] {
  637. map[elem] = val + 1
  638. } else {
  639. map[elem] = 1
  640. }
  641. }
  642. }
  643. var result: [T] = []
  644. let count = arrays.count
  645. for (key, value) in map {
  646. if value == count {
  647. result.append(key)
  648. }
  649. }
  650. return result
  651. }
  652. /// Returns true if i is in range
  653. ///
  654. /// - parameter index: to check if it is in range
  655. /// - parameter isIn: to check in
  656. /// - returns: true if it is in range otherwise false
  657. open class func it<T>(_ index: T, isIn range: Range<T>) -> Bool {
  658. return index >= range.lowerBound && index < range.upperBound
  659. }
  660. /// Joins the elements in the array to create a concatenated element of the same type.
  661. ///
  662. /// - parameter array: The array to join the elements of.
  663. /// - parameter separator: The separator to join the elements with.
  664. /// - returns: Joined element from the array of elements.
  665. open class func join(_ array: [String], separator: String) -> String {
  666. return array.joined(separator: separator)
  667. }
  668. /// Creates an array of keys given a dictionary.
  669. ///
  670. /// - parameter dictionary: The dictionary to source from.
  671. /// - returns: Array of keys from dictionary.
  672. open class func keys<T, U>(_ dictionary: [T: U]) -> [T] {
  673. var result: [T] = []
  674. for key in dictionary.keys {
  675. result.append(key)
  676. }
  677. return result
  678. }
  679. /// Gets the last element from the array.
  680. ///
  681. /// - parameter array: The array to source from.
  682. /// - returns: Last element from the array.
  683. open class func last<T>(_ array: [T]) -> T? {
  684. if array.isEmpty {
  685. return .none
  686. } else {
  687. return array[array.count - 1]
  688. }
  689. }
  690. /// Gets the index at which the last occurrence of value is found.
  691. ///
  692. /// - parameter array: The array to source from.
  693. /// - parameter value: The value whose last index needs to be found.
  694. /// - returns: Last index of element if found otherwise returns nil.
  695. open class func lastIndexOf<T: Equatable>(_ array: [T], value: T) -> Int? {
  696. return self.findLastIndex(array) { $0 == value }
  697. }
  698. /// Maps each element to new value based on the map function passed
  699. ///
  700. /// - parameter collection: The collection to source from
  701. /// - parameter transform: The mapping function
  702. /// - returns: Array of elements mapped using the map function
  703. open class func map<T: Collection, E>(_ collection: T, transform: (T.Iterator.Element) -> E) -> [E] {
  704. return collection.map(transform)
  705. }
  706. /// Maps each element to new value based on the map function passed
  707. ///
  708. /// - parameter sequence: The sequence to source from
  709. /// - parameter transform: The mapping function
  710. /// - returns: Array of elements mapped using the map function
  711. open class func map<T: Sequence, E>(_ sequence: T, transform: (T.Iterator.Element) -> E) -> [E] {
  712. return sequence.map(transform)
  713. }
  714. /// Retrieves the maximum value in an array.
  715. ///
  716. /// - parameter array: The array to source from.
  717. /// - returns: Maximum element in array.
  718. open class func max<T: Comparable>(_ array: [T]) -> T? {
  719. if var maxVal = array.first {
  720. for elem in array {
  721. if maxVal < elem {
  722. maxVal = elem
  723. }
  724. }
  725. return maxVal
  726. }
  727. return .none
  728. }
  729. /// Get memoized function to improve performance
  730. ///
  731. /// - parameter function: The function to memoize.
  732. /// - returns: Memoized function
  733. open class func memoize<T: Hashable, U>(_ function: @escaping (((T) -> U), T) -> U) -> ((T) -> U) {
  734. var cache = [T: U]()
  735. var funcRef: ((T) -> U)!
  736. funcRef = { (param: T) -> U in
  737. if let cacheVal = cache[param] {
  738. return cacheVal
  739. } else {
  740. cache[param] = function(funcRef, param)
  741. return cache[param]!
  742. }
  743. }
  744. return funcRef
  745. }
  746. /// Merge dictionaries together, later dictionaries overiding earlier values of keys.
  747. ///
  748. /// - parameter dictionaries: The dictionaries to source from.
  749. /// - returns: Merged dictionary with all of its keys and values.
  750. open class func merge<T, U>(_ dictionaries: [T: U]...) -> [T: U] {
  751. var result = [T: U]()
  752. for dict in dictionaries {
  753. for (key, value) in dict {
  754. result[key] = value
  755. }
  756. }
  757. return result
  758. }
  759. /// Merge arrays together in the supplied order.
  760. ///
  761. /// - parameter arrays: The arrays to source from.
  762. /// - returns: Array with all values merged, including duplicates.
  763. open class func merge<T>(_ arrays: [T]...) -> [T] {
  764. var result = [T]()
  765. for arr in arrays {
  766. result += arr
  767. }
  768. return result
  769. }
  770. /// Retrieves the minimum value in an array.
  771. ///
  772. /// - parameter array: The array to source from.
  773. /// - returns: Minimum value from array.
  774. open class func min<T: Comparable>(_ array: [T]) -> T? {
  775. if var minVal = array.first {
  776. for elem in array {
  777. if minVal > elem {
  778. minVal = elem
  779. }
  780. }
  781. return minVal
  782. }
  783. return .none
  784. }
  785. /// A no-operation function.
  786. ///
  787. /// - returns: nil.
  788. open class func noop() -> () {
  789. }
  790. /// Gets the number of seconds that have elapsed since the Unix epoch (1 January 1970 00:00:00 UTC).
  791. ///
  792. /// - returns: number of seconds as double
  793. open class func now() -> TimeInterval {
  794. return Date().timeIntervalSince1970
  795. }
  796. /// Creates a shallow clone of a dictionary excluding the specified keys.
  797. ///
  798. /// - parameter dictionary: The dictionary to source from.
  799. /// - parameter keys: The keys to omit from returning dictionary.
  800. /// - returns: Dictionary with the keys specified omitted.
  801. open class func omit<T, U>(_ dictionary: [T: U], keys: T...) -> [T: U] {
  802. var result: [T: U] = [T: U]()
  803. for (key, value) in dictionary {
  804. if !self.contains(keys, value: key) {
  805. result[key] = value
  806. }
  807. }
  808. return result
  809. }
  810. /// Get a wrapper function that executes the passed function only once
  811. ///
  812. /// - parameter function: That takes variadic arguments and return nil or some value
  813. /// - returns: Wrapper function that executes the passed function only once
  814. /// Consecutive calls will return the value returned when calling the function first time
  815. open class func once<T, U>(_ function: @escaping (T...) -> U) -> (T...) -> U {
  816. var result: U?
  817. let onceFunc = { (params: T...) -> U in
  818. typealias Function = ([T]) -> U
  819. if let returnVal = result {
  820. return returnVal
  821. } else {
  822. let f = unsafeBitCast(function, to: Function.self)
  823. result = f(params)
  824. return result!
  825. }
  826. }
  827. return onceFunc
  828. }
  829. /// Get a wrapper function that executes the passed function only once
  830. ///
  831. /// - parameter function: That takes variadic arguments and return nil or some value
  832. /// - returns: Wrapper function that executes the passed function only once
  833. /// Consecutive calls will return the value returned when calling the function first time
  834. open class func once<U>(_ function: @escaping () -> U) -> () -> U {
  835. var result: U?
  836. let onceFunc = { () -> U in
  837. if let returnVal = result {
  838. return returnVal
  839. } else {
  840. result = function()
  841. return result!
  842. }
  843. }
  844. return onceFunc
  845. }
  846. /// Creates a function that, when called, invokes func with any additional partial arguments prepended to those provided to the new function.
  847. ///
  848. /// - parameter function: to invoke
  849. /// - parameter parameters: to pass the function when invoked
  850. /// - returns: Function with partial arguments prepended
  851. open class func partial<T, E> (_ function: @escaping (T...) -> E, _ parameters: T...) -> ((T...) -> E) {
  852. return { (params: T...) -> E in
  853. typealias Function = ([T]) -> E
  854. let f = unsafeBitCast(function, to: Function.self)
  855. return f(parameters + params)
  856. }
  857. }
  858. /// Produces an array of arrays, each containing n elements, each offset by step.
  859. /// If the final partition is not n elements long it is dropped.
  860. ///
  861. /// - parameter array: The array to partition.
  862. /// - parameter n: The number of elements in each partition.
  863. /// - parameter step: The number of elements to progress between each partition. Set to n if not supplied.
  864. /// - returns: Array partitioned into n element arrays, starting step elements apart.
  865. open class func partition<T>(_ array: [T], n num: Int, step: Int? = .none) -> [[T]] {
  866. var num = num
  867. var step = step
  868. var result = [[T]]()
  869. if step == .none { step = num } // If no step is supplied move n each step.
  870. if step < 1 { step = 1 } // Less than 1 results in an infinite loop.
  871. if num < 1 { num = 0 } // Allow 0 if user wants [[],[],[]] for some reason.
  872. if num > array.count { return [[]] }
  873. for i in self.range(from: 0, through: array.count - num, incrementBy: step!) {
  874. result.append(Array(array[i..<(i + num)] as ArraySlice<T>))
  875. }
  876. return result
  877. }
  878. /// Produces an array of arrays, each containing n elements, each offset by step.
  879. ///
  880. /// - parameter array: The array to partition.
  881. /// - parameter n: The number of elements in each partition.
  882. /// - parameter step: The number of elements to progress between each partition. Set to n if not supplied.
  883. /// - parameter pad: An array of elements to pad the last partition if it is not long enough to
  884. /// contain n elements. If there are not enough pad elements
  885. /// the last partition may less than n elements long.
  886. /// - returns: Array partitioned into n element arrays, starting step elements apart.
  887. open class func partition<T>(_ array: [T], n num: Int, step: Int? = .none, pad: [T]?) -> [[T]] {
  888. let array = array
  889. var num = num
  890. var step = step
  891. var result: [[T]] = []
  892. var need = 0
  893. if step == .none { step = num } // If no step is supplied move n each step.
  894. if step < 1 { step = 1 } // Less than 1 results in an infinite loop.
  895. if num < 1 { num = 0 } // Allow 0 if user wants [[],[],[]] for some reason.
  896. for i in self.range(from: 0, to: array.count, incrementBy: step!) {
  897. var end = i + num
  898. if end > array.count { end = array.count }
  899. result.append(Array(array[i..<end] as ArraySlice<T>))
  900. if end != i + num { need = i + num - end; break }
  901. }
  902. if need != 0 {
  903. if let padding = pad {
  904. let end = padding.count > need ? need : padding.count
  905. result[result.count - 1] += Array(padding[0..<end] as ArraySlice<T>)
  906. }
  907. }
  908. return result
  909. }
  910. /// Produces an array of arrays, each containing n elements, each offset by step.
  911. ///
  912. /// - parameter array: The array to partition.
  913. /// - parameter n: The number of elements in each partition.
  914. /// - parameter step: The number of elements to progress between each partition. Set to n if not supplied.
  915. /// - returns: Array partitioned into n element arrays, starting step elements apart.
  916. open class func partitionAll<T>(_ array: [T], n num: Int, step: Int? = .none) -> [[T]] {
  917. var num = num
  918. var step = step
  919. var result = [[T]]()
  920. if step == .none { step = num } // If no step is supplied move n each step.
  921. if step < 1 { step = 1 } // Less than 1 results in an infinite loop.
  922. if num < 1 { num = 0 } // Allow 0 if user wants [[],[],[]] for some reason.
  923. for i in self.range(from: 0, to: array.count, incrementBy: step!) {
  924. var end = i + num
  925. if end > array.count { end = array.count }
  926. result.append(Array(array[i..<end] as ArraySlice<T>))
  927. }
  928. return result
  929. }
  930. /// Applies function to each element in array, splitting it each time function returns a new value.
  931. ///
  932. /// - parameter array: The array to partition.
  933. /// - parameter function: Function which takes an element and produces an equatable result.
  934. /// - returns: Array partitioned in order, splitting via results of function.
  935. open class func partitionBy<T, U: Equatable>(_ array: [T], function: (T) -> U) -> [[T]] {
  936. var result = [[T]]()
  937. var lastValue: U? = .none
  938. for item in array {
  939. let value = function(item)
  940. if let lastValue = lastValue, value == lastValue {
  941. result[result.count-1].append(item)
  942. } else {
  943. result.append([item])
  944. lastValue = value
  945. }
  946. }
  947. return result
  948. }
  949. /// Creates a shallow clone of a dictionary composed of the specified keys.
  950. ///
  951. /// - parameter dictionary: The dictionary to source from.
  952. /// - parameter keys: The keys to pick values from.
  953. /// - returns: Dictionary with the key and values picked from the keys specified.
  954. open class func pick<T, U>(_ dictionary: [T: U], keys: T...) -> [T: U] {
  955. var result: [T: U] = [T: U]()
  956. for key in keys {
  957. result[key] = dictionary[key]
  958. }
  959. return result
  960. }
  961. /// Retrieves the value of a specified property from all elements in the array.
  962. ///
  963. /// - parameter array: The array to source from.
  964. /// - parameter value: The property on object to pull out value from.
  965. /// - returns: Array of values from array of objects with property of value.
  966. open class func pluck<T, E>(_ array: [[T: E]], value: T) -> [E] {
  967. var result: [E] = []
  968. for obj in array {
  969. if let val = obj[value] {
  970. result.append(val)
  971. }
  972. }
  973. return result
  974. }
  975. /// Removes all provided values from the given array.
  976. ///
  977. /// - parameter array: The array to source from.
  978. /// - parameter values: values to remove from the array
  979. /// - returns: Array with values pulled out.
  980. open class func pull<T: Equatable>(_ array: [T], values: T...) -> [T] {
  981. return self.pull(array, values: values)
  982. }
  983. /// Removes all provided values from the given array.
  984. ///
  985. /// - parameter array: The array to source from.
  986. /// - parameter values: The values to remove.
  987. /// - returns: Array with values pulled out.
  988. open class func pull<T: Equatable>(_ array: [T], values: [T]) -> [T] {
  989. return array.filter { !self.contains(values, value: $0) }
  990. }
  991. /// Removes all provided values from the given array at the given indices
  992. ///
  993. /// - parameter array: The array to source from.
  994. /// - parameter indices: The indices to remove from.
  995. /// - returns: Array with values pulled out.
  996. open class func pullAt<T: Equatable>(_ array: [T], indices: Int...) -> [T] {
  997. var elemToRemove = [T]()
  998. for index in indices {
  999. elemToRemove.append(array[index])
  1000. }
  1001. return Dollar.pull(array, values: elemToRemove)
  1002. }
  1003. /// Returns permutation of array
  1004. ///
  1005. /// - parameter character: Characters to source the permutation
  1006. /// - returns: Array of permutation of the characters specified
  1007. open class func permutation<T>(_ elements: [T]) -> [String] where T : CustomStringConvertible {
  1008. guard elements.count > 1 else {
  1009. return Dollar.map(elements) { $0.description }
  1010. }
  1011. let strings = self.permutation(Dollar.initial(elements))
  1012. if let char = Dollar.last(elements) {
  1013. return Dollar.reduce(strings, initial: []) { (result, str) -> [String] in
  1014. let splitStr = Dollar.map(str.description) { $0.description }
  1015. return result + Dollar.map(0...splitStr.count) { (index) -> String in
  1016. var copy = Dollar.copy(splitStr)
  1017. copy.insert(char.description, at: (splitStr.count - index))
  1018. return Dollar.join(copy, separator: "")
  1019. }
  1020. }.sorted()
  1021. }
  1022. return []
  1023. }
  1024. /// Returns random number from 0 upto but not including upperBound
  1025. ///
  1026. /// - parameter upperBound: upper bound when generating random number
  1027. /// - returns: Random number
  1028. open class func random(_ upperBound: Int) -> Int {
  1029. #if os(Linux)
  1030. let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
  1031. srand(time)
  1032. let randomNumber = Glibc.random() % upperBound
  1033. #else
  1034. let randomNumber = Int(arc4random_uniform(UInt32(upperBound)))
  1035. #endif
  1036. return randomNumber
  1037. }
  1038. /// Creates an array of numbers (positive and/or negative) progressing from start up to but not including end.
  1039. ///
  1040. /// - parameter endVal: End value of range.
  1041. /// - returns: Array of elements based on the sequence starting from 0 to endVal and incremented by 1.
  1042. open class func range<T: Strideable>(_ endVal: T) -> [T] where T : ExpressibleByIntegerLiteral {
  1043. return self.range(from: 0, to: endVal)
  1044. }
  1045. /// Creates an array of numbers (positive and/or negative) progressing from start up to but not including end.
  1046. ///
  1047. /// - parameter from: Start value of range
  1048. /// - parameter to: End value of range
  1049. /// - returns: Array of elements based on the sequence that is incremented by 1
  1050. open class func range<T: Strideable>(from startVal: T, to endVal: T) -> [T] {
  1051. return self.range(from: startVal, to: endVal, incrementBy: 1)
  1052. }
  1053. /// Creates an array of numbers (positive and/or negative) progressing from start up to but not including end.
  1054. ///
  1055. /// - parameter from: Start value of range.
  1056. /// - parameter to: End value of range.
  1057. /// - parameter incrementBy: Increment sequence by.
  1058. /// - returns: Array of elements based on the sequence.
  1059. open class func range<T: Strideable>(from startVal: T, to endVal: T, incrementBy: T.Stride) -> [T] {
  1060. let range = stride(from: startVal, to: endVal, by: incrementBy)
  1061. return self.sequence(range)
  1062. }
  1063. /// Creates an array of numbers (positive and/or negative) progressing from start up to but not including end.
  1064. ///
  1065. /// - parameter from: Start value of range
  1066. /// - parameter through: End value of range
  1067. /// - returns: Array of elements based on the sequence that is incremented by 1
  1068. open class func range<T: Strideable>(from startVal: T, through endVal: T) -> [T] {
  1069. return self.range(from: startVal, to: endVal.advanced(by: 1), incrementBy: 1)
  1070. }
  1071. /// Creates an array of numbers (positive and/or negative) progressing from start up to but not including end.
  1072. ///
  1073. /// - parameter from: Start value of range.
  1074. /// - parameter through: End value of range.
  1075. /// - parameter incrementBy: Increment sequence by.
  1076. /// - returns: Array of elements based on the sequence.
  1077. open class func range<T: Strideable>(from startVal: T, through endVal: T, incrementBy: T.Stride) -> [T] {
  1078. return self.range(from: startVal, to: endVal.advanced(by: 1), incrementBy: incrementBy)
  1079. }
  1080. /// Reduce function that will resolve to one value after performing combine function on all elements
  1081. ///
  1082. /// - parameter array: The array to source from.
  1083. /// - parameter initial: Initial value to seed the reduce function with
  1084. /// - parameter combine: Function that will combine the passed value with element in the array
  1085. /// - returns: The result of reducing all of the elements in the array into one value
  1086. open class func reduce<U, T>(_ array: [T], initial: U, combine: (U, T) -> U) -> U {
  1087. return array.reduce(initial, combine)
  1088. }
  1089. /// Creates an array of an arbitrary sequence. Especially useful with builtin ranges.
  1090. ///
  1091. /// - parameter seq: The sequence to generate from.
  1092. /// - returns: Array of elements generated from the sequence.
  1093. open class func sequence<S: Sequence>(_ seq: S) -> [S.Iterator.Element] {
  1094. return Array<S.Iterator.Element>(seq)
  1095. }
  1096. /// Removes all elements from an array that the callback returns true.
  1097. ///
  1098. /// - parameter array: The array to wrap.
  1099. /// - parameter callback: Remove elements for which callback returns true.
  1100. /// - returns: Array with elements filtered out.
  1101. open class func remove<T>(_ array: [T], callback: (T) -> Bool) -> [T] {
  1102. return array.filter { !callback($0) }
  1103. }
  1104. /// Removes an element from an array.
  1105. ///
  1106. /// - parameter array: The array to source from.
  1107. /// - parameter value: Element that is to be removed
  1108. /// - returns: Array with element removed.
  1109. open class func remove<T: Equatable>(_ array: [T], value: T) -> [T] {
  1110. return self.remove(array, callback: {$0 == value})
  1111. }
  1112. /// The opposite of initial this method gets all but the first element or first n elements of an array.
  1113. ///
  1114. /// - parameter array: The array to source from.
  1115. /// - parameter numElements: The number of elements to exclude from the beginning.
  1116. /// - returns: The rest of the elements.
  1117. open class func rest<T>(_ array: [T], numElements: Int = 1) -> [T] {
  1118. var result: [T] = []
  1119. if numElements < array.count && numElements >= 0 {
  1120. for index in numElements..<array.count {
  1121. result.append(array[index])
  1122. }
  1123. }
  1124. return result
  1125. }
  1126. /// Returns a sample from the array.
  1127. ///
  1128. /// - parameter array: The array to sample from.
  1129. /// - returns: Random element from array.
  1130. open class func sample<T>(_ array: [T]) -> T {
  1131. return array[self.random(array.count)]
  1132. }
  1133. /// Slices the array based on the start and end position. If an end position is not specified it will slice till the end of the array.
  1134. ///
  1135. /// - parameter array: The array to slice.
  1136. /// - parameter start: Start index.
  1137. /// - parameter end: End index.
  1138. /// - returns: First element from the array.
  1139. open class func slice<T>(_ array: [T], start: Int, end: Int = 0) -> [T] {
  1140. var uend = end
  1141. if uend == 0 {
  1142. uend = array.count
  1143. }
  1144. if end > array.count || start > array.count || uend < start {
  1145. return []
  1146. } else {
  1147. return Array(array[start..<uend])
  1148. }
  1149. }
  1150. /// Gives the smallest index at which a value should be inserted into a given the array is sorted.
  1151. ///
  1152. /// - parameter array: The array to source from.
  1153. /// - parameter value: Find sorted index of this value.
  1154. /// - returns: Index of where the elemnt should be inserted.
  1155. open class func sortedIndex<T: Comparable>(_ array: [T], value: T) -> Int {
  1156. for (index, elem) in array.enumerated() {
  1157. if elem > value {
  1158. return index
  1159. }
  1160. }
  1161. return array.count
  1162. }
  1163. /// Invokes interceptor with the object and then returns object.
  1164. ///
  1165. /// - parameter object: Object to tap into.
  1166. /// - parameter function: Callback function to invoke.
  1167. /// - returns: Returns the object back.
  1168. open class func tap<T>(_ object: T, function: (T) -> ()) -> T {
  1169. function(object)
  1170. return object
  1171. }
  1172. /// Call a function n times and also passes the index. If a value is returned
  1173. /// in the function then the times method will return an array of those values.
  1174. ///
  1175. /// - parameter num: Number of times to call function.
  1176. /// - parameter function: The function to be called every time.
  1177. /// - returns: Values returned from callback function.
  1178. open class func times<T>(_ num: Int, function: () -> T) -> [T] {
  1179. return self.times(num) { (index: Int) -> T in
  1180. return function()
  1181. }
  1182. }
  1183. /// Call a function n times and also passes the index. If a value is returned
  1184. /// in the function then the times method will return an array of those values.
  1185. ///
  1186. /// - parameter num: Number of times to call function
  1187. /// - parameter function: The function to be called every time
  1188. open class func times(_ num: Int, function: @escaping () -> Void) {
  1189. _ = self.times(num) { (index: Int) -> () in
  1190. function()
  1191. }
  1192. }
  1193. /// Call a function n times and also passes the index. If a value is returned
  1194. /// in the function then the times method will return an array of those values.
  1195. ///
  1196. /// - parameter num: Number of times to call function.
  1197. /// - parameter function: The function to be called every time that takes index.
  1198. /// - returns: Values returned from callback function.
  1199. open class func times<T>(_ num: Int, function: (Int) -> T) -> [T] {
  1200. var result: [T] = []
  1201. for index in (0..<num) {
  1202. result.append(function(index))
  1203. }
  1204. return result
  1205. }
  1206. /// Transposes matrix if able. If unable; parameter matrix is returned.
  1207. ///
  1208. /// - parameter matrix: Generic matrix containing any type.
  1209. /// - returns: A transposed version of input matrix.
  1210. open class func transpose<T>(_ matrix: [[T]]) -> [[T]] {
  1211. guard matrix.filter({ return $0.count == matrix[0].count }).count == matrix.count else {
  1212. return matrix
  1213. }
  1214. var returnMatrix: [[T?]] = Array(repeating: Array(repeating: nil, count: matrix.count),
  1215. count: matrix.first!.count)
  1216. for (rowNumber, row) in matrix.enumerated() {
  1217. for (index, item) in row.enumerated() {
  1218. returnMatrix[index][rowNumber] = item
  1219. }
  1220. }
  1221. return returnMatrix.compactMap { $0.compactMap { $0 } }
  1222. }
  1223. /// Creates an array of unique values, in order, of the provided arrays.
  1224. ///
  1225. /// - parameter arrays: The arrays to perform union on.
  1226. /// - returns: Resulting array after union.
  1227. open class func union<T: Hashable>(_ arrays: [T]...) -> [T] {
  1228. var result: [T] = []
  1229. for arr in arrays {
  1230. result += arr
  1231. }
  1232. return self.uniq(result)
  1233. }
  1234. /// Creates a duplicate-value-free version of an array.
  1235. ///
  1236. /// - parameter array: The array to source from.
  1237. /// - returns: An array with unique values.
  1238. open class func uniq<T: Hashable>(_ array: [T]) -> [T] {
  1239. var result: [T] = []
  1240. var map: [T: Bool] = [T: Bool]()
  1241. for elem in array {
  1242. if map[elem] == .none {
  1243. result.append(elem)
  1244. }
  1245. map[elem] = true
  1246. }
  1247. return result
  1248. }
  1249. /// Create a duplicate-value-free version of an array based on the condition.
  1250. /// Uses the last value generated by the condition function
  1251. ///
  1252. /// - parameter array: The array to source from.
  1253. /// - parameter condition: Called per iteration
  1254. /// - returns: An array with unique values.
  1255. open class func uniq<T: Hashable, U: Hashable>(_ array: [T], by condition: (T) -> U) -> [T] {
  1256. var result: [T] = []
  1257. var map: [U: Bool] = [U: Bool]()
  1258. for elem in array {
  1259. let val = condition(elem)
  1260. if map[val] == .none {
  1261. result.append(elem)
  1262. }
  1263. map[val] = true
  1264. }
  1265. return result
  1266. }
  1267. /// Creates an array of values of a given dictionary.
  1268. ///
  1269. /// - parameter dictionary: The dictionary to source from.
  1270. /// - returns: An array of values from the dictionary.
  1271. open class func values<T, U>(_ dictionary: [T: U]) -> [U] {
  1272. var result: [U] = []
  1273. for value in dictionary.values {
  1274. result.append(value)
  1275. }
  1276. return result
  1277. }
  1278. /// Creates an array excluding all provided values.
  1279. ///
  1280. /// - parameter array: The array to source from.
  1281. /// - parameter values: Values to exclude.
  1282. /// - returns: Array excluding provided values.
  1283. open class func without<T: Equatable>(_ array: [T], values: T...) -> [T] {
  1284. return self.pull(array, values: values)
  1285. }
  1286. /// Creates an array that is the symmetric difference of the provided arrays.
  1287. ///
  1288. /// - parameter arrays: The arrays to perform xor on in order.
  1289. /// - returns: Resulting array after performing xor.
  1290. open class func xor<T: Hashable>(_ arrays: [T]...) -> [T] {
  1291. var map: [T: Bool] = [T: Bool]()
  1292. for arr in arrays {
  1293. for elem in arr {
  1294. map[elem] = !(map[elem] ?? false)
  1295. }
  1296. }
  1297. var result: [T] = []
  1298. for (key, value) in map {
  1299. if value {
  1300. result.append(key)
  1301. }
  1302. }
  1303. return result
  1304. }
  1305. /// Creates an array of grouped elements, the first of which contains the first elements
  1306. /// of the given arrays.
  1307. ///
  1308. /// - parameter arrays: The arrays to be grouped.
  1309. /// - returns: An array of grouped elements.
  1310. open class func zip<T>(_ arrays: [T]...) -> [[T]] {
  1311. var result: [[T]] = []
  1312. for _ in self.first(arrays)! as [T] {
  1313. result.append([] as [T])
  1314. }
  1315. for (_, array) in arrays.enumerated() {
  1316. for (elemIndex, elem): (Int, T) in array.enumerated() {
  1317. result[elemIndex].append(elem)
  1318. }
  1319. }
  1320. return result
  1321. }
  1322. /// Creates an object composed from arrays of keys and values.
  1323. ///
  1324. /// - parameter keys: The array of keys.
  1325. /// - parameter values: The array of values.
  1326. /// - returns: Dictionary based on the keys and values passed in order.
  1327. open class func zipObject<T, E>(_ keys: [T], values: [E]) -> [T: E] {
  1328. var result = [T: E]()
  1329. for (index, key) in keys.enumerated() {
  1330. result[key] = values[index]
  1331. }
  1332. return result
  1333. }
  1334. /// Returns the collection wrapped in the chain object
  1335. ///
  1336. /// - parameter collection: of elements
  1337. /// - returns: Chain object
  1338. open class func chain<T>(_ collection: [T]) -> Chain<T> {
  1339. return Chain(collection)
  1340. }
  1341. }
  1342. // ________ ___ ___ ________ ___ ________
  1343. // |\ ____\|\ \|\ \|\ __ \|\ \|\ ___ \
  1344. // \ \ \___|\ \ \\\ \ \ \|\ \ \ \ \ \\ \ \
  1345. // \ \ \ \ \ __ \ \ __ \ \ \ \ \\ \ \
  1346. // \ \ \____\ \ \ \ \ \ \ \ \ \ \ \ \\ \ \
  1347. // \ \_______\ \__\ \__\ \__\ \__\ \__\ \__\\ \__\
  1348. // \|_______|\|__|\|__|\|__|\|__|\|__|\|__| \|__|
  1349. //
  1350. open class Chain<C> {
  1351. fileprivate var result: Wrapper<[C]>
  1352. fileprivate var funcQueue: [(Wrapper<[C]>) -> Wrapper<[C]>] = []
  1353. open var value: [C] {
  1354. get {
  1355. var result: Wrapper<[C]> = self.result
  1356. for function in self.funcQueue {
  1357. result = function(result)
  1358. }
  1359. return result.value
  1360. }
  1361. }
  1362. /// Initializer of the wrapper object for chaining.
  1363. ///
  1364. /// - parameter collection: The array to wrap.
  1365. public init(_ collection: [C]) {
  1366. self.result = Wrapper(collection)
  1367. }
  1368. /// Get the first object in the wrapper object.
  1369. ///
  1370. /// - returns: First element from the array.
  1371. open func first() -> C? {
  1372. return Dollar.first(self.value)
  1373. }
  1374. /// Get the second object in the wrapper object.
  1375. ///
  1376. /// - returns: Second element from the array.
  1377. open func second() -> C? {
  1378. return Dollar.second(self.value)
  1379. }
  1380. /// Get the third object in the wrapper object.
  1381. ///
  1382. /// - returns: Third element from the array.
  1383. open func third() -> C? {
  1384. return Dollar.third(self.value)
  1385. }
  1386. /// Flattens nested array.
  1387. ///
  1388. /// - returns: The wrapper object.
  1389. open func flatten() -> Chain {
  1390. return self.queue {
  1391. return Wrapper(Dollar.flatten($0.value))
  1392. }
  1393. }
  1394. /// Keeps all the elements except last one.
  1395. ///
  1396. /// - returns: The wrapper object.
  1397. open func initial() -> Chain {
  1398. return self.initial(1)
  1399. }
  1400. /// Keeps all the elements except last n elements.
  1401. ///
  1402. /// - parameter numElements: Number of items to remove from the end of the array.
  1403. /// - returns: The wrapper object.
  1404. open func initial(_ numElements: Int) -> Chain {
  1405. return self.queue {
  1406. return Wrapper(Dollar.initial($0.value, numElements: numElements))
  1407. }
  1408. }
  1409. /// Maps elements to new elements.
  1410. ///
  1411. /// - parameter function: Function to map.
  1412. /// - returns: The wrapper object.
  1413. open func map(_ function: @escaping (C) -> C) -> Chain {
  1414. return self.queue {
  1415. var result: [C] = []
  1416. for elem: C in $0.value {
  1417. result.append(function(elem))
  1418. }
  1419. return Wrapper(result)
  1420. }
  1421. }
  1422. /// Get the first object in the wrapper object.
  1423. ///
  1424. /// - parameter function: The array to wrap.
  1425. /// - returns: The wrapper object.
  1426. open func map(_ function: @escaping (Int, C) -> C) -> Chain {
  1427. return self.queue {
  1428. var result: [C] = []
  1429. for (index, elem) in $0.value.enumerated() {
  1430. result.append(function(index, elem))
  1431. }
  1432. return Wrapper(result)
  1433. }
  1434. }
  1435. /// Get the first object in the wrapper object.
  1436. ///
  1437. /// - parameter function: The array to wrap.
  1438. /// - returns: The wrapper object.
  1439. open func each(_ function: @escaping (C) -> ()) -> Chain {
  1440. return self.queue {
  1441. for elem in $0.value {
  1442. function(elem)
  1443. }
  1444. return $0
  1445. }
  1446. }
  1447. /// Get the first object in the wrapper object.
  1448. ///
  1449. /// - parameter function: The array to wrap.
  1450. /// - returns: The wrapper object.
  1451. open func each(_ function: @escaping (Int, C) -> ()) -> Chain {
  1452. return self.queue {
  1453. for (index, elem) in $0.value.enumerated() {
  1454. function(index, elem)
  1455. }
  1456. return $0
  1457. }
  1458. }
  1459. /// Filter elements based on the function passed.
  1460. ///
  1461. /// - parameter function: Function to tell whether to keep an element or remove.
  1462. /// - returns: The wrapper object.
  1463. open func filter(_ function: @escaping (C) -> Bool) -> Chain {
  1464. return self.queue {
  1465. return Wrapper(($0.value).filter(function))
  1466. }
  1467. }
  1468. /// Returns if all elements in array are true based on the passed function.
  1469. ///
  1470. /// - parameter function: Function to tell whether element value is true or false.
  1471. /// - returns: Whether all elements are true according to func function.
  1472. open func all(_ function: (C) -> Bool) -> Bool {
  1473. return Dollar.every(self.value, callback: function)
  1474. }
  1475. /// Returns if any element in array is true based on the passed function.
  1476. ///
  1477. /// - parameter function: Function to tell whether element value is true or false.
  1478. /// - returns: Whether any one element is true according to func function in the array.
  1479. open func any(_ function: (C) -> Bool) -> Bool {
  1480. let resultArr = self.value
  1481. for elem in resultArr {
  1482. if function(elem) {
  1483. return true
  1484. }
  1485. }
  1486. return false
  1487. }
  1488. /// Returns size of the array
  1489. ///
  1490. /// - returns: The wrapper object.
  1491. open func size() -> Int {
  1492. return self.value.count
  1493. }
  1494. /// Slice the array into smaller size based on start and end value.
  1495. ///
  1496. /// - parameter start: Start index to start slicing from.
  1497. /// - parameter end: End index to stop slicing to and not including element at that index.
  1498. /// - returns: The wrapper object.
  1499. open func slice(_ start: Int, end: Int = 0) -> Chain {
  1500. return self.queue {
  1501. return Wrapper(Dollar.slice($0.value, start: start, end: end))
  1502. }
  1503. }
  1504. fileprivate func queue(_ function: @escaping (Wrapper<[C]>) -> Wrapper<[C]>) -> Chain {
  1505. funcQueue.append(function)
  1506. return self
  1507. }
  1508. }
  1509. typealias `$` = Dollar
  1510. typealias € = Dollar
  1511. private struct Wrapper<V> {
  1512. let value: V
  1513. init(_ value: V) {
  1514. self.value = value
  1515. }
  1516. }
  1517. fileprivate func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
  1518. switch (lhs, rhs) {
  1519. case let (l?, r?):
  1520. return l < r
  1521. case (nil, _?):
  1522. return true
  1523. default:
  1524. return false
  1525. }
  1526. }