f# one list to another?
I have a list of tuples with three values in tuples
I want to create new List of strings out of previous list with one value out of tuples.
List [(string * st开发者_开发百科ring * int) ]
List[ for i in columns -> i.getfirstvalueintuple]
How can i do that? very basic question but i can't figure it out.
Also is there any other way of building another kind of list or seq out of existing list?
seq { for (first, _, _) in lst do yield first };;
This gives a sequence. You can also use List.map
:
lst |> List.map (fun (first, _, _) -> first);;
Which gives a list.
You just need a List.map with a 'project' function:
let newList = List.map (fun (x, _, _) -> x) orgList
The List.map
takes a function and a list and applies the function to each item in the list. The (fun (x, _, _) -> x)
unpacks the tuple and returns the first item.
HTH, Rob
If you wanted 2/3 of the values you could also do this:
let newList = oldList |> List.collect ( fun (a,b,_) -> [a;b])
The builtin function fst
extracts the first element of a tuple, so you can just map fst
over the list:
lst |> List.map fst
(* 'a*'b*'c list -> 'a list *)
精彩评论