SkillAgentSearch skills...

Dollar

A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript

Install / Use

/learn @ankurp/Dollar
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Dollar CocoaPods Reviewed by Hound

Dollar 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.js in Javascript.

Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language. It is now moved into a separate repo to support Swift Package Manager

NOTE: Starting Swift 4 $ is no longer a valid identifier. So you get the following error: '$' is not an identifier; use backticks to escape it . Instead use Dollar.

Contents

Setup

Using cocoapods version 0.36.x or greater

Add pod 'Dollar' to your Podfile and run pod install. Add use_frameworks! to the end of the Podfile. Also checkout this sample app.

Using Swift Package Manager

Add the following dependency .Package(url: "https://github.com/ankurp/Dollar", majorVersion: 7, minor: 1) to your Package.swift file and then run swift build. Requires swift version 2.2 or greater that you can install from https://swift.org

Using git submodule

  1. If you are using git then add Dollar as a submodule using git submodule add https://github.com/ankurp/Dollar.git. If not using git download the project using git clone https://github.com/ankurp/Dollar.git in your project folder.
  2. Open the Dollar folder. Drag Dollar.xcodeproj, inside the Dollar folder, into the file navigator of your Xcode project.
  3. In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar.
  4. In the tab bar at the top of that window, open the "Build Phases" panel.
  5. Expand the "Link Binary with Libraries" group, and add Dollar.framework.
  6. In your project file import Dollar and you can call all of the helper functions.

Still stuck. Then checkout this screencast on how to import

Support for Xcode and Swift

  • For Xcode 15 (Swift 5.1) use version 10.0.0
  • For Xcode 11 (Swift 5.0) use version 9.0.0
  • For Xcode 10 (Swift 4.2) use version 8.0.0
  • For Xcode 9 (Swift 4) use version 7.1.0
  • For Xcode 8 (Swift 3) use version 6.0.0
  • For Xcode 7 (Swift 2) use version 4.1.0 or 5.2.0
  • For Xcode 6.3 (Swift 1.2) use version 3.0.3
  • For Xcode 6.1 and 6.2 (Swift 1.1) use version 2.2.0

Demo Apps

Communication

  • If you need help, use gitter.im or post a question on Stack Overflow with tag dollar.swift.
  • If you'd like to ask a general question, use Stack Overflow.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Dollar Usage

Array

at - Dollar.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.

Dollar.at(["ant", "bat", "cat", "dog", "egg"], indexes: 0, 2, 4) 
=> ["ant", "cat", "egg"]

chunk - Dollar.chunk

Creates an array of elements split into groups the length of size. If array can’t be split evenly, the final chunk will be the remaining elements.

Dollar.chunk([1, 2, 3, 4], size: 2)
=> [[1, 2], [3, 4]]

Dollar.chunk([1, 2, 3, 4], size: 3)
=> [[1, 2, 3], [4]]

compact - Dollar.compact

Creates an array with all nil values removed.

Dollar.compact([3, nil, 4, 5]) 
=> [3, 4, 5]

Dollar.compact([nil, nil]) as NSObject[] 
=> []

contains - Dollar.contains

Checks if a given value is present in the array.

Dollar.contains([1, 2, 3, 1, 2, 3], value: 2) 
=> true

Dollar.contains([1, 2, 3, 1, 2, 3], value: 10) 
=> false

cycle - Dollar.cycle

Cycles through the array definetly or indefinetly passing each element into the callback function. The second parameter is to specify how many times to cycle through the array. If left out it will cycle indefinetly.

Dollar.cycle([1, 2, 3], 2) {
  print($0)
}
// Prints the following
123123

difference - Dollar.difference

Creates an array excluding all values of the provided arrays

Dollar.difference([1, 2, 3, 4, 5], [5, 2, 10]) 
=> [1, 3, 4]

each - Dollar.each

Passes each element in the array to the callback

Dollar.each(["A", "B"]) { 
  print("Value \($0)")
}
=> ["A", "B"]

Dollar.each(["A", "B"]) { (index, elem) in
  print("\(index) - \(elem)")
}
=> ["0 - A", "1 - B"]

every - Dollar.every

Checks if the given callback returns true value for all items in the array.

Dollar.every([1, 2, 3, 4], callback: { $0 < 20 }) 
=> true

Dollar.every([1, 2, 3, 4]) { $0 == 1 } 
=> false

factorial Dollar.factorial

Returns factorial of integer

Dollar.factorial(3)
=> 6

Dollar.factorial(0)
=> 1

fetch - Dollar.fetch

Get element from an array at the given index which can be negative to find elements from the end of the array. A default value can be returned if indexing out of bounds.

let arr = [1, 2, 3, 4, 5, 6, 7, 8]
Dollar.fetch(arr, 100)
=> nil

Dollar.fetch(arr, 100, orElse: 42)
=> 42

Dollar.fetch(arr, -1)
=> 8

fill - Dollar.fill

Fills elements of array with value from start up to, but not including, end. This method mutates array.

var arr = Array<Int>(count: 5, repeatedValue: 1)
Dollar.fill(&arr, withElem: 42)
=> [42, 42, 42, 42, 42]

var arr = Array<Int>(count: 5, repeatedValue: 1)
Dollar.fill(&arr, withElem: 42, startIndex: 1, endIndex: 3)
=> [1, 42, 42, 42, 1]

find - Dollar.find

Iterates over elements of an array and returning the first element that the callback returns true for.

Dollar.find([1, 2, 3, 4], callback: { $0 == 2 }) 
=> 2

Dollar.find([1, 2, 3, 4]) { $0 == 10 } 
=> nil

findIndex - Dollar.findIndex

This method is like find except that it returns the index of the first element that passes the callback check.

let arr = [["age": 36], ["age": 40], ["age": 1]]
let result = Dollar.findIndex(arr) { $0["age"] < 20 }
result 
=> 2

findLastIndex - Dollar.findLastIndex

This method is like findIndex except that it iterates over elements of the array from right to left.

let arr = [["age": 36], ["age": 40], ["age": 1]]
let result = Dollar.findLastIndex(arr) { $0["age"] > 30 }
result
=> 1

first - Dollar.first(array: AnyObject[])

Gets the first element in the array.

Dollar.first([1, 2, 3, 4])
=> 1

Dollar.first([]) 
=> nil

groupBy Dollar.groupBy

This method returns a dictionary of values grouped by the value returned by a callback.

Dollar.groupBy([1, 2, 3, 4, 5], callback: {$0 % 2})
=> [0: [2, 4], 1: [1, 3]]

Dollar.groupBy(["strings", "with", "different", lengths"], callback: {$0.characters.count})
=> [7: ["strings", "lengths"], 9: ["different"], 4: ["With"]]

second - Dollar.second(array: AnyObject[])

Gets the second element in the array.

Dollar.second([1, 2, 3, 4])
=> 2

Dollar.second([1]) 
=> nil

Dollar.second([])
=> nil

flatMap - Dollar.flatMap

Maps a function that converts elements to a list and then concatenates them.

let values = [2, 3, 4, 5, 6, 7]
Dollar.flatMap(values) { [$0, $0] }
=> [2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7]

flatMap - Dollar.flatMap

Maps a function that converts a type to an Optional over an Optional, and then returns a single-level Optional.

let url = NSURL(string: "https://apple.com/swift")
Dollar.flatMap(url) { $0.lastPathComponent }
=> Optional("swift")

Note: This is the same behavior as Optional chaining. The code above translates to

NSURL(string: "https://apple.com/swift/")?.lastPathComponent
=> Optional("swift")

flatten - Dollar.flatten

Flattens a nested array of any depth.

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

Dollar.flatten([[3], "Hello", 5]) as NSObject[] 
=> [3, "Hello", 5]

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

frequencies - Dollar.frequencies

This method returns a dictionary of values in an array mapping to the total number of occurrences in the array. If passed a function it returns a frequency table of the results of the given function on the arrays elements.

Dollar.frequencies(["a", "a", "b", "c", "a", "b"]) 
=> ["a": 3, "b": 2, "c": 1]

Dollar.frequencies([1, 2, 3, 4, 5]) { $0 % 2 == 0 }
=> [false: 3, true: 2]

gcd Dollar.gcd

GCD function return greatest common denominator with number passed

Dollar.gcd(3, 10)
=> 1

Dollar.gcd(3, 9)
=> 3

indexOf - Dollar.indexOf

Gets the index at which the first occurrence of value is found.

Dollar.indexOf([1, 2, 3, 1, 2, 3], value: 2) 
=> 1

Dollar.indexOf(["A", "B", "C"], value: "B") 
=> 1

Dollar.indexOf([3, 4, 5], value: 5) 
=> 2

Dollar.indexOf([3, 4, 5], value: 3) 
=> 0

Dollar.indexOf([3, 4, 5], value: 

Related Skills

View on GitHub
GitHub Stars4.3k
CategoryDevelopment
Updated9h ago
Forks363

Languages

Swift

Security Score

100/100

Audited on Mar 30, 2026

No findings