sloik/zippy
Zip function.
`zipWith`
Many times you run your code to do smething with values that are returned. To stramline that you can pass a function to zipWith.
func validateName (_ s: String) -> Either<Int,String> { ... }
func validateLastName(_ s: String) -> Either<Int,String> { ... }
struct User { let name: String; let lastName: String }
// Given two Eithers when they both are right then returns User.
// Notice as this also returns an Either but luckely this type has a map.
let createUserWhenValid: Either<Int,String>,Either<Int,String>) -> Either<Int,User> = zip(with: User.init)
createUserWhenValid(validateName, validateLastName)
.map { (user: User) in
// Real instance of User type π
}
.mapLeft { (errorCodes: [Int]) in
// error codes that were returned as left values
}
Optional API
If you want to streamline working with Optionals in Swift then please checkout OptionalAPI Swift Package. It has rich documentation and tests to show how it can be used.
ππ³ Rabbit Hole
This project is part of the ππ³ Rabbit Hole Packages Collection
Package Metadata
Repository: sloik/zippy
Default branch: main
README: README.md