Skip to content
🎶 A collection of types and functions that enhance the Swift language.
Swift Other
  1. Swift 99.1%
  2. Other 0.9%
Branch: master
Clone or download
stephencelis Swift 5 (#126)
* Swift 5

* Remove Never's conformance to Error

* Fix some private(set) vars.

* Revert test code

* Fix

* Bump

* Bump

* Try again...

* Fix

* Bump

* bump

* Bump

* RIP empty switch

* Compiling on Linux

* Update .swift-version

* Update Dockerfile
Latest commit b26e98e Aug 9, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Remove Dependencies (#136) Jun 5, 2019
.sourcery-templates Travis+Buddybuild tests (#85) Nov 16, 2017
Prelude.playground fixes Aug 10, 2018
Prelude.xcworkspace Tagged update (#105) Apr 19, 2018
Sources Swift 5 (#126) Aug 9, 2019
Tests Swift 5 (#126) Aug 9, 2019
.gitignore Swift 5 (#126) Aug 9, 2019
.swift-version Swift 5 (#126) Aug 9, 2019
.travis.yml Swift 4.1 support (#97) Mar 29, 2018
CODE_OF_CONDUCT.md Add Code of Conduct (#32) Aug 16, 2017
Development.xcconfig Expression time fixes (#94) Dec 30, 2017
Dockerfile Swift 5 (#126) Aug 9, 2019
LICENSE Update LICENSE Dec 4, 2017
Makefile Swift 5 (#126) Aug 9, 2019
Package.swift
README.md Update README.md (#103) Apr 6, 2018

README.md

swift-prelude

macOS CircleCI Linux Build Status

A collection of frameworks to enhance the Swift language.

Stability

This library should be considered alpha, and not stable. Breaking changes will happen often.

Installation

import PackageDescription

let package = Package(
  dependencies: [
    .package(url: "https://github.com/pointfreeco/swift-prelude.git", .branch("master")),
  ]
)

Table of Contents

Prelude

A collection of types and functions to build powerful abstractions and enhance the Swift standard library.

Either

A type to express a value that holds one of two other types.

import Either

let intOrString = Either<Int, String>.left(2)

intOrString
  .bimap({ $0 + 1 }, { $0 + "!" }) // => .left(3)

Optics

A Lens type and a bridge between the lens world and the Swift key path world.

import Optics
import Prelude

struct User {
  var id: Int
  var name: String
}

let uppercased: (String) -> String = { $0.uppercased() }

let user = User(id: 1, name: "Blob")

user
  |> \.id .~ 2
  |> \.name %~ uppercased

// => User(2, "BLOB")

ValidationSemigroup

The Validation<E, A> type is a type similar to Result<E, A>, except it is given a different applicative instance in the case that E is a semigroup. This allows you to accumulate multiple errors into E instead of just taking the first error:

import Prelude
import ValidationSemigroup

struct User { let name: String; let bio: String; let email: String }
let createUser = { name in { bio in { email in User(name: name, bio: bio, email: email) } } }

func validate(name: String) -> Validation<[String], String> {
  return !name.isEmpty
    ? pure(name)
    : .invalid(["Name must be at least 1 character."])
}

func validate(bio: String) -> Validation<[String], String> {
  return bio.count <= 10
    ? pure(bio)
    : .invalid(["Bio must 10 characters or less."])
}

func validate(email: String) -> Validation<[String], String> {
  return email.contains("@")
    ? pure(email)
    : .invalid(["Email must be valid."])
}

let validUser = pure(createUser)
  <*> validate(name: "Blob")
  <*> validate(bio: "I'm a blob")
  <*> validate(email: "blob@pointfree.co")
// => .valid(User(name: "Blob", bio: "I'm a blob", email: "blob@pointfree.co"))

let invalidUser = pure(createUser)
  <*> validate(name: "Blob")
  <*> validate(bio: "Blobbin around the world")
  <*> validate(email: "blob")
// => .invalid(["Bio must 10 characters or less.", "Email must be valid."])

For more information, watch Stephen Celis’ talk.

ValidationNearSemiring

This Validation<E, A> type is a type similar to Result<E, A> and the above Validation, except it is given a different applicative instance in the case that E is a NearSemiring. This allows you to accumulate errors that describe conditions that hold with both “and” and “or”, e.g. name is required and either email or phone is required.

License

All modules are released under the MIT license. See LICENSE for details.

You can’t perform that action at this time.