Swiftpack.co - juliand665/HandyOperators as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by juliand665.
juliand665/HandyOperators v2.1.0
a tiny collection of operators to help clean up your code
⭐️ 1
🕓 2 years ago
.package(url: "https://github.com/juliand665/HandyOperators.git", from: "v2.1.0")

HandyOperators

Provides some general-purpose operators for shorter & more expressive code.

<- ("with")

Applies a closure to a value, returning the transformed copy.

final class Client {
    // with HandyOperators
    let jsonDecoder1 = JSONDecoder() <- {
        $0.dateDecodingStrategy = .iso8601
    }
    
    // without HandyOperators
    let jsonDecoder2: JSONDecoder = {
        let jsonDecoder = JSONDecoder()
        jsonDecoder.dateDecodingStrategy = .iso8601
        return jsonDecoder
    }()
}

Also useful for easily applying mutating functions to copies:

extension Array {
    // with HandyOperators
    func appending1(_ element: Element) -> Self {
        self <- { $0.append(element) }
    }
    
    // without HandyOperators
    func appending2(_ element: Element) -> Self {
        var copy = self
        copy.append(element)
        return copy
    }
}

??? (unwrap or throw)

Takes an optional and an error (autoclosure, so it's lazy) and returns the unwrapped optional or throws if nil.

// with HandyOperators
func doSomething1(with optional: String?) throws {
    print(try optional ??? MissingValueError())
}

// without HandyOperators
func doSomething2(with optional: String?) throws {
    guard let value = optional else { throw MissingValueError() }
    print(value)
}

GitHub

link
Stars: 1
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Async & Pointfree
2 years ago
  • Added an async version of <- (too bad reasync isn't a thing (yet?))
  • Added (disfavored) overloads taking a nonmutating closure ((T) -> Void) for easy pointfree application of such.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics