Convert string [] [] [] to string [] [] in f #
I'm having trouble with F # because I'm learning. I have something like the following code:
let A = [| [| [|"1";"Albert"|];[|"2";"Ben"|] |];[| [|"1";"Albert"|];[|"3";"Carl"|] |] |]
(Type A: string[][][]
)
I am trying to convert A to:
let B = [| [|"1"; "Albert" |] ; [| "2"; "Ben"|] ; [| "3"; "Carl"|] |]
(Type B: string[][]
)
I do not know how to do that. I've tried multiple for
and recursive function, but I don't get it.
Here are some other options for implementing this to better understand the concept of this type:
let A = [| [| [|"1";"Albert"|];[|"2";"Ben"|] |];[| [|"1";"Albert"|];[|"3";"Carl"|] |] |]
//Matthew answer
//This is exactly what you were asking for.
//It takes all the subarrays and combines them into one
A |> Array.concat
|> Seq.distinct
|> Seq.toArray
//This is the same thing except it combines it with a transformation step,
//although in your case, the transform isn't needed so the transform
//function is simply `id`
A |> Seq.collect id
|> Seq.distinct
|> Seq.toArray
//The same as the second one except using a comprehension.
//This form makes it somewhat more clear exactly what is happening (iterate
//the items in the array and yield each item).
//The equivalent for the first one is `[|for a in A do yield! a|]`
[for a in A do for b in a -> b]
|> Seq.distinct
|> Seq.toArray
You can use Array.concat
to include string[][][]
in string[][]
and then Seq.distinct
to remove duplicate arrays of strings.
let b =
[| [| [|"1";"Albert"|];[|"2";"Ben"|] |];[| [|"1";"Albert"|];[|"3";"Carl"|] |] |]
|> Array.concat
|> Seq.distinct
|> Seq.toArray