SharePoint Newbie
SharePoint Newbie

Reputation: 6082

Is there a way to write this in F#?

let is_sum_greater_than_10 list =
    list
    |> Seq.filter (filter)
    |> Seq.sum
    |> (10 >)

This does not compile. Lookng at the last line "|> (10 >)" is there a way to write this such that the left is pipelined to the right for binary operators?

Thanks

Upvotes: 6

Views: 268

Answers (2)

elmattic
elmattic

Reputation: 12174

You can use a partial application of the < operator, using the (operator-symbol) syntax:

let is_sum_greater_than_10 list =
    list
    |> Seq.filter filter
    |> Seq.sum
    |> (<)10

You can also see this as an equivalent of a lambda application:

let is_sum_greater_than_10 list =
    list
    |> Seq.filter filter
    |> Seq.sum
    |> (fun x y -> x < y)10

or just a lambda:

let is_sum_greater_than_10 list =
    list
    |> Seq.filter filter
    |> Seq.sum
    |> (fun y -> 10 < y)

Upvotes: 7

user4649737
user4649737

Reputation:

You can use a slightly modified version of your example, albeit this is in an infix expression notation:

let ``is sum greater than 10`` filter list =
    (list
        |> Seq.filter filter
        |> Seq.sum) > 10

Upvotes: 0

Related Questions