Giter Club home page Giter Club logo

optionalextensions's Introduction

OptionalExtensions

Swift 2.1 License MIT

Why?

Swift's Optional is pretty awesome, but it can always get better. This repository is an humble attempt to add some utility methods to it.

####filter: (T -> Bool) -> Optional<T>

let number: Int? = 3

let biggerThan2 = number.filter { $0 > 2 } // .Some(3)


let biggerThan3 = number.filter { $0 > 3 } // .None

####replaceNil: T -> Optional<T>

let number: Int? = 3
number.replaceNil(with: 2) // .Some(3)

let nilledNumber: Int? = nil
nilledNumber.replaceNil(with: 2) // .Some(2)

####then: (T -> Void) -> Void (similar to [T]'s forEach)

let number: Int? = 3
number.then { print($0) } // prints "3"

let nilledNumber: Int? = nil
nilledNumber.then { print($0) } // print won't be called

####onSome: (T -> Void) -> Optional<T> (injects a side effect in the .Some branch)

let number: Int? = 3
let sameNumber = number.onSome { print($0) } // prints "3" & returns .Optional(3)

let nilledNumber: Int? = nil
let sameNilledNumber = nilledNumber.onSome { print($0) } // .None

####onNone: (Void -> Void) -> Optional<T> (injects a side effect in the .None branch)

let number: Int? = 3
let sameNumber = number.onNone { print("Hello World") } // .Optional(3)

let nilledNumber: Int? = nil
let sameNilledNumber = nilledNumber.onNone { print("Hello World") } // prints "Hello World" & returns .None

####isSome: Bool

let number: Int? = 3
let isSome = number.isSome // true

let nilledNumber: Int? = nil
let isSome = nilledNumber.isSome // false

####isNone: Bool

let number: Int? = 3
let isSome = number.isNone // false

let nilledNumber: Int? = nil
let isSome = nilledNumber.isNone // true

Setup

Carthage:

github "RuiAAPeres/OptionalExtensions"

Contributing

We will gladly accept Pull Requests with new methods or improving the ones that already exist. Documentation, or tests, are always welcome as well. ❤️

License

OptionalExtensions is licensed under the MIT License, Version 2.0. View the license file

Copyright (c) 2015 Rui Peres

optionalextensions's People

Contributors

alskipp avatar ruiaaperes avatar

Watchers

 avatar  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.