• Stars
    star
    182
  • Rank 210,662 (Top 5 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 5 years ago

Reviews

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

Repository Details

Swift µframework with extensions for the Optional Type

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

More Repositories

1

UIViewController-Swizzled

Used to print the structure of your application as you use it
Objective-C
847
star
2

Receiver

Swift µframework implementing the Observer pattern 📡
Swift
239
star
3

Reactor

Powering your RAC architecture
Swift
184
star
4

iOSArchitecture

Sample project showing a common architecture I use when creating iOS Projects
Objective-C
157
star
5

Swift-Sugar

Swift's Sugar. Heavily inspired on Objc Sugar(https://github.com/supermarin/ObjectiveSugar)
Swift
153
star
6

Tangerine

Swift µframework for fetching images 🍊
Swift
149
star
7

Rinku

A simple networking library
Swift
43
star
8

KirKos

Focus on the most important
Objective-C
34
star
9

SaferFonts

Avoiding potential mistypes while using fonts programatically in Swift
Swift
30
star
10

RPNSURLConnection-Swizzled

NSURLConnection's category for the request and stack trace output
Objective-C
26
star
11

RPDynamicWarningView

A simple warning view that takes advantage of UIKit Dynamics
Objective-C
22
star
12

NSURLConnection-Blocks

Category that allows the dev to specify blocks for the success and failure cases
Objective-C
13
star
13

UITextField-DelegationBlocks

Add blocks, as per a single instance basis, to your UITextFields
Objective-C
12
star
14

NSObject-Dealloc

Category used to know when your object has been deallocated
Objective-C
7
star
15

8472

Strava client, with focus activities analysis. 🏃‍♂️
Swift
6
star
16

JSaaS

Jaden Smith as a Service
Elixir
6
star
17

NSPortoWorkshop

A detailed description about a potential Workshop that could be organised in Porto, Portugal.
5
star
18

SliderController

Objective-C
5
star
19

fartlek

Strava Webhooks + APNS
Python
5
star
20

RPGallery

Gallery used to upload pictures
Objective-C
4
star
21

TeamGen

Generating balanced teams 🚀🏈
Swift
4
star
22

OctifyIssues

Used as Issues Tracker for the App Octo Alarm
3
star
23

FootballTeamGenerator

...because just random is not good enough
Swift
3
star
24

RuiAAPeres

2
star
25

ReversePolishNotation-Swift

Reverse Polish Notation done in Swift, based on http://learnyousomeerlang.com/functionally-solving-problems#rpn-calculator
Swift
2
star
26

but_the_android_team_uses_RxJava

this is a parody
Swift
2
star
27

OctifyPush

APNS for my iOS App (Octify)
Go
2
star
28

TestingFRP

Some benchmarks
Swift
2
star
29

SquareStock

Square Stock
Objective-C
1
star
30

Refactoring

Swift
1
star
31

Franz

Swift
1
star
32

Reading18

A list of material (articles + books) I read + wrote in 2018.
1
star
33

swiftAveiroFP

FP stuff with Argo Fun
Swift
1
star
34

principles

1
star
35

PromisesPlayground

Objective-C
1
star