You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Swiftz is a Swift library for functional programming.
It defines functional data structures, functions, idioms, and extensions that augment
the Swift standard library.
For a small, simpler way to introduce functional primitives into any codebase,
see Swiftx.
Introduction
Swiftz draws inspiration from a number of functional libraries
and languages. Chief among them are Scalaz,
Prelude/Base, SML
Basis, and the OCaml Standard
Library. Elements of
the library rely on their combinatorial semantics to allow declarative ideas to
be expressed more clearly in Swift.
Swiftz is a proper superset of Swiftx that
implements higher-level data types like Arrows, Lists, HLists, and a number of
typeclasses integral to programming with the maximum amount of support from the
type system.
To illustrate use of these abstractions, take these few examples:
Lists
importstruct Swiftz.List
//: Cycles a finite list of numbers into an infinite list.
letfinite:List<UInt>=[1,2,3,4,5]letinfiniteCycle= finite.cycle()
//: Lists also support the standard map, filter, and reduce operators.
letl:List<Int>=[1,2,3,4,5,6,7,8,9,10]lettwoToEleven= l.map(+1) // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
leteven= l.filter((==0)•(%2)) // [2, 4, 6, 8, 10]
letsum= l.reduce(curry(+), initial:0) // 55
//: Plus a few more.
letpartialSums= l.scanl(curry(+), initial:0) // [0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
letfirstHalf= l.take(5) // [1, 2, 3, 4, 5]
letlastHalf= l.drop(5) // [6, 7, 8, 9, 10]
Semigroups and Monoids
letxs=[1,2,0,3,4]importprotocol Swiftz.Semigroup
importfunc Swiftz.sconcat
importstruct Swiftz.Min
//: The least element of a list can be had with the Min Semigroup.
letsmallestElement=sconcat(Min(2), t: xs.map{Min($0)}).value() // 0
importprotocol Swiftz.Monoid
importfunc Swiftz.mconcat
importstruct Swiftz.Sum
//: Or the sum of a list with the Sum Monoid.
letsum=mconcat(xs.map{Sum($0)}).value() // 10
importstruct Swiftz.Product
//: Or the product of a list with the Product Monoid.
letproduct=mconcat(xs.map{Product($0)}).value() // 0
Arrows
importstruct Swiftz.Function
importstruct Swiftz.Either
//: An Arrow is a function just like any other. Only this time around we
//: can treat them like a full algebraic structure and introduce a number
//: of operators to augment them.
letcomp=Function.arr(+3)•Function.arr(*6)•Function.arr(/2)letboth= comp.apply(10) // 33
//: An Arrow that runs both operations on its input and combines both
//: results into a tuple.
letadd5AndMultiply2=Function.arr(+5)&&&Function.arr(*2)letboth= add5AndMultiply2.apply(10) // (15, 20)
//: Produces an Arrow that chooses a particular function to apply
//: when presented with the side of an Either.
letdivideLeftMultiplyRight=Function.arr(/2)|||Function.arr(*2)letleft= divideLeftMultiplyRight.apply(.Left(4)) // 2
letright= divideLeftMultiplyRight.apply(.Right(7)) // 14