From ab97519194ca2fb1e7243da530716d3d4ff8e2a3 Mon Sep 17 00:00:00 2001 From: Gleb Fandeev Date: Thu, 11 Jul 2024 00:50:04 +0300 Subject: [PATCH] Re-Indent + refactor --- Sources/compose.swift | 1 - Sources/curry.swift | 16 +- Sources/internal/MemoizedKeys.swift | 51 ------ Sources/internal/ParameterPacksHelpers.swift | 21 --- Sources/memoized.swift | 155 ------------------- Sources/uncurry.swift | 6 +- Sources/weakify.swift | 113 +++++++------- 7 files changed, 68 insertions(+), 295 deletions(-) delete mode 100644 Sources/internal/MemoizedKeys.swift delete mode 100644 Sources/internal/ParameterPacksHelpers.swift delete mode 100644 Sources/memoized.swift diff --git a/Sources/compose.swift b/Sources/compose.swift index 9dc77f8..19794dc 100644 --- a/Sources/compose.swift +++ b/Sources/compose.swift @@ -143,4 +143,3 @@ func compose( ) -> (Arg1) throws -> Arg4 { { (arg1: Arg1) -> Arg4 in try f3(f2(f1(arg1))) } } - diff --git a/Sources/curry.swift b/Sources/curry.swift index bb4032d..580e433 100644 --- a/Sources/curry.swift +++ b/Sources/curry.swift @@ -13,7 +13,7 @@ /// - f: The function to curry. /// /// - Returns: Curried function -@inlinable +@inlinable @inline(__always) public func curry( _ f: @escaping (Arg1) -> Return ) -> (Arg1) -> Return { @@ -22,25 +22,25 @@ public func curry( } } -@inlinable +@inlinable @inline(__always) public func curry( _ f: @escaping ((Arg1, Arg2)) -> Return ) -> (Arg1) -> (Arg2) -> Return { - { (arg1: Arg1) -> (Arg2) -> Return in + { (arg1: Arg1) -> (Arg2) -> Return in { (arg2: Arg2) -> Return in f((arg1, arg2)) } } } -@inlinable +@inlinable @inline(__always) public func curry( _ f: @escaping ((Arg1, Arg2, Arg3)) -> Return ) -> (Arg1) -> (Arg2) -> (Arg3) -> Return { - { - (arg1: Arg1) -> (Arg2) -> (Arg3) -> Return in { - (arg2: Arg2) -> (Arg3) -> Return in { - (arg3: Arg3) -> Return in f((arg1, arg2, arg3)) + { (arg1: Arg1) -> (Arg2) -> (Arg3) -> Return in + { (arg2: Arg2) -> (Arg3) -> Return in + { (arg3: Arg3) -> Return in + f((arg1, arg2, arg3)) } } } diff --git a/Sources/internal/MemoizedKeys.swift b/Sources/internal/MemoizedKeys.swift deleted file mode 100644 index 552b119..0000000 --- a/Sources/internal/MemoizedKeys.swift +++ /dev/null @@ -1,51 +0,0 @@ -// MARK: iOS 17.0.0, macOS 14.0.0, watchOS 10.0.0 and above - -@available(iOS 17.0.0, macOS 14.0.0, watchOS 10.0.0, *) -@usableFromInline -internal struct Key: Hashable { - @usableFromInline - internal let arg: (repeat each Arg) - - @usableFromInline - init(arg: (repeat each Arg)) { - self.arg = arg - } - - @inlinable - static func == (lhs: Key, rhs: Key) -> Bool { - (repeat each lhs.arg) == (repeat each rhs.arg) - } - - @inlinable - func hash(into hasher: inout Hasher) { - repeat hasher.combine(each arg) - } -} - -// MARK: iOS 12..<17, macOS 10.13.0..<14.0.0, watchOS 4..<10 - -@usableFromInline -internal struct Key2: Hashable { - let arg1: Arg1 - let arg2: Arg2 - - @usableFromInline - init(arg1: Arg1, arg2: Arg2) { - self.arg1 = arg1 - self.arg2 = arg2 - } -} - -@usableFromInline -internal struct Key3: Hashable { - let arg1: Arg1 - let arg2: Arg2 - let arg3: Arg3 - - @usableFromInline - init(arg1: Arg1, arg2: Arg2, arg3: Arg3) { - self.arg1 = arg1 - self.arg2 = arg2 - self.arg3 = arg3 - } -} diff --git a/Sources/internal/ParameterPacksHelpers.swift b/Sources/internal/ParameterPacksHelpers.swift deleted file mode 100644 index 5e05d8f..0000000 --- a/Sources/internal/ParameterPacksHelpers.swift +++ /dev/null @@ -1,21 +0,0 @@ -// https://github.com/apple/swift-evolution/blob/main/proposals/0408-pack-iteration.md -struct NotEqual: Error {} - -@usableFromInline -internal func == (lhs: (repeat each Element), rhs: (repeat each Element)) -> Bool { - func isEqual(_ left: T, _ right: T) throws { - if left == right { - return - } - - throw NotEqual() - } - - do { - repeat try isEqual(each lhs, each rhs) - } catch { - return false - } - - return true -} diff --git a/Sources/memoized.swift b/Sources/memoized.swift deleted file mode 100644 index d0130dc..0000000 --- a/Sources/memoized.swift +++ /dev/null @@ -1,155 +0,0 @@ -// MARK: iOS 17.0.0, macOS 14.0.0, watchOS 10.0.0 and above - -/// Memoization optimisation technique where the return values of -/// a function are cached to avoid repeating the same computation. -/// Example usage: -/// -/// func fibonacci(_ number: Int) -> Int { -/// number < 2 -/// ? number -/// : fibonacci(number - 1) + fibonacci(number - 2) -/// } -/// -/// print(fibonacci(40) // Very slow... O(e^n) complexity -/// print(memoized(fibonacci(40)) // O(n) complexity -/// -/// - Parameters: -/// - f: A function whose result is to be memoized. -/// -/// - Returns: Function with memoization -@available(iOS 17.0.0, macOS 14.0.0, watchOS 10.0.0, *) -@inlinable -public func memoized( - _ f: @escaping ((repeat each Arg) -> Return) -) -> (repeat each Arg) -> Return { - var cache = [Key: Return]() - // Parameter packs workaround - let get: (Key) -> Return? = { cache[$0] } - let set: (Key, Return) -> Void = { cache[$0] = $1 } - return { (arg: repeat each Arg) -> Return in - let key = Key(arg: (repeat each arg)) - if let cached = get(key) { - return cached - } - let value = f(repeat each arg) - set(key, value) - return value - } -} - -@available(iOS 17.0.0, macOS 14.0.0, watchOS 10.0.0, *) -@inlinable -public func memoized( - _ f: @escaping ((repeat each Arg) throws -> Return) -) -> (repeat each Arg) throws -> Return { - var cache = [Key: Return]() - // Parameter packs workaround - let get: (Key) -> Return? = { cache[$0] } - let set: (Key, Return) -> Void = { cache[$0] = $1 } - return { (arg: repeat each Arg) -> Return in - let key = Key(arg: (repeat each arg)) - if let cached = get(key) { - return cached - } - let value = try f(repeat each arg) - set(key, value) - return value - } -} - -// MARK: iOS 12..<17, macOS 10.13.0..<14.0.0, watchOS 4..<10 - -@inlinable -public func memoized( - _ f: @escaping ((Arg) -> Return) -) -> (Arg) -> Return { - var cache = [Arg: Return]() - return { (arg: Arg) -> Return in - if let cached = cache[arg] { - return cached - } - let value = f(arg) - cache[arg] = value - return value - } -} - -@inlinable -public func memoized( - _ f: @escaping ((Arg1, Arg2) -> Return) -) -> (Arg1, Arg2) -> Return { - var cache = [Key2: Return]() - return { (arg1: Arg1, arg2: Arg2) -> Return in - let key = Key2(arg1: arg1, arg2: arg2) - if let cached = cache[key] { - return cached - } - let value = f(arg1, arg2) - cache[key] = value - return value - } -} - -@inlinable -public func memoized( - _ f: @escaping ((Arg1, Arg2, Arg3) -> Return) -) -> (Arg1, Arg2, Arg3) -> Return { - var cache = [Key3: Return]() - return { (arg1: Arg1, arg2: Arg2, arg3: Arg3) -> Return in - let key = Key3(arg1: arg1, arg2: arg2, arg3: arg3) - if let cached = cache[key] { - return cached - } - let value = f(arg1, arg2, arg3) - cache[key] = value - return value - } -} - -@inlinable -public func memoized( - _ f: @escaping ((Arg) throws -> Return) -) -> (Arg) throws -> Return { - var cache = [Arg: Return]() - return { (arg: Arg) -> Return in - if let cached = cache[arg] { - return cached - } - let value = try f(arg) - cache[arg] = value - return value - } -} - -@inlinable -public func memoized( - _ f: @escaping ((Arg1, Arg2) throws -> Return) -) -> (Arg1, Arg2) throws -> Return { - var cache = [Key2: Return]() - return { (arg1: Arg1, arg2: Arg2) -> Return in - let key = Key2(arg1: arg1, arg2: arg2) - if let cached = cache[key] { - return cached - } - let value = try f(arg1, arg2) - cache[key] = value - return value - } -} - -@inlinable -public func memoized( - _ f: @escaping ((Arg1, Arg2, Arg3) throws -> Return) -) -> (Arg1, Arg2, Arg3) throws -> Return { - var cache = [Key3: Return]() - return { (arg1: Arg1, arg2: Arg2, arg3: Arg3) -> Return in - let key = Key3(arg1: arg1, arg2: arg2, arg3: arg3) - if let cached = cache[key] { - return cached - } - let value = try f(arg1, arg2, arg3) - cache[key] = value - return value - } -} - diff --git a/Sources/uncurry.swift b/Sources/uncurry.swift index 372bf01..1eaef62 100644 --- a/Sources/uncurry.swift +++ b/Sources/uncurry.swift @@ -14,7 +14,7 @@ /// - Returns: Uncurried function @inlinable @inline(__always) public func uncurry( - _ f: @escaping (Arg1) -> Return + _ f: @escaping (Arg1) -> Return ) -> (Arg1) -> Return { { (arg1: Arg1) -> Return in f(arg1) @@ -23,7 +23,7 @@ public func uncurry( @inlinable @inline(__always) public func uncurry( - _ f: @escaping (Arg1) -> (Arg2) -> Return + _ f: @escaping (Arg1) -> (Arg2) -> Return ) -> (Arg1, Arg2) -> Return { { (arg1: Arg1, arg2: Arg2) -> Return in f(arg1)(arg2) @@ -32,7 +32,7 @@ public func uncurry( @inlinable @inline(__always) public func uncurry( - _ f: @escaping (Arg1) -> (Arg2) -> (Arg3) -> Return + _ f: @escaping (Arg1) -> (Arg2) -> (Arg3) -> Return ) -> (Arg1, Arg2, Arg3) -> Return { { (arg1: Arg1, arg2: Arg2, arg3: Arg3) -> Return in f(arg1)(arg2)(arg3) diff --git a/Sources/weakify.swift b/Sources/weakify.swift index 0269a13..7f87002 100644 --- a/Sources/weakify.swift +++ b/Sources/weakify.swift @@ -2,20 +2,21 @@ /// Weakly captures a method of `target` /// -/// class ViewController { -/// let textHandler = TextHandler() -/// ... -/// override func viewDidLoad() { -/// ... -/// textHandler.delegate = weakify(self, in: type(of: self).textUpdated) -/// } +/// class ViewController: UIViewController { +/// let networkManager = NetworkManager() +/// ... /// -/// func textUpdated(_ text: String) { -/// ... -/// } -/// } +/// override func viewDidLoad() { +/// super.viewDidLoad() +/// networkManager.fetchData(completion: weakify(self, in: ViewController.updateUI)) +/// } /// -/// - Note: Semantically the same as `textHandler.delegate = { [weak self] in self?.textUpdated($0) }` +/// func updateUI() { +/// ... +/// } +/// } +/// +/// - Note: Semantically the same as `networkManager.fetchData { [weak self] in self?.updateUI() }` /// /// - Parameters: /// - target: The instance that should be captured weakly. @@ -29,8 +30,8 @@ public func weakify( in action: @escaping (Target) -> (repeat each Arg) -> Void ) -> (repeat each Arg) -> Void { { [weak target] (arg: repeat each Arg) in - guard let target else { return } - action(target)(repeat each arg) + guard let target else { return } + action(target)(repeat each arg) } } @@ -41,97 +42,97 @@ public func weakify( in action: @escaping (Target) -> (repeat each Arg) throws -> Void ) -> (repeat each Arg) throws -> Void { { [weak target] (arg: repeat each Arg) in - guard let target else { return } - try action(target)(repeat each arg) + guard let target else { return } + try action(target)(repeat each arg) } } // MARK: iOS 12..<17, macOS 10.13.0..<14.0.0, watchOS 4..<10 -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> () -> Void + _ target: Target, + in action: @escaping (Target) -> () -> Void ) -> () -> Void { { [weak target] in - guard let target else { return } - action(target)() + guard let target else { return } + action(target)() } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> (Arg) -> Void + _ target: Target, + in action: @escaping (Target) -> (Arg) -> Void ) -> (Arg) -> Void { { [weak target] arg in - guard let target else { return } - action(target)(arg) + guard let target else { return } + action(target)(arg) } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> (Arg1, Arg2) -> Void + _ target: Target, + in action: @escaping (Target) -> (Arg1, Arg2) -> Void ) -> (Arg1, Arg2) -> Void { { [weak target] arg1, arg2 in - guard let target else { return } - action(target)(arg1, arg2) + guard let target else { return } + action(target)(arg1, arg2) } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> (Arg1, Arg2, Arg3) -> Void + _ target: Target, + in action: @escaping (Target) -> (Arg1, Arg2, Arg3) -> Void ) -> (Arg1, Arg2, Arg3) -> Void { { [weak target] arg1, arg2, arg3 in - guard let target else { return } - action(target)(arg1, arg2, arg3) + guard let target else { return } + action(target)(arg1, arg2, arg3) } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> () throws -> Void + _ target: Target, + in action: @escaping (Target) -> () throws -> Void ) -> () throws -> Void { { [weak target] in - guard let target else { return } - try action(target)() + guard let target else { return } + try action(target)() } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> (Arg) throws -> Void + _ target: Target, + in action: @escaping (Target) -> (Arg) throws -> Void ) -> (Arg) throws -> Void { { [weak target] arg in - guard let target else { return } - try action(target)(arg) + guard let target else { return } + try action(target)(arg) } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> (Arg1, Arg2) throws -> Void + _ target: Target, + in action: @escaping (Target) -> (Arg1, Arg2) throws -> Void ) -> (Arg1, Arg2) throws -> Void { { [weak target] arg1, arg2 in - guard let target else { return } - try action(target)(arg1, arg2) + guard let target else { return } + try action(target)(arg1, arg2) } } -@inlinable +@inlinable @inline(__always) public func weakify( - _ target: Target, - in action: @escaping (Target) -> (Arg1, Arg2, Arg3) throws -> Void + _ target: Target, + in action: @escaping (Target) -> (Arg1, Arg2, Arg3) throws -> Void ) -> (Arg1, Arg2, Arg3) throws -> Void { { [weak target] arg1, arg2, arg3 in - guard let target else { return } - try action(target)(arg1, arg2, arg3) + guard let target else { return } + try action(target)(arg1, arg2, arg3) } }