Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is something in Swift like LINQ in C#

Tags:

I know that Swift is relatively new yet, but I would like to know if Swift have anything like LINQ in C#?

With LINQ I mean all the excellent tools like Standard Query Operators, Anonymous types, Object Initializer, etc.

like image 719
Victor Sigler Avatar asked Apr 15 '15 16:04

Victor Sigler


People also ask

Is LINQ only for C#?

LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.

What is LINQ used for in C?

LINQ provides functions to query cached data from all kinds of data sources. The data source could be a collection of objects, database or XML files.

Is LINQ Faster C#?

Most of the times, LINQ will be a bit slower because it introduces overhead. Do not use LINQ if you care much about performance. Use LINQ because you want shorter better readable and maintainable code. So your experience is that LINQ is faster and makes code harder to read and to maintain?

Is LINQ in .NET core?

Using LINQ in MVC . NET Core LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats. LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced.


2 Answers

Swift incorporates several of the features that are bundled together in .net as LINQ, though possibly not in quite an out-of-the-box sense.

Anonymous types are quite close to tuples with named values in Swift.

In C#:

   var person = new { firstName = "John", lastName = "Smith" };    Console.WriteLine(person.lastName); 

Output: Smith

In Swift:

var person = (firstName: "John", lastName: "Smith") person.firstName = "Fred" print(person.lastName) 

Output: Smith

LINQ queries are of course very powerful/expressive, but you can replicate a large portion of what they do using map, filter and reduce in Swift. With lazy, you can get the same functionality where you create an object that can be looped over ahead of time, and only evaluate it when the looping actually happens:

In C#:

var results =  SomeCollection     .Where(c => c.SomeProperty < 10)     .Select(c => new {c.SomeProperty, c.OtherProperty});  foreach (var result in results) {     Console.WriteLine(result.ToString()); } 

In Swift:

// just so you can try this out in a playground... let someCollection = [(someProperty: 8, otherProperty: "hello", thirdProperty: "foo")]  let results =   someCollection.lazy     .filter { c in c.someProperty < 10 }     // or instead of "c in", you can use $0:     .map { ($0.someProperty, $0.otherProperty) }  for result in results {     print(result) } 

Swift generics make writing operations similar to existing LINQ functionality quite straightforward. For example, from the LINQ wikipedia article:

Count The Count operator counts the number of elements in the given collection. An overload taking a predicate, counts the number of elements matching the predicate.

Could be written in Swift like this (2.0 protocol extension syntax):

extension SequenceType {     // overload for count that takes a predicate     func count(match: Generator.Element -> Bool) -> Int {         return reduce(0) { n, elem in match(elem) ? n + 1 : n }     } }  // example usage let isEven = { $0 % 2 == 0 }  [1,1,2,4].count(isEven)  // returns 2 

You could also overload it to take a particular element to count if the element conforms to Equatable:

extension SequenceType where Generator.Element: Equatable {     // overload for count that takes a predicate     func count(element: Generator.Element) -> Int {         return count { $0 == element }     } }  [1,1,2,4].count(1) 

Structs by default have object-initializer-like syntax:

struct Person { let name: String; let age: Int; }  let person = Person(name: "Fred Bloggs", age: 37) 

and via ArrayLiteralConvertible, any collection type can have similar syntax to collection initializer syntax:

let list: MyListImplementation = [1,2,3,4] 
like image 174
Airspeed Velocity Avatar answered Sep 28 '22 05:09

Airspeed Velocity


I'd personally use CoreData or NSPredicate for some LINQ like functionality. It meshes with Swift I'm sure. I've only used it in Objective-C, but I've seen articles people implementing it with Swift.

http://nshipster.com/nspredicate/

like image 20
apollosoftware.org Avatar answered Sep 28 '22 06:09

apollosoftware.org