Skip to content

Swift (µ)library for functional programming and higher-order data processing

License

Notifications You must be signed in to change notification settings

AlexanderKaraberov/SwiftLand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftLand µframework

Travis GitHub release Swift 4.0.x

Functional programming is usually defined informally as a style of building the structure and elements of computer programs that treats computation as the evaluation of pure functions. These functions are not precisely mathematical functions because FP deals only with the subset of these functions which are computable. Ordinary mathematical functions, on the other hand, don't compute anything: output already exists and we only have to define a rule of correspondence between input and output. This rule is called a function. As aforesaid we can define functional programming (again informally) as programming only with computable functions which map set of inputs (domain) to the set of permissible outputs (codomain) with the property that each input is related to exactly one output.

These functions can possibly be nonterminated but this is not true in languages with totality checking such as Idris. So in my opinion totality is not a mandatory requirement for functional programming. Also all computable functions might be continuos as stated here, here and here, but this fact is not commonly accepted and therefore I decided to omit statement about continuity and Scott-continuity particularly from the definition.

In simple words functional programming is about side effects, how to deal with them, how to make them explicit and how to reason about the whole program mathematically as though we are solving an algebraic expression.

SwiftLand is a small library which contains useful higher-order functions. Library can be used to simplify Swift development and to make functional programming a little bit simpler in Swift. There is nothing super new here. Similar functions can be found in Haskell, Idris and PureScript standard libraries. Each function has detailed comments, which can be used as documentation.

SwiftLand contains:

  • Catamorphism: foldRight (fold from the right). Swift currently has only reduce function, which is equivalent to the foldLeft in classic FP literature.
  • Special cases of foldLeft and foldRight: foldLeft1 and foldRight1 which use array's first element as initial element and don't work for empty lists.
  • Hylomorphism (hylo) and paramorphism (para) which are decribed in classical paper about recursion schemes: Functional Programming with Bananas Lenses Envelopes and Barbed Wire
  • Special folds: sum, product.
  • mapWithIndex, until
  • curry, and its inverse uncurry for different number of arguments.
  • General purpose list processing functions: filterLength, intercalate, null, concat, intersperse, span, concatMap, groupBy, splitAt, find, remove, uniq, scanl, iterateWhile, decompose.
  • Cons lists with list pattern matching
  • Zipping/unzipping lists: zip3, zipWith, unzip. (Swift standard library already has a usual zip function)
  • Combinators. Besides well-known combinators from SKI/BCKW, I also will add one by one combinators from the great book To Mock a Mockingbird. Currently implemented in Combinators.swift: C-combinator (flip), K-combinator (const), Y-combinator: (fix), I-combinator (id), Psi-combinator (on), S-combinator (substitute), B-combinator (compose), A-combinator (apply), W-combinator (duplicate).

Adding library using Carthage:

In your Cartfile specify github "AlexanderKaraberov/SwiftLand" and then run carthage update. You'll find the .framework in the Carthage/Build/iOS folder.