webcpu / sodium-swift

Swift implementation of Sodium FRP (Functional Reactive Programming) library based on SodiumFRP/sodium-swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

import UIKit
import SodiumCocoa
import SodiumSwift

class 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<String> {
    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.

import UIKit
import SodiumSwift
import SodiumCocoa

class QuestionViewController: UIViewController {
    @IBOutlet weak var questionLabel: NALabel!
    @IBOutlet weak var answerLabel: NALabel!
    @IBOutlet weak var nextButton: NAButton!

    var count: Int64 = loadCount()

    let sViewDidLoad = StreamSink<SUnit>()
    let sNextButton  = StreamSink<SUnit>()

    @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
    
  • Brew

    https://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

About

Swift implementation of Sodium FRP (Functional Reactive Programming) library based on SodiumFRP/sodium-swift

License:Other


Languages

Language:Swift 98.6%Language:Objective-C 1.3%Language:Shell 0.1%