Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions FSharpKoans/AboutBinding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ module ``02: About Binding`` =
[<Test>]
let ``01 Basic 'let' binding`` () =
let x = 50 in // note that the syntax is more explicit about what's really going on!
x |> should equal __
x |> should equal 50

[<Test>]
let ``02 Equivalent basic 'let' binding`` () = // this is exactly equivalent to the previous binding.
let x = 50
x |> should equal __
x |> should equal 50

[<Test>]
let ``03 There are many types of values`` () =
let a = __
let b = __
let c = __
let d = __
let e = __
let f = __
let a = 7
let b = 4.5
let c = true
let d = "Hi"
let e = 'e'
let f = ()
a |> should be ofType<int>
b |> should be ofType<float>
c |> should be ofType<bool>
Expand All @@ -84,16 +84,16 @@ module ``02: About Binding`` =

[<Test>]
let ``04 We can compare values using F#'s comparison operators`` () =
1 |???| 2 |> should equal true // change the |???| to be an actual operator
2 |???| 1 |> should equal true
1 |???| 1 |> should equal true
1 |???| 1 |> should equal false
() |???| () |> should equal true
() |???| () |> should equal false
1 < 2 |> should equal true // change the |???| to be an actual operator
2 > 1 |> should equal true
1 = 1 |> should equal true
1 < 1 |> should equal false
() = () |> should equal true
() > () |> should equal false

[<Test>]
let ``05 There's a `not` function instead of a `not` operator`` () =
__ true |> should equal false
not true |> should equal false

[<Test>]
let ``06 Nest your 'let' statements as deeply as you'd like`` () =
Expand All @@ -103,7 +103,7 @@ module ``02: About Binding`` =
let d = 63 in d
c + 1
b + 7
a |> should equal ___
a |> should equal 71

(*
Identifiers are *referentially transparent*: the link between value and identifier never changes.
Expand Down Expand Up @@ -139,9 +139,9 @@ module ``02: About Binding`` =
3 + a
let c = a + 4
let a = a + a
a |> should equal __
b |> should equal __
c |> should equal __
a |> should equal 42
b |> should equal 11
c |> should equal 25


(*
Expand All @@ -155,36 +155,36 @@ module ``02: About Binding`` =

[<Test>]
let ``08 An identifier pattern will match anything`` () =
let x = __ // replace with an integer
let y = __ // replace with a string
let z = __ // replace with anything else!
let x = 11 // replace with an integer
let y = "hello" // replace with a string
let z = 'h' // replace with anything else!
x |> should be ofType<int>
y |> should be ofType<string>

[<Test>]
let ``09 A wildcard pattern will match anything`` () =
let _ = __ // replace with an integer
let _ = __ // replace with a string
let _ = __ // replace with anything else!
let _ = 45 // replace with an integer
let _ = "hhhuh" // replace with a string
let _ = true // replace with anything else!
()

[<Test>]
let ``10 Constant patterns succeed if both sides match`` () =
let 900 = __
let "Can't win all the time" = __
let 900 = 900
let "Can't win all the time" = "Can't win all the time"
()

[<Test>]
let ``11 Constant patterns fail if the sides don't match exactly`` () =
// fill in something below, on the right-hand side, to make this pattern FAIL
(fun () ->
let "FILL ME IN" = FILL__ME_IN
()
(fun () ->
let "FILL ME IN" = failwith "cannot match"
()
) |> should throw typeof<MatchFailureException>

[<Test>]
let ``12 Or patterns succeed if any pattern matches`` () =
let a | a = __
let 7 | 13 | 2 = 3 + __
let 'x' | _ | 'p' = __
let a | a = 'a'
let 7 | 13 | 2 = 3 + 4
let 'x' | _ | 'p' = 'p'
()
54 changes: 27 additions & 27 deletions FSharpKoans/AboutFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,44 @@ open NUnit.Framework
module ``03: Putting the Function into Functional Programming`` =
[<Test>]
let ``01 A function takes one input and produces one output`` () =
(fun a -> a + 100) __ |> should equal 2097
(fun a -> a + 100) 1997 |> should equal 2097

[<Test>]
let ``02 The input to a function is a pattern (Part 1).`` () =
(fun 7 -> 9) __ |> should equal 9
(fun 7 -> 9) 7 |> should equal 9

[<Test>]
let ``03 The input to a function is a pattern (Part 2).`` () =
(fun _ -> 75) __ |> should equal 75
(fun _ -> 75) 4 |> should equal 75

[<Test>]
let ``04 The input to a function is a pattern (Part 3).`` () =
(fun (2 | 3 | 5) -> "Prime") __ |> should equal "Prime"
(fun (2 | 3 | 5) -> "Prime") 3 |> should equal "Prime"

[<Test>]
let ``05 A function can be bound to a name (Part 1).`` () =
let one_third = fun ka -> ka / 3
__ 21 |> should equal 7
one_third 21 |> should equal 7

[<Test>]
let ``06 A function can be bound to a name (Part 2).`` () =
let pinky bleh = bleh / 3 // The syntax has changed from Part 1, but the meaning is the same
__ 21 |> should equal 7
pinky 21 |> should equal 7

[<Test>]
let ``07 A function can span multiple lines (Part 1).`` () =
(fun zorro ->
let k = "swash" // notice the indentation.
let b = "buckle" // F# is whitespace-sensitive, so it is important!
zorro + " likes to " + k + b
) "Zorro the pirate" |> should equal __
) "Zorro the pirate" |> should equal "Zorro the pirate likes to swashbuckle"

[<Test>]
let ``08 A function can span multiple lines (Part 2).`` () =
let jorus who =
let p = 5
who * p
jorus 12 |> should equal __
jorus 12 |> should equal 60

[<Test>]
let ``09 A function can span multiple lines (Part 2, expanded syntax).`` () =
Expand All @@ -66,7 +66,7 @@ module ``03: Putting the Function into Functional Programming`` =
let p = 3 in
who * p
in
jorus 12 |> should equal __
jorus 12 |> should equal 60

// The next few are very similar. Resist the temptation to
// just fill out values without having any idea about what's
Expand All @@ -79,36 +79,36 @@ module ``03: Putting the Function into Functional Programming`` =
// read the above as: fun love -> (fun hate -> (love - hate))
let j = i 10
let k = j 9
k |> should equal __
k |> should equal 1

[<Test>]
let ``11 A function can return a function (Part 2).`` () =
let funky a b = a + b
let j = funky 10
let k = j 9
k |> should equal __
k |> should equal 19

[<Test>]
let ``12 You can write a function as a one-liner (Part 1).`` () =
(fun ___ -> fun ___ -> __ * __) __ __ |> should equal 27
(fun a -> fun b -> a * b) 3 9 |> should equal 27

[<Test>]
let ``13 You can write a function as a one-liner (Part 2).`` () =
(fun _____ ____ -> __ + __) __ __ |> should equal 17
(fun a b -> a + b) 10 7 |> should equal 17

[<Test>]
let ``14 'Multiple-argument' functions are one-input, one-output in disguise`` () =
let i j k = j * k
let j = __ 4
let k = __ 12
let j = i 4
let k = j 12
k |> should equal 48

[<Test>]
let ``15 A function is executed when it is called, NOT when it is defined or referenced (Part 1).`` () =
let f a =
failwith "An exception will be thrown as soon as this is executed."
a + 2
___ |> should be ofType<int -> int>
f |> should be ofType<int -> int>

[<Test>]
let ``16 A function is executed when it is called, NOT when it is defined or referenced (Part 2).`` () =
Expand All @@ -123,31 +123,31 @@ module ``03: Putting the Function into Functional Programming`` =
let ``17 Two names can be bound to the same value`` () =
let f x = x + 2
let y = f
y 20 |> should equal ___
y 20 |> should equal 22


[<Test>]
let ``18 Shadowing and functions`` () =
let a = 25
let f () = a + 10
let a = 99
a |> should equal __
f () |> should equal __
a |> should equal 99
f () |> should equal 35

[<Test>]
let ``19 Nesting functions`` () =
let hailstone x =
let triple x = x * 3
let addOne x = x + 1
addOne (triple x)
hailstone 5 |> should equal __
hailstone 5 |> should equal 16

[<Test>]
let ``20 Functions have types`` () =
let a x y = x + "cabbage" + y
let b r = 50.0 / r
a |> should be ofType<FILL_ME_IN>
b |> should be ofType<FILL_ME_IN>
a |> should be ofType<string>
b |> should be ofType<float>


[<Test>]
Expand Down Expand Up @@ -178,11 +178,11 @@ module ``03: Putting the Function into Functional Programming`` =

[<Test>]
let ``22 Operators are functions in disguise`` () =
(+) 5 8 |> should equal __
(-) 3 5 |> should equal __
(/) 12 4 |> should equal __
(=) 93.1 93.12 |> should equal __
(<) "hey" "jude" |> should equal __
(+) 5 8 |> should equal 13
(-) 3 5 |> should equal -2
(/) 12 4 |> should equal 3
(=) 93.1 93.12 |> should equal false
(<) "hey" "jude" |> should equal true
// ... and other operators: >, <=, >=, <>, %, ...

(*
Expand Down
4 changes: 2 additions & 2 deletions FSharpKoans/AboutTesting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ module ``01: About Testing`` =
[<Test>]
let ``01 How this works`` () = // In F#, any sequence of characters between `` marks can be identifiers. ``This is a long method name`` is way better than ThisIsALongMethodName !
let expected_value = 1 + 1
let actual_value = __ //start by changing this line
let actual_value = 2 //start by changing this line
actual_value |> should equal expected_value

// Easy, right? Now try one more.
[<Test>]
let ``02 Fill in the values`` () = (1 + 1) |> should equal __
let ``02 Fill in the values`` () = (1 + 1) |> should equal 2
34 changes: 17 additions & 17 deletions FSharpKoans/FSharpKoans.fsproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Helpers.fs" />
<Compile Include="AboutTesting.fs" />
Expand All @@ -22,13 +22,13 @@
<Compile Include="AboutListOperations.fs" />
<Compile Include="AboutRecords.fs" />
<Compile Include="AboutYou.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsUnit" Version="3.4.0" />
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>

</ItemGroup>
<ItemGroup>
<PackageReference Include="FsUnit" Version="3.4.0" />
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
</Project>