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) |> Array.map (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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s