Giter VIP home page Giter VIP logo

sweeft's Introduction

Sweeft

Swift but a bit Sweeter - More Syntactic Sugar for Swift

This is a collection of extensions and operators that make swift a bit sweeter. I have added these from multiple projects where I've been using these.

Note: These operators are supposed to help me in the way I write Swift. Which is a functional style. So most of these regard possible problems and annoyances with functional programming in Swift.

Please Contribute to make Swift a bit cooler looking... Post your ideas in the issues as enhancements

Installing

To add this to your project you have to add this to your Podfile:

pod Sweeft

Usage

Operators

(|) Pipe

Will pipe the left value to the function to the right. Just like in Bash:

value | function

is the same as:

function(value)

(=>) Map with

This will call map with the function to the right:

array => function

is the same as:

array.map(function)

(==>) FlatMap with

The same as above but with flatMap.

(|>) Filter with

The same as above but with filter

( ** ) Drop input/output from function

Will cast a function to allow any input and drop it.

**{
    // Do stuff
}

is the same as:

{ _,_ in
    // Do stuff
}

or as a postfix it will drop the output

{
    return something
}**

is equivalent to:

{
    _ = something
}

(<-) Assignment of non-nil

Will assign b to a if b is not nil

a <- b

is equivalent to:

a = b ?? a

(<-) Assign result of map

Will assign the result of a map to an array.

array <- handler

is equivalent to:

array = array.map(handler)

If the handler returns an optional, but the array can't handle optionals then it will drop all of the optionals.

(<|) Assign result of filter

Will assign the result of a filter to the array

array <| handler

is the same as:

array = array.filter(handler)

(!) Will remove all the optional values from an array

let array = [1, nil, 3]
!array // [1, 3]

(<=>) Will swap the values of two variables

// a = 1 and b = 2
a <=> b // a = 2 and b = 1

(.?) Will unwrap an optional. Is not it will give the types default value

Note: the type has to conform to the Defaultable protocol.

let i: Int? = nil
let j: Int? = 2

i.? // 0
j.? // 2

(.?) Check for nil

Will check if a value is not nil

.?myVariable

is equivalent to:

myVariable != nil

sweeft's People

Contributors

nerdsupremacist avatar

Watchers

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