mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-05 23:38:38 +09:00
Dactyloidae iOS initial commit
This commit is contained in:
parent
daa6179d22
commit
7154a0497e
2123 changed files with 197052 additions and 0 deletions
70
mobile/ios/Shared/Extensions/ArrayExtensions.swift
Normal file
70
mobile/ios/Shared/Extensions/ArrayExtensions.swift
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Array where Element: Comparable {
|
||||
func sameElements(_ arr: [Element]) -> Bool {
|
||||
guard self.count == arr.count else { return false }
|
||||
let sorted = self.sorted(by: <)
|
||||
let arrSorted = arr.sorted(by: <)
|
||||
for elements in sorted.zip(arrSorted) where elements.0 != elements.1 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
public extension Array {
|
||||
|
||||
func find(_ f: (Iterator.Element) -> Bool) -> Iterator.Element? {
|
||||
for x in self {
|
||||
if f(x) {
|
||||
return x
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func contains(_ x: Element, f: (Element, Element) -> Bool) -> Bool {
|
||||
for y in self {
|
||||
if f(x, y) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Performs a union operator using the result of f(Element) as the value to base uniqueness on.
|
||||
func union<T: Hashable>(_ arr: [Element], f: ((Element) -> T)) -> [Element] {
|
||||
let result = self + arr
|
||||
return result.unique(f)
|
||||
}
|
||||
|
||||
// Returns unique values in an array using the result of f()
|
||||
func unique<T: Hashable>(_ f: ((Element) -> T)) -> [Element] {
|
||||
var map: [T: Element] = [T: Element]()
|
||||
return self.flatMap { a in
|
||||
let t = f(a)
|
||||
if map[t] == nil {
|
||||
map[t] = a
|
||||
return a
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public extension Sequence {
|
||||
func every(_ f: (Self.Iterator.Element) -> Bool) -> Bool {
|
||||
for x in self {
|
||||
if !f(x) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue