Last year I mentioned an Underscore.js inspired library providing a number of helper methods for working with Collections in Swift.
Here’s an open source library for working with Swift sequences and collections inspired by LINQ in .NET called SINQ from Leszek Ślażyński.
SINQ provides many helper methods for working with methods and collections, and allows you to chain those messages.
This code snippet from the readme shows a number of examples of SINQ in use:
// or less (linq | sql)-esque
let nums2 = sinq([1, 4, 2, 3, 5]).filter{ $0 > 2 }.orderBy{ $0 }.map{ 2 * $0 }
// path1 : String = …, path2: String = …
let commonComponents = sinq(path1.pathComponents)
.zip(path2.pathComponents) { ($0, $1) }
.takeWhile { $0 == $1 }
.count()
let prefixLength = sinq(path1).zip(path2){ ($0, $1) }.takeWhile{ $0 == $1 }.count()
// available : String[] = …, blacklist : String[] = …
let next = sinq(available).except(blacklist){ $0 }.firstOrDefault("unknown")
// employees : Employee[] = …
let headCnt = sinq(employees).groupBy{ $0.manager }.map{ ($0.key, $0.values.count()) }
let allTasks = from(employees).selectMany{ $0.tasks }.orderBy{ $0.priority }
let elite1 = sinq(employees).whereTrue{ $0.salary > 1337 }.orderBy{ $0.salary }
let elite2 = from(employees).orderBy{ $0.salary }.takeWhile{ $0.salary > 1337 }
// products : Product[] = …, categories : Category[] = …
let breadcrumbs = sinq(categories).join(inner: products,
outerKey: { $0.id },
innerKey: { $0.categoryId },
result: { "($0.name) / ($1.name)" })
You can find SINQ on Github here.
A nice interface for working with Swift collections and sequences.
Original article: Open Source Swift Library Providing LINQ Inspired Syntax For Using Sequences And Collections
©2015 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.