All Projects → typelift → Swiftz

typelift / Swiftz

Licence: bsd-3-clause
Functional programming in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftz

Funcshell
Improve your shell by making it functional through Haskell! (An update to Awkward)
Stars: ✭ 297 (-91.07%)
Mutual labels:  functional-programming
Coconut
Simple, elegant, Pythonic functional programming.
Stars: ✭ 3,422 (+2.86%)
Mutual labels:  functional-programming
Monio
Async-capable IO monad for JS
Stars: ✭ 311 (-90.65%)
Mutual labels:  functional-programming
Aecor
Pure functional event sourcing runtime
Stars: ✭ 299 (-91.01%)
Mutual labels:  functional-programming
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (-90.92%)
Mutual labels:  functional-programming
Quack
🐤 A multi-paradigm programming language with gradual and duck typing that targets PHP and JS
Stars: ✭ 309 (-90.71%)
Mutual labels:  functional-programming
Hareactive
Purely functional reactive programming library
Stars: ✭ 293 (-91.19%)
Mutual labels:  functional-programming
Here Be Dragons
An Intellij/Android Studio plugin to help visualise side effects in your code.
Stars: ✭ 325 (-90.23%)
Mutual labels:  functional-programming
Kocircuit
Ko: A generic type-safe language for concurrent, stateful, deadlock-free systems and protocol manipulations
Stars: ✭ 305 (-90.83%)
Mutual labels:  functional-programming
Lambda Talk
A Flock of Functions: Combinators, Lambda Calculus, & Church Encodings in JS
Stars: ✭ 315 (-90.53%)
Mutual labels:  functional-programming
Virtual Audio Graph
🎶 Library for declaratively manipulating the Web Audio API
Stars: ✭ 299 (-91.01%)
Mutual labels:  functional-programming
Awesome Idris
𝛌 Awesome Idris resources
Stars: ✭ 299 (-91.01%)
Mutual labels:  functional-programming
Functional Fortran
Functional programming for modern Fortran
Stars: ✭ 311 (-90.65%)
Mutual labels:  functional-programming
Android Cleanarchitecture Kotlin
This is a movies sample app in Kotlin, which is part of a serie of blog posts I have written about architecting android application using different approaches.
Stars: ✭ 3,646 (+9.59%)
Mutual labels:  functional-programming
Vertx Zero
Zero Framework:http://www.vertxup.cn
Stars: ✭ 320 (-90.38%)
Mutual labels:  functional-programming
Lambda
🔮 Estudos obscuros de programação funcional
Stars: ✭ 297 (-91.07%)
Mutual labels:  functional-programming
Shapeless
Generic programming for Scala
Stars: ✭ 3,207 (-3.61%)
Mutual labels:  functional-programming
Magic In Ten Mins
十分钟魔法练习
Stars: ✭ 327 (-90.17%)
Mutual labels:  functional-programming
Effect
effect isolation in Python, to facilitate more purely functional code
Stars: ✭ 324 (-90.26%)
Mutual labels:  functional-programming
Prelude Ts
Functional programming, immutable collections and FP constructs for typescript and javascript
Stars: ✭ 315 (-90.53%)
Mutual labels:  functional-programming

Carthage compatible Build Status Gitter chat

Swiftz

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

import struct Swiftz.List

//: Cycles a finite list of numbers into an infinite list.
let finite : List<UInt> = [1, 2, 3, 4, 5]
let infiniteCycle = finite.cycle()

//: Lists also support the standard map, filter, and reduce operators.
let l : List<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let twoToEleven = l.map(+1) // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
let even = l.filter((==0)  (%2)) // [2, 4, 6, 8, 10]
let sum = l.reduce(curry(+), initial: 0) // 55

//: Plus a few more.
let partialSums = l.scanl(curry(+), initial: 0) // [0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
let firstHalf = l.take(5) // [1, 2, 3, 4, 5]
let lastHalf = l.drop(5) // [6, 7, 8, 9, 10]

Semigroups and Monoids

let xs = [1, 2, 0, 3, 4]

import protocol Swiftz.Semigroup
import func Swiftz.sconcat
import struct Swiftz.Min

//: The least element of a list can be had with the Min Semigroup.
let smallestElement = sconcat(Min(2), t: xs.map { Min($0) }).value() // 0
 
import protocol Swiftz.Monoid
import func Swiftz.mconcat
import struct Swiftz.Sum

//: Or the sum of a list with the Sum Monoid.
let sum = mconcat(xs.map { Sum($0) }).value() // 10

import struct Swiftz.Product

//: Or the product of a list with the Product Monoid.
let product = mconcat(xs.map { Product($0) }).value() // 0

Arrows

import struct Swiftz.Function
import struct 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.
let comp = Function.arr(+3)  Function.arr(*6)  Function.arr(/2)
let both = comp.apply(10) // 33

//: An Arrow that runs both operations on its input and combines both
//: results into a tuple.
let add5AndMultiply2 = Function.arr(+5) &&& Function.arr(*2)
let both = add5AndMultiply2.apply(10) // (15, 20)

//: Produces an Arrow that chooses a particular function to apply
//: when presented with the side of an Either.
let divideLeftMultiplyRight = Function.arr(/2) ||| Function.arr(*2)
let left = divideLeftMultiplyRight.apply(.Left(4)) // 2
let right = divideLeftMultiplyRight.apply(.Right(7)) // 14

Operators

See Operators for a list of supported operators.

Setup

To add Swiftz to your application:

Using Carthage

  • Add Swiftz to your Cartfile
  • Run carthage update
  • Drag the relevant copy of Swiftz into your project.
  • Expand the Link Binary With Libraries phase
  • Click the + and add Swiftz
  • Click the + at the top left corner to add a Copy Files build phase
  • Set the directory to Frameworks
  • Click the + and add Swiftz

Using Git Submodules

  • Clone Swiftz as a submodule into the directory of your choice
  • Run git submodule init -i --recursive
  • Drag Swiftz.xcodeproj or Swiftz-iOS.xcodeproj into your project tree as a subproject
  • Under your project's Build Phases, expand Target Dependencies
  • Click the + and add Swiftz
  • Expand the Link Binary With Libraries phase
  • Click the + and add Swiftz
  • Click the + at the top left corner to add a Copy Files build phase
  • Set the directory to Frameworks
  • Click the + and add Swiftz

Using Swift Package Manager

  • Add Swiftz to your Package.swift within your project's Package definition:
let package = Package(
	name: "MyProject",
	...
	dependencies: [
		.package(url: "https://github.com/typelift/Swiftz.git", from: "0.0.0")
		...
	],
	targets: [
		.target(
            name: "MyProject",
            dependencies: ["Swiftz"]),
        ...
	]
)

System Requirements

Swiftz supports OS X 10.9+ and iOS 8.0+.

License

Swiftz is released under the BSD license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].