Giter Club home page Giter Club logo

result's Introduction

Result

Codacy Badge Travis

Simple Swift μ-framework that wraps your throwing functions results.

How to install

CocoaPods

CocoaPods CocoaPods

Podfile:

pod 'YetAnotherResult'

Carthage

Carthage compatible

Cartfile:

github "Skogetroll/Result" >= 1.2.8

Swift package manager

Package.swift:

import PackageDescription

let package = Package(
  
  dependencies: [
    
    .Package(url: "https://github.com/Skogetroll/Result.git", majorVersion: 1, minor: 2),
    
  ]
  
)

How to use?

Like this:

let result = Result<Type> {
    // Your unsafe code resulting in Type or Error goes here
}

or like this:

let result = Result<Type>(/* Your unsafe code resulting in Type or Error goes here */)

How to get value?

let value: Type? = result.value

How to get error?

let error: ErrorType? = result.error

How to crash application return back to throwing paradigm?

let value: Type = try result.unwrap()

What else can I do with Result<V>?

You can use

let resultString = Result<String>(try unsafelyGetString())
let stringLength = resultString.map { string in
  string.characters.count
}

// stringLength now contains either value of `String.Index.Distance` or error wrapped in Result<Distance>

// Or you can use operator `<^>` to perform map
process <^> resultString
// Here `process : String -> Void` gonna be called if and only if resultString resulted successfully
let someResult = Result(try unsafelyGetResult())
let processedResult = someResult.flatMap { value in
	return Result(try someUnsafeProcessing(value))
}

// or you can use operators `>>-` and `-<<`

let processedResult = someResult >>- { value in Result(try someUnsafeProcessing(value)) }
let resultedFunction: Result<A -> B> = 
let resultedValue: A = 

let result: Result<B> = resultedValue.apply(resultedFunction)

// or you can use operator `<*>`

let result: Result<B> = resultedFunction <*> resultedValue
func yourThrowingFunction(in: InType) throws -> (out: OutType) {
	
}

let resultWrappedFunction: InType -> Result<OutType> = wrap(yourThrowingFunction)

let someInput: InType = 

// And we can get
let resultOutput = resultWrappedFunction(someInput)

// instead of
do {
	let output = try yourThrowingFunction(someInput)
}

result's People

Contributors

skogetroll avatar readmecritic avatar

Watchers

xu avatar

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.