Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nghialv/try
Swift µframework providing Try<T>
https://github.com/nghialv/try
Last synced: 4 months ago
JSON representation
Swift µframework providing Try<T>
- Host: GitHub
- URL: https://github.com/nghialv/try
- Owner: nghialv
- License: mit
- Created: 2015-06-10T19:03:00.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-09-16T16:51:13.000Z (over 9 years ago)
- Last Synced: 2024-10-03T12:39:57.205Z (5 months ago)
- Language: Swift
- Homepage:
- Size: 189 KB
- Stars: 32
- Watchers: 4
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Try
=====[](https://developer.apple.com/swift)
[]
(https://github.com/Carthage/Carthage)
[](http://mit-license.org)
[](https://github.com/nghialv/Try/issues?state=open)Swift µframework providing Try<T>.
This library is inspired by the `Try` implementation in Scala.
##### Without `Try`
``` swift
enum FileError: ErrorType {
case FileNotFound
case Unknown
}
func lineCountOfFile(filename: String) throws -> Int {
if exists(filename) {
let file = open(filename)
return file.lineCount
} else {
throw FileError.FileNotFound
}
}
// traditional way with Swift 2.0
do {
let lineCount = try lineCountOfFile("data.text")
// do something
} catch {
// error handling
}
```##### Code with `Try`
``` swift
let t = Try(try lineCountOfFile("data.text"))switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
```- `map` `<^>`
```swift
let t = Try(try lineCountOfFile("data.text")).map { $0 * 5 }switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
```- `flatMap` `>>-`
```swift
let t = Try(try lineCountOfFile("data.text")).flapMap { Try(try doSomething($0)) }switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
```- Operators
``` swift
let t = Try(try lineCountOfFile("data.text")) <^> { $0 * 5} >>- { Try(try doSomething($0)) }switch t {
case .Success(let lines): print(lines)
case .Failure(let error): print(error)
}
```Installation
------ Using Carthage
> - Insert `github "nghialv/Try"` to your Cartfile
> - Run `carthage update`- Using Cocoapods
> - Insert `use_frameworks!` to your Podfile
> - Insert `pod "Try"` to your Podfile
> - Run `pod install`- Using Submodule
Requirements
------ Swift 2.0 (Xcode 7.0 or later)
- iOS 8.0 or later