简体   繁体   中英

F# How to convert a list of tuples into a parrallel array

I am new to the functional programming and F#

I'm trying to convert a list of tuples into parallel lists, for example

let results = [("foo",3);("bar", 4)};("bazz", 8)]
// do something to convert it
// output = ["foo";"bar";"bazz"], output2 = [3;4;8]

What I attempted to was this

let issue = []
let count = []

for tpl in results do
    fst tpl |> issue
    snd tpl |> count

But obviously this wont compile.

I am getting the list of tuples from the statement

let results = IssueData |> Seq.countBy id |> Seq.toList 

How would I go about doing this?

for the first part: there is List.unzip :

> let results = [("foo",3);("bar", 4);("bazz", 8)];;

val results : (string * int) list = [("foo", 3); ("bar", 4); ("bazz", 8)]

> let (issue, count) = List.unzip results;;

val issue : string list = ["foo"; "bar"; "bazz"]
val count : int list = [3; 4; 8]

if I get you right this is all you want to know so you can write

let (issues, count)= IssueData |> Seq.countBy id |> Seq.toList |> List.unzip

as well.

remark

  • In case you are wondering - you still need the Seq.toList in F# 4 as there still is no Seq.unzip ;) (it's one of the few things that did not get normalized to all of List/Array/Seq)
  • if you are new to FP/F# it is a nice exercise to reimplement List.unzip yourself (hint: as always there are the two cases: [] and (a,b):rest )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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