Prelude

所属分类:collect
开发工具:Swift
文件大小:0KB
下载次数:0
上传日期:2018-07-19 13:21:25
上 传 者sh-1993
说明:  简单函数编程工具的Swiftμ框架,
(Swift μframework of simple functional programming tools,)

文件列表:
LICENSE (1073, 2018-07-19)
Package.swift (455, 2018-07-19)
Prelude.podspec (629, 2018-07-19)
Prelude.xcodeproj/ (0, 2018-07-19)
Prelude.xcodeproj/project.pbxproj (43822, 2018-07-19)
Prelude.xcodeproj/project.xcworkspace/ (0, 2018-07-19)
Prelude.xcodeproj/project.xcworkspace/contents.xcworkspacedata (152, 2018-07-19)
Prelude.xcodeproj/project.xcworkspace/xcshareddata/ (0, 2018-07-19)
Prelude.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (238, 2018-07-19)
Prelude.xcodeproj/xcshareddata/ (0, 2018-07-19)
Prelude.xcodeproj/xcshareddata/xcschemes/ (0, 2018-07-19)
Prelude.xcodeproj/xcshareddata/xcschemes/Prelude-Mac.xcscheme (4258, 2018-07-19)
Prelude.xcodeproj/xcshareddata/xcschemes/Prelude-iOS.xcscheme (4258, 2018-07-19)
Prelude.xcodeproj/xcshareddata/xcschemes/Prelude.xcscheme (3643, 2018-07-19)
Prelude.xcodeproj/xcshareddata/xcschemes/PreludeTests.xcscheme (2447, 2018-07-19)
Prelude/ (0, 2018-07-19)
Prelude/Application.swift (2294, 2018-07-19)
Prelude/Compose.swift (2107, 2018-07-19)
Prelude/Curry.swift (1420, 2018-07-19)
Prelude/Fix.swift (537, 2018-07-19)
Prelude/Flip.swift (799, 2018-07-19)
Prelude/Info.plist (909, 2018-07-19)
Prelude/Optional.swift (436, 2018-07-19)
Prelude/Prelude.h (223, 2018-07-19)
Prelude/Prelude.swift (420, 2018-07-19)
Prelude/Tuple.swift (228, 2018-07-19)
PreludeTests/ (0, 2018-07-19)
PreludeTests/ApplicationTests.swift (1861, 2018-07-19)
PreludeTests/ComposeTests.swift (1032, 2018-07-19)
PreludeTests/ConjunctionTests.swift (688, 2018-07-19)
PreludeTests/CurryTests.swift (982, 2018-07-19)
PreludeTests/FixTests.swift (998, 2018-07-19)
PreludeTests/FlipTests.swift (268, 2018-07-19)
PreludeTests/Info.plist (733, 2018-07-19)
PreludeTests/PreludeTests.swift (322, 2018-07-19)
PreludeTests/TupleTests.swift (245, 2018-07-19)

# Prelude This is a Swift framework providing a number of simple functions that I use in many of my other frameworks. Rather than continue to reimplement them for each consumer, I am gathering them here together. Notably, this framework does not provide any new types, or any functions which operate on custom types; those presumably belong in frameworks of their own. ## Table of Contents - [Gallery](https://github.com/robrix/Prelude/blob/master/#gallery) - [`id`](https://github.com/robrix/Prelude/blob/master/#id) - [`const`](https://github.com/robrix/Prelude/blob/master/#const) - [`>>>` and `<<<`](https://github.com/robrix/Prelude/blob/master/#-and-) - [`fix`](https://github.com/robrix/Prelude/blob/master/#fix) - [`|>` and `<|`](https://github.com/robrix/Prelude/blob/master/#-and--1) - [`curry`](https://github.com/robrix/Prelude/blob/master/#curry) - [`flip`](https://github.com/robrix/Prelude/blob/master/#flip) - [`&&&`](https://github.com/robrix/Prelude/blob/master/#-) - [`swap`](https://github.com/robrix/Prelude/blob/master/#swap) - [`first` and `second`](https://github.com/robrix/Prelude/blob/master/#first-and-second) - [Documentation](https://github.com/robrix/Prelude/blob/master/#documentation) - [Integration](https://github.com/robrix/Prelude/blob/master/#integration) # Gallery Prelude’s functions are infinitely useful. Here’s a gallery of just a few of the things you can do with them. ## `id` Passing `id` as the argument to the `flattenMap` method of a [`Stream`](https://github.com/robrix/Prelude/blob/master/https://github.com/robrix/Traversal) of `Stream`s will flatten it out into a stream of all the nested elements: ```swift func flatten(stream: Stream>) -> Stream { return stream.flattenMap(id) } ``` ## `const` Passing the result of `const` to an [`Either`](https://github.com/robrix/Prelude/blob/master/https://github.com/robrix/Either) is convenient for transforming it into an `Optional`: ```swift let result: Either = … if let string = result.either(const(nil), id) { println("ohai \($0)") } ``` ## `>>>` and `<<<` The left-to-right and right-to-left composition operators (`>>>` and `<<<` respectively) chain operations together: ```swift let repl: File -> String = readLine >>> parseString >>> evaluateAST >>> toString while true { println(repl(standardInput)) } ``` ## `fix` You can use `fix` to make an anonymous function which calls itself recursively: ```swift let factorial = fix { recur in { n in n > 0 ? n * recur(n - 1) : 1 } } ``` ## `|>` and `<|` The forward and backward application operators (`|>` and `<|` respectively) apply the function on the side they’re pointing at to the value on the other side. This can sometimes make code more readable. This is particularly the case for the forward application operator. `x |> f` is equivalent to `f(x)`, but it reads in the direction that the data flows. The benefit is more obvious with longer function names: ```swift 100 |> toString |> count // => 3 // this is equivalent to countElements(toString(100)) ``` Backward application reads in the wrong direction for this—`f <| x` isn’t really any improvement on `f(x)`. Unlike forward application, however, `<|` can apply binary and ternary functions to their first operands. This enables you to make something like [Haskell’s operator sections](https://github.com/robrix/Prelude/blob/master/https://www.haskell.org/haskellwiki/Section_of_an_infix_operator): ```swift let successor: Int -> Int = (+) <| 1 successor(3) // => 4 map([1, 2, 3], (*) <| 2) // => [2, 4, 6] ``` You can also combine `|>` and `<|` with [`flip`](https://github.com/robrix/Prelude/blob/master/#flip) to pass data through chains of higher-order functions like `sorted`, `map`, and `reduce`: ```swift let result = [66, 78, 1, 95, 76] |> (flip(sorted) <| (<)) // sort in ascending order |> (flip(map) <| toString) // make them into strings |> String.join(", ") // comma-separate them let sum: [Int] -> Int = flip(reduce) <| (+) <| 0 ``` Since Swift functions can also be applied to tuples of their arguments, you can also use `|>` and `<|` with binary, ternary, etc. functions just by placing a tuple on the other side: ```swift (1, 2) |> (+) // => 3 ``` ## `curry` Currying takes a function of >1 parameter and returns a function of one parameter which returns a function of one parameter, and so on. That is, given `(T, U) -> V`, currying returns `T -> U -> V`. This is particularly useful when making more interesting functions such as [`<|`](https://github.com/robrix/Prelude/blob/master/#-and--1). ## `flip` Faux operator sectioning using `<|` might be a little surprising using non-commutative operators like `-` and `/`: `(-) <| 1` means `{ 1 - $0 }`, which is very different from `{ $0 - 1 }`. You can use `flip` to produce the latter: ```swift map([1, 2, 3], (-) <| 1) // => [0, -1, -2] map([1, 2, 3], flip(-) <| 1) // => [0, 1, 2] ``` ## `&&&` `Optional` has a `map` method which is just what you need when you want to apply a function to a value if non-`nil`, or return `nil` otherwise. When you have two `Optional` values, you can use `&&&` to combine them: ```swift let (x: Int?, y: Int?) = (2, 2) (x &&& y).map(+) // => .Some(4) ``` ## `swap` Swift’s tuples are very convenient, but sometimes when you get one, it’s the wrong way around. `swap` does to tuples what `flip` does to functions: it reverses their order. ```swift map(enumerate("hello"), swap) // => [(h, 0), (e, 1), (l, 2), (l, 3), (o, 4)] ``` ## `first` and `second` Getting one value from a tuple is a common operation that can be expressed with `first` and `second` functions. Operators provide first and second values of two-elements tuple accordingly. ```swift [(0,0), (5, 1), (9, 2)].map(second) // => [0, 1, 2] ``` # Documentation Full API documentation is in the source. # Integration 1. Add this repository as a submodule and check out its dependencies, and/or [add it to your Cartfile](https://github.com/robrix/Prelude/blob/master/https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile) if you’re using [carthage](https://github.com/robrix/Prelude/blob/master/https://github.com/Carthage/Carthage/) to manage your dependencies. 2. Drag `Prelude.xcodeproj` into your project or workspace. 3. Link your target against `Prelude.framework`. 4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Prelude.) Or use the Swift package manager and add this to your `Package.swift` file: ``` ... dependencies: [ ... .package(url: "https://github.com/robrix/Prelude", "3.0.0" ..< "4.0.0") ], targets: [ ... .target( name: "", dependencies: ["Prelude"]), ] ```

近期下载者

相关文件


收藏者