Coder Social home page Coder Social logo

applicativeswift's Introduction

ApplicativeSwift

ApplicativeSwift provides the operators to realize the applicative style for Swift, which is used in Haskell.

let a: Int? = 2
let b: Int? = 3

let result: Int? = (+) <^> a <*> b // Optional(5)

Purpose

We use a lot of Optional values in Swift. Sometimes we want to apply operators or functions to 2 or more optional values. But it is too complicated.

let a: Int? = 2
let b: Int? = 3

// How to calculate a + b? (wants nil if a or b is nil)

// Too complicated
let result: Int? = {
    if let a0 = a {
        if let b0 = b {
            return a0 + b0
        }
    }
    return nil
}()

In Haskell, it can be done easily in the applicative style.

-- Haskell
(+) <$> a <*> b

ApplicativeSwift provides such operators for Swift. Because the letter $ cannot be used for operators in Swift, ApplicativeSwift provides <^> instead of <$>. Therefore a + b can be calculated in the following way.

// Swift
(+) <^> a <*> b

Usage

// Optional
let a: Int? = 2
let b: Int? = 3
let c: Int? = 5
let d: Int? = 7
let e: Int? = 11
let f: Int? = 13
let g: Int? = 17

(+) <^> a <*> b
sum3 <^> a <*> b <*> c
sum4 <^> a <*> b <*> c <*> d
sum5 <^> a <*> b <*> c <*> d <*> e
sum6 <^> a <*> b <*> c <*> d <*> e <*> f
sum7 <^> a <*> b <*> c <*> d <*> e <*> f <*> g

// Array
(*) <^> [1, 2] <*> [3, 4]

// Currying
curry(+)(2) <*> .Some(3)
[curry(+), curry(*)] <*> [1, 2] <*> [3, 4]

Installation

Carthage

Carthage is available to install ApplicativeSwift. Add it to your Cartfile:

github "koher/ApplicativeSwift" ~> 2.0.0

Manually

Embedded Framework

For iOS 8 or later,

  1. Put ApplicativeSwift.xcodeproj into your project in Xcode.
  2. Click the project icon and select the "General" tab.
  3. Add ApplicativeSwift.framework to "Embedded Binaries".
  4. import ApplicativeSwift in your swift files.

Source

For iOS 7, put Applicative.swift into your project.

License

The MIT License

References

  1. Applicative functors - Learn You a Haskell for Great Good!
  2. Advanced Operators - The Swift Programming Language

applicativeswift's People

Contributors

koher avatar

Stargazers

HOANG Bao Tin avatar Astemir Eleev avatar Karsten Gresch avatar Shingo Sato avatar Pawel Rusin avatar Tatsuya Kawano avatar Henry S avatar yoshimaa avatar ikemai avatar Takayuki_Sei avatar Tobias Deekens avatar KOSUGI Tomo avatar Manoj Mahapatra avatar terasaka avatar David Rico avatar Ushio avatar Tim Kersey avatar  avatar Dennis Farandy avatar Pin Shih Wang avatar Francisco J. Belchi avatar Evgeniy Yurtaev avatar Boris Bügling avatar Dave Lee avatar Javi avatar

Watchers

 avatar James Cloos avatar Ushio avatar

Forkers

kokoron

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.