• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift µframework providing Future<T, Error>

Future

Language CocoaPods [Carthage compatible] (https://github.com/Carthage/Carthage) License Issues

Swift µframework providing Future<T, Error>.

This library is inspired by the talk of Javier Soto at SwiftSubmit2015 and the Future implementation in Scala.

And this is using antitypical/Result.

Why we need Future?

Traditional async code
 func requestRepository(repoId: Int64, completion: (Repository?, NSError?) -> Void) {}
 func requestUser(userId: Int64, completion: (User?, NSError?) -> Void) {}
 
 // get owner info of a given repository
 requestRepository(12345) { repo, error in
 	if let repo = repo {
 		requestUser(repo.ownerId) { user, error in
 		   if let user = user {
 		       // do something
 		   } else {
 		       // error handling
 		   }
 		}
 	} else {
 		// error handling
 	}
 }
 
Code with Future
let future = requestRepository(12345)
		.map { $0.ownerId }
		.flatMap(requestUser)

future.onCompleted { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}

Shorthand by using operator

let future = requestRepository(12345) <^> { $0.ownerId } >>- requestUser

future.onCompleted { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}

Usage

  • map <^>
let f = requestUser("nghialv") <^> { $0.id }

f.onSuccess { userId in
	println(userId)
}
  • flatMap >>-
let f = searchRepositories("Hakuba") <^> { $0.first!.ownerName } >>- requestUser

f.onComplete { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}
  • filter
let e = NSError(domain: "noSuchElement", code: 1, userInfo: nil)
let f1 = searchRepositories("Hakuba")

let f = f1.filter(e){ $0.count > 0 } <^> { $0.first!.ownerName } >>- requestUser

f.onComplete { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}
  • andThen
// side-effect
var reposCount = 0
        
let f1 = searchRepositories("Hakuba")
let f2 = f1.andThen { result in
    switch result {
        case .Success(let repos): reposCount = repos.value.count
        case .Failure(let error): break
    }
}
let f3 = f2 <^> { $0.first!.ownerName } >>- requestUser
        
f3.onComplete { result in
    switch result {
        case .Success(let user):   println(user)
        case .Failure(let error):  println(error)
    }
}
  • recover

  • zip

let f1 = searchRepositories("Future")
let f2 = requestUser("nghialv")
        
let f3 = f1.zip(f2)

f3.onSuccess { repos, user in
	println(repos)
	println(user)
}
  • flatten

Installation

  • Using Carthage
  • Insert github "nghialv/Future" to your Cartfile
  • Run carthage update
  • Using Cocoapods
  • Insert use_frameworks! to your Podfile
  • Insert pod "Future" to your Podfile
  • Run pod install
  • Using Submodule

Requirements

  • Swift 1.2 (Xcode 6.3 or later)
  • iOS 8.0 or later

More Repositories

1

MaterialKit

Material design components for iOS written in Swift
Swift
2,504
star
2

promviz

Visualize the traffic of your clusters in realtime from Prometheus data
Go
955
star
3

Hakuba

🌸 Cellmodel-driven tableview manager
Swift
474
star
4

Transporter

A tiny library makes uploading and downloading easier
Swift
452
star
5

Net

Http Request wrapper written in Swift
Swift
302
star
6

Sapporo

Cellmodel-driven collectionview manager
Swift
246
star
7

GCD

A wrapper of Grand Central Dispatch written in Swift
Swift
72
star
8

Try

Swift µframework providing Try<T>
Swift
32
star
9

openGL-tankgame

A simple 3D game using openGL
C++
23
star
10

TVDataSource

datasource class for uitableview
Objective-C
17
star
11

gamegl

the bottom part of puzzle dragon game using OpenglES (without any framework)
Objective-C
14
star
12

iBall

iPhone, iPad間のバトルゲーム (OpenGLES)
C
6
star
13

VersionTracker

Tracking the app version
Swift
4
star
14

clibs

Central repository containing C libraries for testing
C
3
star
15

opencv-optical-flow

OpenCV, Python
Python
3
star
16

xcode_project_templates

xcode project templates
Objective-C
3
star
17

Kinect-ARToolKit

some simple examples of using Kinect (OpenNI) and ARToolKit
C++
2
star
18

vlcamera

vlcamera app
Objective-C
2
star
19

funnytext

render text in some shapes, change the position and orientation of each character
Objective-C
2
star
20

opencv_ios

opencv (face, eyes... tracking)
1
star
21

nghialv.github.com

my blog
HTML
1
star
22

flex-bison-cpp

a simple example of flex, bison
C++
1
star
23

tomojisho

a facebook game which looks like a dictionary for your friends. Through this game, you can review the information of your friends
PHP
1
star
24

rubyss

ruby shell script
Ruby
1
star