TheSharperDev

Educating about C# and F#

Seq.append - F#

Continuing on with the F# standard library, we come to Seq.append.

Wraps the two given enumerations as a single concatenated enumeration. ~ FSharp.Core

Seq.append concats two sequences together. Here is a sample of how to use it:

Using a double forward pipe:

let append1 s1 s2 = 
    (s1, s2)
    ||> Seq.append

[<EntryPoint>]
let main argv =
    let seq1 = seq { 1 .. 4 }
    let seq2 = seq { 5 .. 8 }
    let result = append1 seq1 seq2
    result 
        |> Seq.iter (fun x -> printfn "%O" x)
    0

Passing parameters:

let append2 s1 s2 = 
    Seq.append s1 s2

[<EntryPoint>]
let main argv =
    let seq1 = seq { 1 .. 4 }
    let seq2 = seq { 5 .. 8 }
    let result = append2 seq1 seq2
    result 
        |> Seq.iter (fun x -> printfn "%O" x)
    0

Passing parameters explicitly declaring parentheses:

let append3 (s1: seq<'T>, s2: seq<'T>) = 
    Seq.append s1 s2

[<EntryPoint>]
let main argv =
    let seq1 = seq { 1 .. 4 }
    let seq2 = seq { 5 .. 8 }
    let result = append3(seq1, seq2)
    result 
        |> Seq.iter (fun x -> printfn "%O" x)
    0

All output the following:

1
2
3
4
5
6
7
8

Notes

  • Both sequences have to be the same type, can’t mix strings and ints.
  • Notice ont eh third example, I have to be explicit with my parameters when calling append3. This is because my defining append3 I’m requiring both parameters to be provided as once, instead of leveraging partial application.
    • The first two appends’s signatures are: seq<'a> -> seq<'a> -> seq<'a>
    • The third’s signature is: seq<'T> * seq<'T> -> seq<'T>