Functions Supporting IEnumerable Interface in F#

Introduction

Today I am going to explain about the Functions supporting IEnumerable Interface and how they work with Collections in F#. F# gives a seq alias to IEnumerable interface that's why the module is called seq. why we give this alias name to the IEnumerable interface. The reason for this is to make it easy to read and Type and we use this short name for type definition.

These Functions names are given below.

Function Name

exists and forall

fold

concat

iter and map

choose

unfold

cast

init and initinfinite

find,filter and tryFind

exists and forall

When you want to make an assertion about the content of the collection then both functions are helpful in the situation.

Example

Exixts-and-Forall-example

let intArray = [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]
let existsMultipleOfTwo =
    intArray |>
    Seq.exists (fun x -> x % 2 = 0)
let allMultipleOfTwo =
    intArray |>
    Seq.forall (fun x -> x % 2 = 0)
printfn "existsMultipleOfTwo: %b" existsMultipleOfTwo
printfn "allMultipleOfTwo: %b" allMultipleOfTwo

Output

exists-and-forall-Output

fold

When you want to create summary of the list by folding the items in the collection together this function is helpful.

Example

Fold Example

let myPhrase = [|"How"; "are"; "you"; "Dea?"|]
let myCompletePhrase =
    myPhrase |>
    Seq.fold (fun acc n -> acc + " " + n) ""
printfn "%s" myCompletePhrase

Output

Fold Output

concat

If you want to concatenate a collection of collections into one collection then this function is used.

Example

concat Example

open System.Collections.Generic
let myList =
    let temp = new List<int[]>()
    temp.Add([|5; 6; 7|])
    temp.Add([|8; 9; 10|])
    temp.Add([|11; 12; 13|])
    temp
let myCompleteList = Seq.concat myList
myCompleteList |> Seq.iter (fun n -> printf "%i ... " n)

Output

concat Output

iter and map

Both the functions let you apply a given function to every item in the collection.

Example

iter-and-map Example

let myArr = [|4; 5; 6|]
let myNewCollection =
    myArr |>
    Seq.map (fun N -> N * 3)
printfn "%A" myArr
myNewCollection |> Seq.iter (fun N -> printf "%i ... " N)

Output

iter-and-map-output

choose

You can perform a filter and a map task at the same time with the help of this function.

Example

choose-function Example

let floatArray = [|0.5; 0.50; 0.75; 1.0; 1.25; 1.50; 1.75;2.0 |]
let integers =
    floatArray |>
    Seq.choose
(fun n ->
   let a = n * 2.0
     let b = floor a
     if a- b = 0.0 then
        Some (int b)
     else
     None)
integers |> Seq.iter (fun n -> printf "%i ... " n)

Output

choose-function Output

unfold

If you want to initialize a list then this provides a more flexible way for initialization.

Example

unfold-function Example
let fibs =
    (1,1) |> Seq.unfold
    (fun (a0, a1) ->
    Some(a0, (a1, a0 + a1)))
let first10 = Seq.take 20 fibs
printfn "%A" first10

Output

unfold Output

cast

If you want to convert a nongeneric version of an IEnumerable interface to IEnumerable<T>. This function provides a way to do this.

Example

open System.Collections
open System.Collections.Generic
let floatArrayList =
    let temp = new ArrayList()
    temp.AddRange([| 1.0; 2.0; 3.0 |])
    temp
let (typedFloatSeq: seq<float>) = Seq.cast floatArrayList

Output

init and initinfinite

These functions helps to initialize the collections.

Example

init Example

let tenOnes = Seq.init 10 (fun _ -> 2)
let allIntegers = Seq.initInfinite (fun n -> System.Int32.MinValue + n)
let firstTenInts = Seq.take 10 allIntegers
tenOnes |> Seq.iter (fun n -> printf "%i ... " n)
printfn ""
printfn "%A" firstTenInts

Output

init-and-initinfinite Output

find filter and tryfind

These function are used and helpful when you want to choose an element from a list that meets a certain condition.

Example

find-filter Example
let shortWordList = [|"hat"; "hot"; "bat"; "lot"; "mat"; "dot"; "rat";|]
let atWords =
    shortWordList
    |> Seq.filter (fun x -> x.EndsWith("at"))
let otWord =
    shortWordList
    |> Seq.find (fun x -> x.EndsWith("ot"))
let ttWord =
    shortWordList
    |> Seq.tryFind (fun x -> x.EndsWith("tt"))
atWords |> Seq.iter (fun x -> printf "%s ... " x)
printfn ""
printfn "%s" otWord
printfn "%s" (match ttWord with | Some x -> x | None -> "Not found")

Output

Find-Filter Output

Summary

In this article I have discussed about Function Supporting IEnumerable interface in F#.


Similar Articles