简体   繁体   English

F#中的列表,数组和元组有什么区别?

[英]What is difference between lists, arrays, and tuples in F#?

What is difference between lists, arrays, and tuples in F#? F#中的列表,数组和元组有什么区别? Is it easy to convert between them when required? 在需要时,它们之间的转换是否容易? They seem all similar in spirit, so what do actually I need to know that lets me understand when to use one versus another? 它们在精神上看起来都很相似,所以我真正需要知道的是什么才能让我明白何时使用它?

A tuple is a grouping of unnamed, ordered values. 元组是一组未命名的有序值。 Each value in a tuple does not need to be the same type, which means you can have a tuple defined like: 元组中的每个值不需要是相同的类型,这意味着您可以将元组定义为:

let someTuple = (1, "foo", 42.3)

This would create a tuple containing an int, string, and float value. 这将创建一个包含int,string和float值的元组。

A list is an ordered collection of values of the same type which is immutable. 列表相同类型的值的有序集合,它是不可变的。 This is probably the most common collection type used in functional programming, as it's completely immutable and has a lot of functionality for creating lists built on top of previously created lists, which allows you to make collections that "grow" (they're actually new collections, however, as the lists are immutable). 这可能是函数式编程中最常用的集合类型,因为它完全不可变,并且具有许多用于创建基于先前创建的列表构建的列表的功能,这使得您可以创建“增长”的集合(它们实际上是新的然而,集合,因为列表是不可变的)。

An array is a fixed size, mutable collection. 数组是固定大小的可变集合。 They are very efficient to create, but must always be a single type. 它们非常有效,但必须始终是单一类型。

Is it easy to convert between them when required? 在需要时,它们之间的转换是否容易?

It's very easy to convert between lists and arrays. 在列表和数组之间进行转换非常容易。 You can use List.ofArray , List.toArray , Array.ofList , and Array.toList to convert between the types. 您可以使用List.ofArrayList.toArrayArray.ofListArray.toList在类型之间进行转换。

Converting from tuple to array or list is not common, and not always possible, as tuples allow multiple types to be stored within them. 从元组转换为数组或列表并不常见,并且并非总是可行,因为元组允许在其中存储多种类型。

They seem all similar in spirit, so what do actually I need to know that lets me understand when to use one versus another? 它们在精神上看起来都很相似,所以我真正需要知道的是什么才能让我明白何时使用它?

Lists and arrays are both used for collections. 列表和数组都用于集合。 In general, you'll want to prefer lists if you're going to be making lists that "grow", as making a new list comprised of an element + the original list is much more efficent than doing the same thing with an array. 一般来说,如果你要制作“增长”的列表,你会希望更喜欢列表,因为制作一个由元素组成的新列表+原始列表比使用数组做同样的事情要好得多。

Arrays are often used for higher performance scenarios (they have better memory locality, are more space efficient, etc), but are mutable and fixed size, so they're often not ideal when you're trying to work with constructing collections. 数组通常用于更高性能的场景(它们具有更好的内存局部性,更节省空间等),但是它们是可变的和固定的大小,所以当你尝试构建集合时它们通常不是理想的。

Tuples are typically used for completely different scenarios. 元组通常用于完全不同的场景。 The most common use case for tuples is to pass around multiple items as one value. 元组最常见的用例是将多个项目作为一个值传递。 This happens automatically when using framework methods that have out parameters, for example, so you'll see use cases like: 例如,当使用具有out参数的框架方法时会自动发生这种情况,因此您将看到如下用例:

let (success, value) = int.TryParse(someString)

In this case, the tuple is automatically created and then pattern matched to extract the values in a single line of code. 在这种情况下,自动创建元组,然后模式匹配以在单行代码中提取值。 Instead of thinking of a tuple as a "collection", it's more of a way to hold multiple values, often of different types, together. 它不是将元组视为“集合”,而是将多个值(通常是不同类型)保持在一起的方式。

About using collection types: 关于使用集合类型:

Lists when : You need a dynamic collection that changes in size. 列表时间 :您需要一个大小变化的动态集合。 List is a one directional linked list so keep in mind that every member of list requires 4 or 8 bytes of additional memory storing pointer to next list element. List是单向链表,因此请记住,list的每个成员都需要4或8个字节的附加内存,用于存储指向下一个list元素的指针。

Arrays when : You need to store large amount of primitive values, like ints, floats or bytes. 数组在以下情况 :您需要存储大量原始值,如整数,浮点数或字节。 Arrays represent a static block of memory reserved for the values, this is efficient when you have to store for example bytes of an image. 数组表示为值保留的静态内存块,当您必须存储例如图像的字节时,这是有效的。

Additional useful collections: 其他有用的集合:

Sets when : You need to store, well a set. 设置时间 :您需要存储,一组。 Set is a collection of unique values. Set是一组唯一值。 Set has useful functions to calculate difference, union and intersection. Set具有用于计算差异,并集和交集的有用函数。 For example you might have set of all people (A) and set of people who bought candy (B) so you can now calculate A - B = C, now you have set C which represents people who didn't buy candy. 例如,您可能拥有所有人(A)和购买糖果(B)的人的集合,因此您现在可以计算A - B = C,现在您已设置C代表不购买糖果的人。

Maps when : You need a projection from one value to another. 以下情况下映射 :您需要从一个值到另一个值的投影。 Now in previous example you calculated set C (people who didn't buy candy), you probably used some id to describe these people. 现在在前面的例子中你计算了集合C(不买糖果的人),你可能用一些id来描述这些人。 Now you can project from this id to the person record using Map :). 现在,您可以使用Map :)从此id投影到人员记录。

I hope this was useful. 我希望这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM