处理列表

创建列表

列表(List)是一个有序的值的集合。 你可以用方括号创建一个列表,周围的值用空格和/或逗号隔开 (以方便阅读)。 例如,[foo bar baz] 或 [foo, bar, baz]。

更新列表

当列表数据从管道流入时,你可以向列表中更新(update)和插入(insert)值,例如,让我们在列表的中间插入值10:

1. > [1, 2, 3, 4] | insert 2 10
2. # [1, 2, 10, 3, 4]

我们也可以使用update将第二个元素的值替换为10:

1. > [1, 2, 3, 4] | update 1 10
2. # [1, 10, 3, 4]

除了 update 和 insert 之外,还有 prepend 和 append,它们分别让你插入到列表的开头或列表的结尾。例如:

1. let colors = [yellow green]
2. let colors = ($colors | prepend red)
3. let colors = ($colors | append purple)
4. echo $colors # [red yellow green purple]

迭代列表

要遍历一个列表中的元素,可以使用each命令和 Nu 代码块 指定对每一个元素做什么操作。块参数(例如{ |it| echo $it }中的|it|)通常是当前的列表元素,但如果需要,通过 --numbered(-n) 标志可以将其改为包含index和item值的元素。比如:

1. let names = [Mark Tami Amanda Jeremy]
2. $names | each { |it| $"Hello, ($it)!" }
3. # Outputs "Hello, Mark!" and three more similar lines.

5. $names | each -n { |it| $"($it.index + 1) - ($it.item)" }
6. # Outputs "1 - Mark", "2 - Tami", etc.

where命令可以用来创建一个列表的子集,高效地根据一个条件过滤列表。

下面的例子得到所有名称以 “e” 结尾的颜色:

1. let colors = [red orange yellow green blue purple]
2. echo $colors | where ($it | str ends-with 'e')

在这个例子中,我们只保留大于7的数字:

1. # The block passed to where must evaluate to a boolean.
2. # This outputs the list [orange blue purple].

4. let scores = [7 10 8 6 7]
5. echo $scores | where $it > 7 # [10 8]

reduce命令从一个列表计算一个单一的值。 它使用了一个代码块,该块有两个参数:当前元素(即 it)和一个累加器 (即 acc)。如果想要给累加器指定一个初始值,请使用 --fold (-f) 标志。 若要改变it使其具有index和item两个值,请添加--numbered(-n)标志。 例如:

1. let scores = [3 8 4]
2. echo "total =" ($scores | reduce { |it, acc| $acc + $it }) # 15

4. echo "total =" ($scores | math sum) # easier approach, same result

6. echo "product =" ($scores | reduce --fold 1 { |it, acc| $acc * $it }) # 96

8. echo $scores | reduce -n { |it, acc| $acc + $it.index * $it.item } # 3 + 1*8 + 2*4 = 19

访问列表

要访问一个给定索引的列表元素, 可以使用$name.index形式, 其中$name是持有列表的变量。

例如,下面列表中的第二个元素可以用$names.1来访问:

1. let names = [Mark Tami Amanda Jeremy]
2. $names.1 # gives Tami

如果索引在某个变量$index中,我们可以使用get命令从列表中提取该元素:

1. let names = [Mark Tami Amanda Jeremy]
2. let index = 1
3. $names | get $index # gives Tami

length命令返回列表中的元素个数。例如,[red green blue] | length输出3。

empty? 命令确定一个字符串、列表或表格是否为空。它可以与列表一起使用,如下所示:

1. let colors = [red green blue]
2. $colors | empty? # false

4. let colors = []
5. $colors | empty? # true

in 和 not-in 运算符用于测试一个值是否在一个列表中,例如:

1. let colors = [red green blue]
2. 'blue' in $colors # true
3. 'yellow' in $colors # false
4. 'gold' not-in $colors # true

any?命令用于确定一个列表中是否有任意一个元素匹配给定的条件,例如:

1. # Do any color names end with "e"?
2. echo $colors | any? ($it | str ends-with "e") # true

4. # Is the length of any color name less than 3?
5. echo $colors | any? ($it | str length) < 3 # false

7. # Are any scores greater than 7?
8. echo $scores | any? $it > 7 # true

10. # Are any scores odd?
11. echo $scores | any? $it mod 2 == 1 # true

all?命令确定一个列表中是否所有元素都匹配给定的条件。例如:

1. # Do all color names end with "e"?
2. echo $colors | all? ($it | str ends-with "e") # false

4. # Is the length of all color names greater than or equal to 3?
5. echo $colors | all? ($it | str length) >= 3 # true

7. # Are all scores greater than 7?
8. echo $scores | all? $it > 7 # false

10. # Are all scores even?
11. echo $scores | all? $it mod 2 == 0 # false

转换列表

flatten命令通过将嵌套列表中的元素添加到顶层列表中来从现有的列表创建一个新列表。这条命令可以被多次调用,以使任意嵌套深度的列表变平。例如:

1. echo [1 [2 3] 4 [5 6]] | flatten # [1 2 3 4 5 6]

3. echo [[1 2] [3 [4 5 [6 7 8]]]] | flatten | flatten | flatten # [1 2 3 4 5 6 7 8]

wrap命令将一个列表转换为一个表格。每个列表的值将都会被转换为一个单独的行和列:

1. let zones = [UTC CET Europe/Moscow Asia/Yekaterinburg]

3. # Show world clock for selected time zones
4. $zones | wrap 'Zone' | upsert Time {|it|
5. (
6. date now
7. | date to-timezone $it.Zone
8. | date format '%Y.%m.%d %H:%M'
9. )
10. }