我在F#中有一个(char [] * int) []
。我想将整数提取到int[]
。
我有一个很棒的非功能版本。
let temp = new List<int>()
for i in population do
temp.Add(snd i)
有人可以帮忙吗?
答案 0 :(得分:2)
如果你想创建一个System.Generic.List<int>
,你可以这样做:
let temp = ResizeArray (population |> Seq.map snd)
如果你想要一个F#列表,你可以这样做:
let temp = population |> List.map snd
答案 1 :(得分:0)
这是我在F#interactive中所做的,将每个元组的第二部分提取到一个新数组中:
[|([|'a'; 'b'|], 123);([|'c'|], 456)|] |> Array.map snd;;
请注意,snd是Tuple.Second的简写
以下是详细版本:
Microsoft (R) F# Interactive version 12.0.30110.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> let pascalString0 = ([||], 0)
let pascalString1 = ([|'c'|], 1)
let pascalString2 = ([|'a'; 'b'|], 2)
let pascalStrings = [|pascalString0; pascalString1; pascalString2|]
let result = pascalStrings|> Array.map snd ;;
val pascalString0 : 'a [] * int
val pascalString1 : char [] * int = ([|'c'|], 1)
val pascalString2 : char [] * int = ([|'a'; 'b'|], 2)
val pascalStrings : (char [] * int) [] =
[|([||], 0); ([|'c'|], 1); ([|'a'; 'b'|], 2)|]
val result : int [] = [|0; 1; 2|]
>