How to filter in Swift using where

In F#, it”s common to pipeline a chain of commands together, perhaps filtering an input array before mapping with a function:

let xs = [| 1; 2; 3; 4; 5 |]
xs |> Array.filter (fun x -> x > 2) |> (fun x -> x*x)

Marcin Krzyzanowski wrote a neat post showing how Swift uses where in a number of cases to achieve similar filtering.

The fact is you can use where keyword in a case label of a switch statement, a catch clause of a do statement, or in the case condition of an if, while, guard, for-in statement, or to define type constraints

Leave a comment

Filed under Programming, Swift

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.