Home

Awesome

Interstellar

Build Status CocoaPods Version CocoaPods Plattforms Carthage compatible

The simplest Observable<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

Requirements


Usage

For a full guide on how this implementation works see 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 = Observable<String>()

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

text.update("World")

Mapping and transforming observables

let text = Observable<String>()

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

greeting.subscribe { text in
  print(text)
}

text.update("World")

Use functions as transforms

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

Handle errors in sequences of functions

let text = Observable<String>()

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

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

This also works for asynchronous functions

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

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

Flatmap is also available on observables

let baseCost = Observable<Int>()

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

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

baseCost.update(10) // prints "Your total is: 21.8"
baseCost.update(122) // prints "Your total is: 265.96"


Communication

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. 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: 2),
  ]
)

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 Observable<T>. Therefore it's easy to understand and portable (there is no dependency except Foundation).

Also Interstellar is supporting BYOR (bring your own Result<T>). Due to its protocol based implementation you can use result types from other frameworks directly with Interstellar methods.


Credits

Interstellar is owned and maintained by Jens Ravens.

Changelog

License

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