Git Product home page Git Product logo

fission's Introduction

Fission

A collection of functions and custom operators for function composition, function application, the monadic functions, and some other helpful things.

Mostly an attempt to consolidate my most used/useful functions/operators into one place that has all of these things, as well as provide more flexibility than similiar frameworks.

What is included:

Operators for flatMap (>>-, -<<)

Operator for map (<^>)

Operators to apply a function to a parameter (|>, <|)

Functions curry and uncurry.

Function compact.

Functions apply and operators for each version included <*>, and <->.

Usage Examples

|> and <| - the function application operators

Consider a line of code like this:

display(lines(readFromFile("file://allwords.txt")))

It can be pretty hard to see in what way the data is flowing or to reason about what function is called first. With the |> operator, we can write it like this:

"file://allwords.txt" |> readFromFile |> lines |> display

This makes it clear, from left to right, how your data is flowing.

<| can be used the same way, except from right to left.

<^> - the map operator

The <^> is an operator that just calls through to map on either Optional or CollectionType.

For example:

Optional
let optionalInt: Int? = ...
let mapped: String? = String.init <^> optionalInt
CollectionType

let integers = toInt <^> ["1", "2"]

compact

Let's say you have a collection (lets say Array for now) that contains optionals (so they type signature is something like Array<String?> - compact is a function that can convert that array into something of type Array<String> as such:

let array = ["Swift", Optional<String>.None, "Kenny"] which is of type Array<String?> - you can easily remove the optionals like so with Fission's compact function; compact(array).

-<< and >>- - the flatMap operators

Calls through to flatMap on Optional or SequenceType

Optional
let string: String? = ...
let flatMapped: Int? = string >>- toInt

or let flatMapped: Int? = toInt -<< string

SequenceType

let allCharaters: [Character] = characters -<< ["hello", "my", "friend"] // returns an array of every character in all the strings

curry and uncurry

curry transforms a function that takes two arguments into a function that returns another function - both of which take one argument.

This can be useful if you want to partially apply a function that is not already curried. Take for example this function:

func add(first: Double, second: Double) -> Double { return first + second }

If you wanted to partially apply this for any reason (maybe you want to pass a function of type Double -> Double into a higher order function) you can use the curry function as so:

let curriedAddition = curry(add)(4.0)

and you called the function with 3.0 curriedAddition(3.0) - the result will be 7.0

uncurry works in the opposite way, so if you did uncurry(curry(add)) - you end up with a function with the same signature as the original add function.

apply

A simple example using apply is as follows:

let splitCharacters = ["me", "file"].apply([{ return $0.characters.sort() }])

splitCharacters's value is [["e", "m"], ["e", "f", "i", "l"]] - which is one array of characters per string in the original Array, in an array.

Another version of apply is included, that works on functions that take an inout parameter, and return a function that does not and returns a value. For example: since mutating funcs do not have a return value (they return Void) - you must bind to a var like so:

var first = ["First"] 
first.appendContentsOf(["Second"])

A lot of the time, you just want to bind the mutation to a let variable in one statement, which is the problem the version of apply solves. The example above can be written as so using Fission:

let both = apply(Array.appendContentsOf)(["First"])(["Second"])

Or, using the operator <-> (and <|)

let both = Array.appendContentsOf <-> ["First"] <| ["Second"]

Integration

CocoaPods:

You must have use_frameworks! at the beginning of your podfile.

Add pod 'Fission' to your podfile.

Next steps:

  1. Better tests (the current ones are not good)
  2. Complete header docs
  3. Carthage support

fission's People

Contributors

pearapps avatar readmecritic avatar

Stargazers

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