Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/webcpu/haskellswift

A functional library for Swift programmers
https://github.com/webcpu/haskellswift

functional-programming haskell swift

Last synced: about 2 months ago
JSON representation

A functional library for Swift programmers

Awesome Lists containing this project

README

        

# HaskellSwift
A functional library for Swift programmers.

It contains a collection of useful functional libraries.
### Data
Data.Char, Data.List, Data.Maybe, Data.Either, Data.Tuple, Data.Function

### GHC
Data.Num

### Language
Prelude

# Prerequisites
macOS 10.13
Xcode 9+

# Examples
## 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
### .. (Function Composition)
#### 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]`

### How does it help?
There are 3 solutions to the same problem. A, B, C
screenshot 2016-03-15 12 02 15

A doesn't use HaskellSwift, B and C use HaskellSwift.
A is imperative, B and C are declarative.

If you want to know more about it, please check the test cases in the Xcode Project or use Hoogle.

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

In addition, there is a public project which is written in HaskellSwift.

https://github.com/unchartedworks/autobuild