xakpc
xakpc

Reputation: 1809

Combine several functions to one

Good day. Can you help me to combine several functions (string -> string -> bool) to one big function (string -> bool).

I have a (contactList:seq<string>) and function

let checkEqual localPhone azurePhone =
    localPhone = azurePhone

I did Seq.map to get seq of functions (string -> bool):

let checks = contactList |> Seq.map (fun x -> checkEqual x)

Now i need to combine this seq somehow to one big func (string -> bool) to use it in Where clause of Azure Mobile Services.

let! result = table.Where(fun y > checkAll y).ToEnumerableAsync() 
              |> Async.AwaitTask

I want to make expression like this: y == x1 || y == x2 || etc

Upvotes: 1

Views: 137

Answers (2)

Tarmil
Tarmil

Reputation: 11372

For a more general solution, you can write something like this:

checkAll y = checks |> Seq.fold (fun acc check -> acc || check y) false

acc is generally named like this because it is the accumulator -- the value that stores what has been computed so far.

The advantage of exists is that it will stop scanning the sequence as soon as it finds a true value, whereas fold always goes to the end of the sequence. But fold is more general: you can write a sum, a product, or really any global computation on your sequence with it.

Upvotes: 0

Ganesh Sittampalam
Ganesh Sittampalam

Reputation: 29110

For this particular scenario you can use Seq.exists:

checkAll y = checks |> Seq.exists (fun f -> f y)

Upvotes: 2

Related Questions