Giter Club home page Giter Club logo

optionalextensions's Introduction

OptionalExtensions

CocoaPods Swift 4.0 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.

Operators

filter: (Wrapped -> Bool) -> Optional<Wrapped>

let number: Int? = 3

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

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

mapNil: (Void -> Wrapped) -> Optional<Wrapped>

let number: Int? = 3
number.mapNil { 2 } // .Some(3)

let nilledNumber: Int? = nil
nilledNumber.mapNil { 2 } // .Some(2)

flatMapNil: (Void -> Optional<Wrapped>) -> Optional<Wrapped>

let number: Int? = 3
number.flatMapNil { .Some(2) } // .Some(3)

let nilledNumber: Int? = nil
nilledNumber.flatMapNil { .Some(2) } // .Some(2)

then: (Wrapped -> 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

maybe: U -> (Wrapped -> U) -> U (similar to Haskell's maybe)

let number: Int? = 3
number.maybe(100) { $0 + 1 } // 4

let nilledNumber: Int? = nil
nilledNumber.maybe(100) { $0 + 1 } // 100

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

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

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

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

let number: Int? = 3
let sameNumber = number.onNone { print("Hello World") } // .Some(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"

CocoaPods:

pod "OptionalExtensions"

Manually:

Grab the OptionalExtensions.swift file and drop it in your project.

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 dentelezhkin avatar dmcrodrigues avatar gergelyorosz avatar riteshhgupta avatar ruiaaperes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

optionalextensions's Issues

Sugary syntax version

This is not an issue!

I'm just interested in your opinion of the code below, it adds no additional functionality, hence, not an issue. As an exercise for my own entertainment, I've created a version of the extension that utilises as much sugary syntax as I could muster (using single expressions per function and using if case syntax when apt, etc).

Your original is written in a clear, explicit style which is easy to decipher, the version below is undoubtedly more cryptic (one thing I do like is that each function contains one expression and one return statement, but it is arguable how important that is). I think the versions of onSome and onNone would fit with the current style, but the others are a bit of a departure.

Anyway, I thought I'd throw it out there to see what you thought.

All the best,
Al

public extension Optional {
    func filter(@noescape predicate: Wrapped -> Bool) -> Optional {
        return map(predicate) == .Some(true) ? self : .None
    }

    func replaceNil(with replacement: Wrapped) -> Optional {
        return self ?? replacement
    }

    func apply(@noescape f: Wrapped -> Void) {
        if case let wrapped? = self { f(wrapped) }
    }

    func onSome(@noescape f: Wrapped -> Void) -> Optional {
        apply(f)
        return self
    }

    func onNone(@noescape f: Void -> Void) -> Optional {
        if isNone { f() }
        return self
    }

    var isSome: Bool {
        return map { _ in true } ?? false
    }

    var isNone: Bool {
        return !isSome
    }
}

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.