Slide 1

Slide 1 text

FizzBuzz in Swift Abizer Nasir | @abizern | abizern.org Geek, Raconteur, Jobseeker

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

The Set Up You’ve turned up for an interview…

Slide 4

Slide 4 text

FizzBuzz • For some set of input numbers: • If the number is a multiple of 3 print “Fizz” • If the number is a multiple of 5 print “Buzz” • If the number is a multiple of 3 and 5 print “FizzBuzz” • Otherwise, Print the number.

Slide 5

Slide 5 text

Demo

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Simple Implementation func fizzBuzz (number: Int) -> String { var output = "" ! if number % 3 == 0 { output += "Fizz" } ! if number % 5 == 0 { output += "Buzz" } ! if output.isEmpty { output = "\(number)" } return output }

Slide 8

Slide 8 text

Pattern Matching func fizzBuzz (number: Int) -> String { switch (number % 5, number % 3) { case (0, 0): return "FizzBuzz" case (0, _): return "Buzz" case (_, 0): return "Fizz" default: return "\(number)" } }

Slide 9

Slide 9 text

FizzBuzzBar • Same as FizzBuzz, except • For multiples of 7 add the string “Bar” • e.g. 105 -> “FizzBuzzBar”

Slide 10

Slide 10 text

Simple solution func fizzBuzzBar(number: Int) -> String { switch (number % 7, number % 5, number % 3) { case (0, 0, 0): return "FizzBuzzBar" case (0, 0, _): return "BuzzBar" case (_, 0, 0): return "FizzBuzz" case (0, _, 0): return "FizzBar" case (0, _, _): return "Bar" case (_, 0, _): return "Buzz" case (_, _, 0): return "Fizz" default: return "\(number)" }

Slide 11

Slide 11 text

Pass Options func fizzBuzzBar(number: Int, #options: Array<(Int, String)>) -> String { var output = "" for (divisor, description) in options { if number % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(number)" } ! return output }

Slide 12

Slide 12 text

for i in 1...105 { let options = [(3, "Fizz"), (5, "Buzz"), (7, "Bar")] println(fizzBuzzBar(i, options: options)) }

Slide 13

Slide 13 text

Extension extension Int { func fizzBuzzBar(options: Array<(Int, String)>) -> String { var output = "" for (divisor, description) in options { if self % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(self)" } return output } }

Slide 14

Slide 14 text

for i in 1...105 { let options = [(3, "Fizz"), (5, "Buzz"), (7, "Bar")] println(i.fizzBuzzBar(options)) }

Slide 15

Slide 15 text

Protocol protocol FizzBuzzable { func fizzBuzz(options: Array<(Int, String)>) -> String }

Slide 16

Slide 16 text

extension Int: FizzBuzzable { func fizzBuzzBar(options: Array<(Int, String)>) -> String { var output = "" for (divisor, description) in options { if self % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(self)" } return output } }

Slide 17

Slide 17 text

But what if they change to strings?

Slide 18

Slide 18 text

FizzBuzz with Strings • For a list of strings: • If the string contains the word “Three” print “Fizz” • If the string contains the word “Five” print “Buzz” • If the string contains the words “Three” and “Five” print “FizzBuzz”

Slide 19

Slide 19 text

let stringInput = ["one", "three", "ThreeFive", "EleventyOne"] for string in stringInput { var result = "" ! if string.rangeOfString("three", options: .CaseInsensitiveSearch) { result += "Fizz" } ! if string.rangeOfString("five", options: .CaseInsensitiveSearch) { result += "Buzz" } ! if result.isEmpty { result = string } ! println(result) }

Slide 20

Slide 20 text

Generic Protocol protocol FizzBuzzable { typealias ItemType func fizzBuzz(options: Array<(ItemType, String)>) -> String }

Slide 21

Slide 21 text

extension Int: FizzBuzzable { typealias ItemType = Int func fizzBuzz(options: Array<(Int, String)>) -> String { var output = "" for (divisor, description) in options { if self % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(self)" } ! return output } }

Slide 22

Slide 22 text

extension String: FizzBuzzable { typealias ItemType = String func fizzBuzz(options: Array<(String, String)>) -> String { var output = "" for (substring, description) in options { if self.rangeOfString(substring, options: .CaseInsensitiveSearch) { output += description } ! if output.isEmpty { output = self } ! return output } }

Slide 23

Slide 23 text

for x in 1..20 { let options = [(3, "Fizz"), (5, "Buzz")]; println(x.fizzBuzz(options)) } ! for x in ["three", "six", "ninetyfive"] { let options = [("three", "Fizz"), ("five", "Buzz")] println(x.fizzBuzz(options)) }

Slide 24

Slide 24 text

So now we can use a typed array

Slide 25

Slide 25 text

let typedArray: FizzBuzzable[] = [1, 3, 5, 15, "three", "five", "wibble"]

Slide 26

Slide 26 text

for x in typedArray { let intOptions = [(3, "Fizz"), (5, "Buzz")] let strOptions = [("three", "Fizz"), ("five", "Buzz")] var output = "" ! if let num = x as? Int { output = num.fizzBuzz(intOptions) } else if let str = x as? String { output = str.fizzBuzz(strOptions) } ! println(output) }

Slide 27

Slide 27 text

Don’t try to do things the same way as you did in Objective-C

Slide 28

Slide 28 text

Think of types and interfaces rather than classes and methods

Slide 29

Slide 29 text

Learn a functional programming language

Slide 30

Slide 30 text

fizzBuzz :: Integer -> String fizzBuzz n | n `mod` 15 == 0 = "FizzBuzz" | n `mod` 5 == 0 = "Fizz" | n `mod` 3 == 0 = "Buzz" | otherwise = show n

Slide 31

Slide 31 text

fizzBuzz :: Integer -> String fizzBuzz n | n `mod` 15 == 0 = "FizzBuzz" | n `mod` 5 == 0 = "Fizz" | n `mod` 3 == 0 = "Buzz" | otherwise = show n func fizzBuzz (number: Int) -> String { if number % 15 == 0 { return “FizzBuzz” } else if number % 5 == 0 { return "Buzz" } else if number % 3 == 0 { return "Fizz" } else { return “\(number)” } }

Slide 32

Slide 32 text

• http://learnyouahaskell.com • http://book.realworldhaskell.org

Slide 33

Slide 33 text

Write the code, change the world.

Slide 34

Slide 34 text

Find the bugs, file the radars

Slide 35

Slide 35 text

“We have every opportunity and every encouragement before us, to form the noblest purest constitution on the face of the earth. We have it in our power to begin the world over again” Thomas Paine, Common Sense, 1776

Slide 36

Slide 36 text

Thank You Abizer Nasir | @abizern | abizern.org Geek, Raconteur, Jobseeker