HaskellSwift

Swift 程序员的函数式库。

它包含一系列有用的函数式库。

数据

Data.Char, Data.List, Data.Maybe, Data.Either, Data.Tuple, Data.Function

GHC

Data.Num

语言

Prelude

先决条件

macOS 10.13 Xcode 9+

示例

Data.List

map

let xs = "haskell"
map(toUpper, xs)

"HASKELL"

filter

let xs               = [1, 2, 3, 4, 5]
let greaterThanThree = { x in x > 3 }
filter(greaterThanThree, list)

[4, 5]

foldl

let xs       = [1, 2, 3]
let adds     = { (x: Int,y: Int) in x+y }
foldl1(adds, xs)

6

Data.Function

.. (函数组合)

1

let process : ([Int]) -> Int = last .. reverse
let xs       = [1,2,3,4,5]
process(xs)

1

2

func _isPrime(n : Int) -> Bool {
    let array2ToN : Int->[Int]   = { x in Array(2...x) }
    let divisors  : Int->[Int]   = array2ToN .. Int.init .. ceil .. sqrt .. Double.init
    let isDivisible              = or .. map({ x in n % x == 0})
    let isNotPrime               = isDivisible .. divisors
    return !isNotPrime(n)
}
_isPrime(3)

true

Data.Maybe

maybeToList

let x : Int? = nil
maybeToList(x)

[]

catMaybes

let xs: [Int?] = [1, nil, 3]
catMaybes(xs)

[1,3]

它有什么帮助?

对于同一个问题,有 3 个解决方案。A、B、C screenshot 2016-03-15 12 02 15

A 没有使用 HaskellSwift,B 和 C 使用了 HaskellSwift。A 是命令式的,B 和 C 是声明式的。

如果您想了解更多信息,请查看 Xcode 项目中的测试用例或使用 Hoogle。

https://www.haskell.org/hoogle/?hoogle=map

此外,还有一个使用 HaskellSwift 编写的公共项目。

https://github.com/unchartedworks/autobuild