Coder Social home page Coder Social logo

webcpu / sodium-swift Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 970 KB

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

License: Other

Swift 98.57% Objective-C 1.30% Shell 0.13%
swift frp functional-reactive-programming ios macos

sodium-swift's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.