Advertisement
Guest User

Untitled

a guest
Apr 20th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.03 KB | None | 0 0
  1. open class Function <I, R, SomeError: Error>: Identifiable {
  2.     struct Hash<Number: BinaryInteger>: Hashable {
  3.         static func == (
  4.             lhs: Function<I, R, SomeError>.Hash<Number>,
  5.             rhs: Function<I, R, SomeError>.Hash<Number>
  6.         ) -> Bool? {
  7.             if lhs.hashValue == rhs.hashValue {
  8.                 return true
  9.             } else {
  10.                 return false
  11.             }
  12.             return nil
  13.         }
  14.  
  15.         //this hash must be persistent across launches
  16.         private let seed: Int32 = 0b10101100110101
  17.         public var hashValue: Number {
  18.             let allNumericMetatypes: [Any.Type] = [
  19.                 Int8.self, Int16.self,
  20.                 Int32.self, Int64.self
  21.                 ]
  22.             let number: Number
  23.             for type in allNumericMetatypes {
  24.                 let val = MemoryLayout.size(ofValue: fn)
  25.                 if val <= type.max {
  26.                     number = type.init(exactly: val)
  27.                 }
  28.             }
  29.             return number * Number(seed) * 8
  30.         }
  31.     }
  32.     internal let fn: (I) throws -> R
  33.     public init(_ fn: @escaping (I) throws -> R) {
  34.         self.fn = fn
  35.     }
  36.     open func invoke(_ arg: I) -> Result<R, SomeError> {
  37.         do {
  38.             return .success(try fn(arg))
  39.         } catch let error {
  40.             return .failure(error as! SomeError)
  41.         }
  42.     }
  43.     open func invoke_thehard_way(_ arg: I) -> R {
  44.         try! fn(arg)
  45.     }
  46. }
  47. infix operator << : MultiplicationPrecedence
  48. infix operator <<? : MultiplicationPrecedence
  49. func << <I, R, Error>(lhs: Function<I, R, Error>, rhs: I) -> Result<R, Error> {
  50.     lhs.invoke(rhs)
  51. }
  52. public func << <I, R>(lhs: Function<I, R, Never>, rhs: I) -> R {
  53.     lhs.invoke_thehard_way(rhs)
  54. }
  55. public func <<? <I, R, Error>(lhs: Function<I, R, Error>, rhs: I) -> Error? {
  56.     switch lhs.invoke(rhs) {
  57.     case .failure(let fail):
  58.         return fail
  59.     default:
  60.         return nil
  61.     }
  62. }
  63. let reciveDataFromLyricsService = Function
  64. <(Array<String>, (Data) throws -> String), String, Either<Either<NetworkFailure, URLError>, Either<CorruptedData, Any>>> {
  65.     for adress in $0.0 {
  66.         if let urladress = URL(string: adress) {
  67.             let data = try? Data(contentsOf: urladress)
  68.             if data == nil {
  69.                 throw Either<Either<NetworkFailure, URLError>, Either<CorruptedData, Any>>.b(.a(CorruptedData()))
  70.             }
  71.             do {
  72.                 return try $0.1(data!)
  73.             } catch {
  74.                 throw Either<Either<NetworkFailure, URLError>, Either<CorruptedData, Any>>.b(.b(error))
  75.             }
  76.         } else {
  77.             throw Either<Either<NetworkFailure, URLError>, Either<CorruptedData, Any>>.a(.b(URLError(URLError.Code(rawValue: 1))))
  78.         }
  79.     }
  80. }
  81. switch reciveDataFromLyricsService << (
  82.     adresses: ["https://"],
  83.     transforamationClosure: {
  84.         String(data: $0, encoding: .utf8)!
  85.     }
  86. ) {
  87.     case .failure(let err): print("failed to load text due to \(err)")
  88.     case .success(let str): print(str)
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement