微信号:SwiftDev

介绍:探讨移动开发技术,推荐常用框架,分享移动互联网资讯. 主要提供Swift;IOS等相关技术资讯

使用Dollar.$wift简化代码编写

2014-06-15 23:43 Swift开发

$ is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to Lo-Dash or Underscore in Javascript.


Contents

  • Setup

  • Usage

    • Array

    • Dictionary

    • Object

    • Function

    • Chaining

  • Examples

  • Contributing

  • Roadmap

  • Why?

Setup

Include the Dollar.swift into your project and import the library using import Dollar

Currently there are issues loading the library using pod 'Dollar', '~> 0.2'

Usage

Array Methods

Method Usage
$.at Creates an array of elements from the specified indexes, or keys, of the collection. Indexes may be specified as individual arguments or as arrays of indexes.
$.first Gets the first element in the array.
$.compact Creates an array with all nil values removed.
$.contains Checks if a given value is present in the array.
$.difference Creates an array excluding all values of the provided arrays
$.every Checks if the given callback returns true value for all items in the array.
$.find Iterates over elements of an array and returning the first element that the callback returns true for.
$.findIndex This method is like find except that it returns the index of the first element that passes the callback check.
$.findLastIndex This method is like findIndex except that it iterates over elements of the array from right to left.
$.flatten Flattens a nested array of any depth.
$.frequencies This method returns a dictionary of values in an array mapping to the total number of occurances in the array.
$.indexOf Gets the index at which the first occurrence of value is found.
$.initial Gets all but the last element or last n elements of an array.
$.intersection Creates an array of unique values present in all provided arrays.
$.join Joins the elements in the array to create a concatenated element of the same type.
$.last Gets the last element from the array.
$.lastIndexOf Gets the index at which the last occurrence of value is found.
$.rest The opposite of initial this method gets all but the first element or first n elements of an array.
$.noop A no-operation function.
$.min Retrieves the minimum value in an array.
$.max Retrieves the maximum value in an array.
$.pluck Retrieves the value of a specified property from all elements in the array.
$.pull Removes all provided values from the given array.
$.range Creates an array of numbers (positive and/or negative) progressing from start up to but not including end.
$.sequence Creates an array of an arbitrary sequence. Especially useful with builtin ranges.
$.remove Removes all elements from an array that the callback returns true.
$.sortedIndex Gives the smallest index at which a value should be inserted into a given the array is sorted.
$.union Creates an array of unique values, in order, of the provided arrays.
$.uniq Creates a duplicate-value-free version of an array.
$.without Creates an array excluding all provided values.
$.xor Creates an array that is the symmetric difference of the provided arrays.
$.zip Creates an array of grouped elements, the first of which contains the first elements of the given arrays.
$.zipObject Creates an object composed from arrays of keys and values.

Dictionary Methods

Method Usage
$.keys Creates an array of keys given a dictionary.
$.values Creates an array of values given a dictionary
$.merge Merges all of the dictionaries together and the latter dictionary overrides the value at a given key
$.pick Creates a shallow clone of a dictionary composed of the specified keys.
$.omit Creates a shallow clone of a dictionary excluding the specified keys.

Object Methods

Method Usage
$.tap Invokes interceptor with the object and then returns object.

Function Methods

Method Usage
$.after Creates a function that executes passed function only after being called n times.
$.bind Creates a function that, when called, invokes func with the binding of arguments provided.
$.partial Creates a function that, when called, invokes func with any additional partial arguments prepended to those provided to the new function.
$.times Call a function n times and also passes the index. If a value is returned in the function then the times method will return an array of those values.

Chaining

$(array: ...)

Method Usage
any Returns true if callback function returns true for at least one element in the array
all Returns true if callback function returns true for all elements in the array
each Passes each element value to the callback function
filter Filters the arrary to elements for which the callback function returns true
first Returns the first element in the array and terminated the chain
flatten Flattens a nested array of any depth.
initial Gets all but the last element or last n elements of an array.
map Maps each element to the new value returned in the callback function
value Returns the array after applying all of the chained operators on it.

Examples

$.first([1, 2, 3]) as Double == 1

$.first([]) == nil

$.noop() == nil

$.compact([3, nil, 4, 5]) as NSObject[] == [3, 4, 5]

$.compact([nil, nil]) as NSObject[] == []

$.flatten([[3], 4, 5]) as Int[] == [3, 4, 5]


 
Swift开发 更多文章 协同写作的力量——中国开发者9天完成《Swift语言》中文版 程序员眼中的苹果Swift语言:简单 易学 高效 「高考准考证」生成器,穿越回18岁那年,好紧张! App Store应用审查时间缩短 已不到24小时 “爆炸性”新闻!未来谷歌Android操作系统可能基于苹果Swift
猜您喜欢 如何在ES6中管理类的私有数据 品味淡淡的感觉 淡淡的生活 【RxJava && Agera】从源码简要分析基本调用流程 【2015 SACC】手机淘宝性能优化全记录