Skip to content

Commons is a collection of largely standalone Swift abstractions.

License

Notifications You must be signed in to change notification settings

EBGToo/SBCommons

Repository files navigation

Commons

Commons is a collection of largely standalone Swift abstractions.

Swift 3.0 Platforms OS X | Linux License Version

Features

Functions

Swift has first class functions with static binding from the lexical context. Combining functions in a few well-defined ways is a common need. We provide the following functions that returns functions of x:

  • Always - always returns y
  • Complement - returns !pred(x)
  • Compose - computes g(f(x))
  • Disjoin - returns pred1(x) || pred2(x)
  • Conjoin - returns pred1(x) && pred2(x)
  • equalTo - returns pred(x, y)

where y, pred{,1,2}, f, and g are captured lexically.

Additional functions include any and all on SequenceType and a set of functions taking a predicate and returning functions of a SequenceType as: allUsing, anyUsing, mapUsing and appUsing.

As, Any Protocols

The As<Protocol> pattern allows any Item, with a property satisfying a protocol, to 'adopt' that protocol. For example, consider a Person abstraction with properties: ssn, name and age (in years):

class Person : Equatable {
  let ssn  : String
  let name : String
  var age  : UInt
}

func == (lhs:Person, rhs:Person) -> Bool {
  return lhs.ssn == rhs.ssn
}

Person is not comparable but in some contexts you might need to compare people based on any of ssn, name or age. You'd do that with, for example:

let p1 = Person (ssn: "123", name: "Zack", age: 10)
let p2 = Person (ssn: "321", name: "Zoey", age: 9)

let younger = min(AsComparable (value: p1.age, item: p1), AsComparable (value: p2.age, item: p2)).item
younger.name // => "Zoey"

The As<Protocol> is implemented for three common Swift protocols as AsEquatable, AsComparable, and AsHashable. AsComparableInvert is also provided.

Result

The ubiquitious

enum Result<V,E:ErrorType> {
  case Success(V)
  case Failure(E)
}

This really should be supplanted by other, more complete, GitHub packages.

Usage

Access the framework with

import SBCommons

Installation

Three easy installation options:

Apple Package Manager

In your Package.swift file, add a dependency on SBCommons:

import PackageDescription

let package = Package (
  name: "<your package>",
  dependencies: [
    // ...
    .Package (url: "https://github.com/EBGToo/SBCommons.git",  majorVersion: 0),
    // ...
  ]
)

Cocoa Pods

pod 'SBCommons', '~> 0.1'

XCode

$ git clone https://github.com/EBGToo/SBCommons.git SBCommons

Add the SBCommons Xcode Project to your Xcode Workspace.

About

Commons is a collection of largely standalone Swift abstractions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages