https://github.com/webcpu/sodium-swift
Swift implementation of Sodium FRP (Functional Reactive Programming) library based on SodiumFRP/sodium-swift
https://github.com/webcpu/sodium-swift
frp functional-reactive-programming ios macos swift
Last synced: 3 months ago
JSON representation
Swift implementation of Sodium FRP (Functional Reactive Programming) library based on SodiumFRP/sodium-swift
- Host: GitHub
- URL: https://github.com/webcpu/sodium-swift
- Owner: webcpu
- License: other
- Created: 2018-02-09T16:51:31.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-02T21:13:11.000Z (about 6 years ago)
- Last Synced: 2025-01-12T19:35:52.742Z (4 months ago)
- Topics: frp, functional-reactive-programming, ios, macos, swift
- Language: Swift
- Homepage:
- Size: 947 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# sodium-swift
## Introduction
Functional Reactive Programming (FRP) library.This library is based on https://github.com/SodiumFRP/sodium-swift
The original sodium-swift only supports Swift 3, that's why I migrated sodium-swift to Swift 4 and created a new repo.If you’re familiar with the idea of a domain-specific language (DSL), then you can understand FRP as a minimal complete DSL for stateful logic.
Sodium-swift replaces listeners (also known as callbacks) in the widely used observer pattern, making your code cleaner, clearer, more robust, and more maintainable—in a word, simpler.
## Examples
### Counter
This is a counter app. To increase the value, tap plus button; to decrease the value, tap minus button. The magic is that you don't have to use a mutable variable to store the value.
```swift
import UIKit
import SodiumCocoa
import SodiumSwiftclass CounterViewController: UIViewController {
@IBOutlet weak var counterLabel: NALabel!
@IBOutlet weak var upButton: NAButton!
@IBOutlet weak var downButton: NAButton!
override func viewDidLoad() {
super.viewDidLoad()
counterLabel.txt = count(upButton, downButton)
}
}func count(_ upButton: NAButton!, _ downButton: NAButton!) -> Cell {
let sUp = upButton.clicked.mapTo(1)
let sDown = downButton.clicked.mapTo(-1)
return sUp.orElse(sDown).accum(0, f: +).map({String($0)})
}
```### Math Quiz
This is a math app. When you tap the next button, it will show a new math question and the previous question's answer.
```swift
import UIKit
import SodiumSwift
import SodiumCocoaclass QuestionViewController: UIViewController {
@IBOutlet weak var questionLabel: NALabel!
@IBOutlet weak var answerLabel: NALabel!
@IBOutlet weak var nextButton: NAButton!var count: Int64 = loadCount()
let sViewDidLoad = StreamSink()
let sNextButton = StreamSink()@IBAction func next(_ sender: AnyObject) {
sNextButton.send(SUnit.value)
}override func viewDidLoad() {
super.viewDidLoad()
sViewDidLoad.send(SUnit.value)
setupQuiz()
}func setupQuiz() {
let sArithmeticExpression = sViewDidLoad.orElse(sNextButton).map(createQuestion)
questionLabel.txt = createQuestionEndPoint(sArithmeticExpression)
answerLabel.txt = createAnswerEndPoint(sArithmeticExpression)
}
}
```## Prerequisites
- Xcode Command Line Tools
```
$ xcode-select --install
```
- Brewhttps://brew.sh
- Carthage```
$ brew install Carthage
```## Build
### Carthage
**Create a Cartfile in your project's root directory.**```
github "unchartedworks/sodium-swift"
```
**Build**```
$ carthage update
```
**Add the framework(s) to your project****iOS**
./Carthage/Build/iOS/
SodiumSwift.framework
SodiumCocoa.framework
**macOS**
./Carthage/Build/Mac/
SodiumSwift.framework