Code Monkey home page Code Monkey logo

interstellar's Introduction

Interstellar

Build Status CocoaPods Version CocoaPods Plattforms

The simplest Signal<T> implementation for Functional Reactive Programming you will ever find.

This library does not use the term FRP (Functional Reactive Programming) in the way it was defined by Conal Elliot, but as a paradigm that is both functional and reactive. Read more about the difference at Why I cannot say FRP but I just did.

Features

  • Lightweight, simple, cross plattform FRP
  • Multithreading with GCD becomes a breeze
  • Most of your methods will conform to the needed syntax anyway.
  • Swift 2 compatibility
  • Multithreading with GCD becomes a breeze via WarpDrive
  • Supports Linux and swift build

Requirements

  • iOS 7.0+ / Mac OS X 10.9+ / Ubuntu 14.10
  • Xcode 7

Usage

For a full guide on how this implementation works the the series of blog posts about Functional Reactive Programming in Swift or the talk at UIKonf 2015 How to use Functional Reactive Programming without Black Magic.

Creating and updating a signal

let text = Signal<String>()

text.next { string in
  print("Hello \(string)")
}

text.update("World")

Mapping and transforming signals

let text = Signal<String>()

let greeting = text.map { subject in
  return "Hello \(subject)"
}

greeting.next { text in
  print(text)
}

text.update("World")

Use functions as transforms

let text = Signal<String>()
let greet: String->String = { subject in
  return "Hello \(subject)"
}
text
  .map(greet)
  .next { text in
    print(text)
  }
text.update("World")

Handle errors in sequences of functions

let text = Signal<String>()

func greetMaybe(subject: String)->Result<String> {
  if subject.characters.count % 2 == 0 {
    return .Success("Hello \(subject)")
  } else {
    let error = NSError(domain: "Don't feel like greeting you.", code: 401, userInfo: nil)
    return .Error(error)
  }
}

text
  .flatMap(greetMaybe)
  .next { text in
    print(text)
  }
  .error { error in
    print("There was a greeting error")
  }
text.update("World")

This also works for asynchronous functions

let text = Signal<String>()
func greetMaybe(subject: String, completion: Result<String>->Void) {
  if subject.characters.count % 2 == 0 {
    completion(.Success("Hello \(subject)"))
  } else {
    let error = NSError(domain: "Don't feel like greeting you.", code: 401, userInfo: nil)
    completion(.Error(error))
  }
}
text
  .flatMap(greetMaybe)
  .next { text in
    print(text)
  }
  .error { error in
    print("There was a greeting error")
  }
text.update(.Success("World"))

FlatMapping across a signal that returns signals

let baseCost = Signal<Int>()


let total = baseCost
    .flatMap { base in
        // Marks up the price
        return Signal(base * 2)
    }
    .map { amount in
        // Adds sales tax
        return Double(amount) * 1.09
    }

total.next { total in
    print("Your total is: \(total)")
}

baseCost.update(10)
baseCost.update(122)

/* Output:
Your total is: 21.8
Your total is: 265.96
*/

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

Dynamic frameworks on iOS require a minimum deployment target of iOS 8 or later. To use Interstellar with a project targeting iOS 7, you must include all Swift files directly in your project.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:

$ gem install cocoapods

To integrate Interstellar into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Interstellar'

Then, run the following command:

$ pod install

swift build

Add Interstellar to your Package.swift:

import PackageDescription

let package = Package(
  name: "Your Awesome App",
  targets: [],
  dependencies: [
    .Package(url: "https://github.com/jensravens/interstellar.git", majorVersion: 1),
  ]
)

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Interstellar into your Xcode project using Carthage, specify it in your Cartfile:

github "JensRavens/Interstellar"

FAQ

Why use Interstellar instead of [insert your favorite FRP framework here]?

Interstellar is meant to be lightweight. There are no UIKit bindings, no heavy constructs - just a simple Signal<T>. Therefore it's easy to understand and portable (there is no dependency except Foundation).


Credits

Interstellar is owned and maintained by Jens Ravens.

Changelog

  • 1.1 added compability with Swift 2. Also renamed bind to flatMap to be consistent with Optional and Array.
  • 1.2 Thread was moved to a new project called WarpDrive
  • 1.3 WarpDrive has been merged into Interstellar. Also Interstellar is now divided into subspecs via cocoapods to make it easy to just select the needed components. The basic signal library is now "Interstellar/Core".
  • 1.4 Support swift build and the new Swift package manager, including support for Linux. Also removed deprecated bind methods.

License

Interstellar is released under the MIT license. See LICENSE for details.

interstellar's People

Contributors

duemunk avatar filwag avatar icanzilb avatar jensravens avatar js avatar neonichu avatar nixterrimus avatar pyanfield avatar rcarlsen avatar readmecritic avatar ryanmasondavies avatar scelis avatar schwa avatar tonyarnold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kumarios orta artsy

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.