在Swift中的东西就像C#中的LINQ一样 [英] Is something in Swift like LINQ in C#

查看:138
本文介绍了在Swift中的东西就像C#中的LINQ一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Swift相对较新,但我想知道Swift是否有像 LINQ 在C#中?

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

使用LINQ我指的是所有优秀的工具,如标准查询运算符 匿名类型 对象初始化程序等。

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

推荐答案

Swift结合了.net中捆绑在一起的几个功能作为LINQ,虽然可能不是一个开箱即用的意义。

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.

匿名类型与Swift中具有命名值的元组非常接近。

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

在C#中:


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

输出:史密斯

在Swift中:


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

输出:史密斯

LINQ查询当然非常强大/富有表现力,但您可以使用 map 复制他们所做的大部分工作,在Swift中过滤 reduce 。使用 lazy ,您可以获得与创建可以提前循环的对象相同的功能,并且只在实际发生循环时对其进行评估:

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:

在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());
}


在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泛型使编写操作与现有LINQ功能类似,非常简单。例如,来自 LINQ维基百科文章

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


计数
Count运算符计算给定集合中的元素数。带谓词的重载,计算与谓词匹配的元素数。

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.

可以像这样用Swift编写(2.0协议扩展语法):

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

你可以如果元素符合 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具有类似对象初始化器的语法:

Structs by default have object-initializer-like syntax:

struct Person { let name: String; let age: Int; }

let person = Person(name: "Fred Bloggs", age: 37)

并通过 ArrayLiteralConvertible ,任何集合类型都可以具有与集合初始化程序语法类似的语法:

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

let list: MyListImplementation = [1,2,3,4]

这篇关于在Swift中的东西就像C#中的LINQ一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆