F # one list to another?

I have a list of tuples with three values ​​in tuples

I want to create a new list of rows from the previous list with one value from tuples.

List [(string * string * int) ]

List[ for i in columns -> i.getfirstvalueintuple]

How can i do this? very simple question, but I can not understand.

Also, is there another way to create another type of list or seq from an existing list?

+3
source share
4 answers
seq { for (first, _, _) in lst do yield first };;

This gives consistency. You can also use List.map:

lst |> List.map (fun (first, _, _) -> first);;

Which gives a list.

+8
source

You just need List.map with the "project" function:

let newList = List.map (fun (x, _, _) -> x) orgList

List.maptakes a function and a list and applies the function to each item in the list. (fun (x, _, _) -> x)decompresses the tuple and returns the first element.

NTN, Rob

+8
source

2/3 , :

let newList = oldList |> List.collect ( fun (a,b,_) -> [a;b])
0

fst , fst :

lst |> List.map fst

(* 'a*'b*'c list -> 'a list *)
-3

Source: https://habr.com/ru/post/1750536/


All Articles