Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Add an element to a sequence

Tags:

f#

a simple question I cannot find an answer to: how to add an element to a sequence? Eg I have a seq and a newElem XElement I'd like to append to it.

Thanks

like image 422
pistacchio Avatar asked Jul 20 '09 14:07

pistacchio


People also ask

Facebook itu apa sih?

Facebook adalah media sosial dan layanan jejaring sosial online Amerika yang dimiliki oleh Meta Platforms.


2 Answers

Seq.append:

> let x = { 1 .. 5 };;

val x : seq<int>

> let y = Seq.append x [9];; // [9] is a single-element list literal

val y : seq<int>

> y |> Seq.toList;;
val it : int list = [1; 2; 3; 4; 5; 9]
like image 125
Juliet Avatar answered Oct 14 '22 05:10

Juliet


You can also use

let newSeq = Seq.append oldSeq (Seq.singleton newElem)

Which is a slight modification of the first answer but appends sequences instead of a list to a sequence.

given the following code

let startSeq = seq {1..100}
let AppendTest = Seq.append startSeq [101] |> List.ofSeq
let AppendTest2 = Seq.append startSeq (Seq.singleton 101) |> List.ofSeq
let AppendTest3 = seq { yield! startSeq; yield 101 } |> List.ofSeq

looped 10000 executions the run times are

Elapsed 00:00:00.0001399
Elapsed 00:00:00.0000942
Elapsed 00:00:00.0000821

Take from that what you will.

like image 42
killspice Avatar answered Oct 14 '22 07:10

killspice