项目作者: unchartedworks

项目描述 :
Swift implementation of Sodium FRP (Functional Reactive Programming) library based on SodiumFRP/sodium-swift
高级语言: Swift
项目地址: git://github.com/unchartedworks/sodium-swift.git
创建时间: 2018-02-09T16:51:31Z
项目社区:https://github.com/unchartedworks/sodium-swift

开源协议:Other

下载


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.

counter

  1. import UIKit
  2. import SodiumCocoa
  3. import SodiumSwift
  4. class CounterViewController: UIViewController {
  5. @IBOutlet weak var counterLabel: NALabel!
  6. @IBOutlet weak var upButton: NAButton!
  7. @IBOutlet weak var downButton: NAButton!
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10. counterLabel.txt = count(upButton, downButton)
  11. }
  12. }
  13. func count(_ upButton: NAButton!, _ downButton: NAButton!) -> Cell<String> {
  14. let sUp = upButton.clicked.mapTo(1)
  15. let sDown = downButton.clicked.mapTo(-1)
  16. return sUp.orElse(sDown).accum(0, f: +).map({String($0)})
  17. }

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.

  1. import UIKit
  2. import SodiumSwift
  3. import SodiumCocoa
  4. class QuestionViewController: UIViewController {
  5. @IBOutlet weak var questionLabel: NALabel!
  6. @IBOutlet weak var answerLabel: NALabel!
  7. @IBOutlet weak var nextButton: NAButton!
  8. var count: Int64 = loadCount()
  9. let sViewDidLoad = StreamSink<SUnit>()
  10. let sNextButton = StreamSink<SUnit>()
  11. @IBAction func next(_ sender: AnyObject) {
  12. sNextButton.send(SUnit.value)
  13. }
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. sViewDidLoad.send(SUnit.value)
  17. setupQuiz()
  18. }
  19. func setupQuiz() {
  20. let sArithmeticExpression = sViewDidLoad.orElse(sNextButton).map(createQuestion)
  21. questionLabel.txt = createQuestionEndPoint(sArithmeticExpression)
  22. answerLabel.txt = createAnswerEndPoint(sArithmeticExpression)
  23. }
  24. }

Prerequisites

  • Xcode Command Line Tools

    1. $ xcode-select --install
  • Brew

    https://brew.sh

  • Carthage

    1. $ brew install Carthage

Build

Carthage

Create a Cartfile in your project’s root directory.

  1. github "unchartedworks/sodium-swift"

Build

  1. $ carthage update

Add the framework(s) to your project

iOS

./Carthage/Build/iOS/

SodiumSwift.framework

SodiumCocoa.framework

macOS

./Carthage/Build/Mac/

SodiumSwift.framework