{"id":1134,"url":"https://github.com/typelift/Swiftz","last_synced_at":"2025-08-06T16:32:09.270Z","repository":{"id":17619369,"uuid":"20423545","full_name":"typelift/Swiftz","owner":"typelift","description":"Functional programming in Swift","archived":false,"fork":false,"pushed_at":"2022-06-30T07:38:54.000Z","size":1566,"stargazers_count":3333,"open_issues_count":12,"forks_count":233,"subscribers_count":132,"default_branch":"master","last_synced_at":"2024-10-29T15:28:04.168Z","etag":null,"topics":["functional-programming","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typelift.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-02T23:42:15.000Z","updated_at":"2024-10-29T06:59:47.000Z","dependencies_parsed_at":"2022-09-22T14:24:42.307Z","dependency_job_id":null,"html_url":"https://github.com/typelift/Swiftz","commit_stats":null,"previous_names":["maxpow4h/swiftz"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelift%2FSwiftz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelift%2FSwiftz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelift%2FSwiftz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelift%2FSwiftz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typelift","download_url":"https://codeload.github.com/typelift/Swiftz/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228714137,"owners_count":17961331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional-programming","swift"],"created_at":"2024-01-05T20:15:39.628Z","updated_at":"2024-12-09T16:31:18.531Z","avatar_url":"https://github.com/typelift.png","language":"Swift","readme":"[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Build Status](https://travis-ci.org/typelift/Swiftz.svg?branch=master)](https://travis-ci.org/typelift/Swiftz)\n[![Gitter chat](https://badges.gitter.im/DPVN/chat.png)](https://gitter.im/typelift/general?utm_source=share-link\u0026utm_medium=link\u0026utm_campaign=share-link)\n \nSwiftz\n======\n\nSwiftz is a Swift library for functional programming.\n\nIt defines functional data structures, functions, idioms, and extensions that augment \nthe Swift standard library.\n\nFor a small, simpler way to introduce functional primitives into any codebase,\nsee [Swiftx](https://github.com/typelift/Swiftx). \n\nIntroduction\n------------\n\nSwiftz draws inspiration from a number of functional libraries \nand languages.  Chief among them are [Scalaz](https://github.com/scalaz/scalaz),\n[Prelude/Base](https://hackage.haskell.org/package/base), [SML\nBasis](http://sml-family.org/Basis/), and the [OCaml Standard\nLibrary](http://caml.inria.fr/pub/docs/manual-ocaml/stdlib.html).  Elements of\nthe library rely on their combinatorial semantics to allow declarative ideas to\nbe expressed more clearly in Swift.\n\nSwiftz is a proper superset of [Swiftx](https://github.com/typelift/Swiftx) that\nimplements higher-level data types like Arrows, Lists, HLists, and a number of\ntypeclasses integral to programming with the maximum amount of support from the\ntype system.\n\nTo illustrate use of these abstractions, take these few examples:\n\n**Lists**\n\n```swift\nimport struct Swiftz.List\n\n//: Cycles a finite list of numbers into an infinite list.\nlet finite : List\u003cUInt\u003e = [1, 2, 3, 4, 5]\nlet infiniteCycle = finite.cycle()\n\n//: Lists also support the standard map, filter, and reduce operators.\nlet l : List\u003cInt\u003e = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n\nlet twoToEleven = l.map(+1) // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\nlet even = l.filter((==0) • (%2)) // [2, 4, 6, 8, 10]\nlet sum = l.reduce(curry(+), initial: 0) // 55\n\n//: Plus a few more.\nlet partialSums = l.scanl(curry(+), initial: 0) // [0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]\nlet firstHalf = l.take(5) // [1, 2, 3, 4, 5]\nlet lastHalf = l.drop(5) // [6, 7, 8, 9, 10]\n```\n\n**Semigroups and Monoids**\n\n```swift\nlet xs = [1, 2, 0, 3, 4]\n\nimport protocol Swiftz.Semigroup\nimport func Swiftz.sconcat\nimport struct Swiftz.Min\n\n//: The least element of a list can be had with the Min Semigroup.\nlet smallestElement = sconcat(Min(2), t: xs.map { Min($0) }).value() // 0\n \nimport protocol Swiftz.Monoid\nimport func Swiftz.mconcat\nimport struct Swiftz.Sum\n\n//: Or the sum of a list with the Sum Monoid.\nlet sum = mconcat(xs.map { Sum($0) }).value() // 10\n\nimport struct Swiftz.Product\n\n//: Or the product of a list with the Product Monoid.\nlet product = mconcat(xs.map { Product($0) }).value() // 0\n```\n\n**Arrows**\n\n```swift\nimport struct Swiftz.Function\nimport struct Swiftz.Either\n\n//: An Arrow is a function just like any other.  Only this time around we\n//: can treat them like a full algebraic structure and introduce a number\n//: of operators to augment them.\nlet comp = Function.arr(+3) • Function.arr(*6) • Function.arr(/2)\nlet both = comp.apply(10) // 33\n\n//: An Arrow that runs both operations on its input and combines both\n//: results into a tuple.\nlet add5AndMultiply2 = Function.arr(+5) \u0026\u0026\u0026 Function.arr(*2)\nlet both = add5AndMultiply2.apply(10) // (15, 20)\n\n//: Produces an Arrow that chooses a particular function to apply\n//: when presented with the side of an Either.\nlet divideLeftMultiplyRight = Function.arr(/2) ||| Function.arr(*2)\nlet left = divideLeftMultiplyRight.apply(.Left(4)) // 2\nlet right = divideLeftMultiplyRight.apply(.Right(7)) // 14\n``` \n\n**Operators**\n\nSee [Operators](https://github.com/typelift/Operadics#operators) for a list of supported operators.\n\nSetup\n-----\n\nTo add Swiftz to your application:\n\n**Using Carthage**\n\n- Add Swiftz to your Cartfile\n- Run `carthage update`\n- Drag the relevant copy of Swiftz into your project.\n- Expand the Link Binary With Libraries phase\n- Click the + and add Swiftz\n- Click the + at the top left corner to add a Copy Files build phase\n- Set the directory to `Frameworks`\n- Click the + and add Swiftz\n\n**Using Git Submodules**\n\n- Clone Swiftz as a submodule into the directory of your choice\n- Run `git submodule init -i --recursive`\n- Drag `Swiftz.xcodeproj` or `Swiftz-iOS.xcodeproj` into your project tree as a subproject\n- Under your project's Build Phases, expand Target Dependencies\n- Click the + and add Swiftz\n- Expand the Link Binary With Libraries phase\n- Click the + and add Swiftz\n- Click the + at the top left corner to add a Copy Files build phase\n- Set the directory to `Frameworks`\n- Click the + and add Swiftz\n \n**Using Swift Package Manager**\n\n- Add Swiftz to your `Package.swift` within your project's `Package` definition:\n\n```swift\nlet package = Package(\n\tname: \"MyProject\",\n\t...\n\tdependencies: [\n\t\t.package(url: \"https://github.com/typelift/Swiftz.git\", from: \"0.0.0\")\n\t\t...\n\t],\n\ttargets: [\n\t\t.target(\n            name: \"MyProject\",\n            dependencies: [\"Swiftz\"]),\n        ...\n\t]\n)\n```\n\nSystem Requirements\n===================\n\nSwiftz supports OS X 10.9+ and iOS 8.0+.\n\nLicense\n=======\n\nSwiftz is released under the BSD license.\n\n","funding_links":[],"categories":["Functional Programming","Libs","Libraries","Swift","Library","Utility [🔝](#readme)"],"sub_categories":["Getting Started","Utility","[Swift](https://developer.apple.com/swift)","Other free courses","Linter"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypelift%2FSwiftz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypelift%2FSwiftz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypelift%2FSwiftz/lists"}