thinking in f#

12
Thinking in F# Mike Clement @mdclement [email protected] http://blog.softwareontheside.com

Upload: michael-clement

Post on 06-May-2015

282 views

Category:

Technology


0 download

DESCRIPTION

Coding in a functional language requires more than simply knowing the syntax of that language. You can write a for loop in F# as easily as you can in C#, but you're missing out on the functional aspects of F# (and C# for that matter). We'll do an introduction to the language features of F# and how to unit test but more importantly we'll write some code with a functional mindset and discuss where and why you would want to use F# in your existing projects.

TRANSCRIPT

Page 1: Thinking in F#

Thinking in F#Mike Clement@mdclement

[email protected]://blog.softwareontheside.com

Page 2: Thinking in F#

XP Simple Design

•Passes all tests•Clear, expressive, consistent•No duplication•Minimal

Page 3: Thinking in F#

FizzBuzz

• If multiple of 3, get “Fizz”• If multiple of 5, get “Buzz”• If not, return input int as string• Rules are in order

Page 4: Thinking in F#

FizzBuzz Code

Page 5: Thinking in F#

Currying/Partial function application

let Multiples divisor result x s = if x % divisor = 0 then s + result else slet Fizzy = Multiples 3 "Fizz"

val Multiples : divisor:int -> result:string -> x:int -> s:string -> stringval Fizzy : (int -> string -> string)

let Fizzy x s = if x % 3 = 0 then s + "Fizz" else s

Page 6: Thinking in F#

Bowling Game Kata

The game consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares.

A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll. So in frame 3 above, the score is 10 (the total number knocked down) plus a bonus of 5 (the number of pins knocked down on the next roll.)

A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled.

In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame. However no more than three balls can be rolled in tenth frame.

Page 7: Thinking in F#

Bowling Game – The OO Way

Page 8: Thinking in F#

Bowling GameHow do we get more functional?

Page 9: Thinking in F#

Lists

[1, 2, 3, 4]

Head

Tail

Page 10: Thinking in F#

Lists

[1, 2, 3, 4].Head = 1[1, 2, 3, 4].Tail = [2, 3, 4][1, 2, 3, 4].Tail.Tail = [3, 4]

Page 12: Thinking in F#

Mike Clement

• @mdclement• [email protected]• http://blog.softwareontheside.com• https://github.com/mdclement• http://agilecodegames.com• Utah Software Craftsmanship

• http://utahsc.org• @utahsc• We meet the first Wednesday at SLCC