Skip to content

Commit

Permalink
Merge branch 'fsharp' into typeproviderkoan
Browse files Browse the repository at this point in the history
  • Loading branch information
normanhh3 committed Mar 19, 2019
2 parents fdd2e71 + 04acc34 commit 5073668
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 146 deletions.
14 changes: 7 additions & 7 deletions FSharpKoans/AboutArrays.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module ``about arrays`` =
let CreatingArrays() =
let fruits = [| "apple"; "pear"; "peach"|]

AssertEquality fruits.[0] __
AssertEquality fruits.[1] __
AssertEquality fruits.[2] __
AssertEquality fruits.[0] "apple"
AssertEquality fruits.[1] "pear"
AssertEquality fruits.[2] "peach"

[<Koan>]
let ArraysAreDotNetArrays() =
Expand All @@ -33,15 +33,15 @@ module ``about arrays`` =
let fruits = [| "apple"; "pear" |]
fruits.[1] <- "peach"

AssertEquality fruits __
AssertEquality fruits [| "apple"; "peach" |]

[<Koan>]
let YouCanCreateArraysWithComprehensions() =
let numbers =
[| for i in 0..10 do
if i % 2 = 0 then yield i |]

AssertEquality numbers __
AssertEquality numbers [| 0; 2; 4; 6; 8; 10 |]

[<Koan>]
let ThereAreAlsoSomeOperationsYouCanPerformOnArrays() =
Expand All @@ -51,5 +51,5 @@ module ``about arrays`` =
let original = [| 0..5 |]
let result = Array.map cube original

AssertEquality original __
AssertEquality result __
AssertEquality original [| 0..5 |]
AssertEquality result [| 0; 1; 8; 27; 64; 125 |]
4 changes: 2 additions & 2 deletions FSharpKoans/AboutAsserts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ module ``about asserts`` =
[<Koan>]
let AssertExpectation() =
let expected_value = 1 + 1
let actual_value = __ //start by changing this line
let actual_value = 2 //start by changing this line

AssertEquality expected_value actual_value

//Easy, right? Now try one more

[<Koan>]
let FillInValues() =
AssertEquality (1 + 1) __
AssertEquality (1 + 1) 2
16 changes: 8 additions & 8 deletions FSharpKoans/AboutBranching.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module ``about branching`` =
"it's odd!"

let result = isEven 2
AssertEquality result __
AssertEquality result "it's even!"

[<Koan>]
let IfStatementsReturnValues() =
Expand All @@ -34,7 +34,7 @@ module ``about branching`` =
else
"no problem here"

AssertEquality result __
AssertEquality result "no problem here"

[<Koan>]
let BranchingWithAPatternMatch() =
Expand All @@ -46,8 +46,8 @@ module ``about branching`` =
let result1 = isApple "apple"
let result2 = isApple ""

AssertEquality result1 __
AssertEquality result2 __
AssertEquality result1 true
AssertEquality result2 false

[<Koan>]
let UsingTuplesWithIfStatementsQuicklyBecomesClumsy() =
Expand All @@ -64,8 +64,8 @@ module ``about branching`` =
let person1 = ("Chris", "steak")
let person2 = ("Dave", "veggies")

AssertEquality (getDinner person1) __
AssertEquality (getDinner person2) __
AssertEquality (getDinner person1) "Chris wants 'em some steak"
AssertEquality (getDinner person2) "Dave doesn't want red meat"

[<Koan>]
let PatternMatchingIsNicer() =
Expand All @@ -80,5 +80,5 @@ module ``about branching`` =
let person1 = ("Bob", "fish")
let person2 = ("Sally", "Burger")

AssertEquality (getDinner person1) __
AssertEquality (getDinner person2) __
AssertEquality (getDinner person1) "Bob doesn't want red meat"
AssertEquality (getDinner person2) "Sally wants 'em some Burger"
12 changes: 6 additions & 6 deletions FSharpKoans/AboutClasses.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,29 @@ module ``about classes`` =
let ClassesCanHaveProperties() =
let zombie = new Zombie()

AssertEquality zombie.FavoriteFood __
AssertEquality zombie.FavoriteFood "brains"

[<Koan>]
let ClassesCanHaveMethods() =
let zombie = new Zombie()

let result = zombie.Eat "brains"
AssertEquality result __
AssertEquality result "mmmmmmmmmmmmmmm"

[<Koan>]
let ClassesCanHaveConstructors() =

let person = new Person("Shaun")

let result = person.Speak()
AssertEquality result __
AssertEquality result "Hi my name is Shaun"

[<Koan>]
let ClassesCanHaveLetBindingsInsideThem() =
let zombie = new Zombie2()

let result = zombie.Eat "chicken"
AssertEquality result __
AssertEquality result "grrrrrrrr"

(* TRY IT: Can you access the let bound value Zombie2.favoriteFood
outside of the class definition? *)
Expand All @@ -75,8 +75,8 @@ module ``about classes`` =
let person = new Person2("Shaun")

let firstPhrase = person.Speak()
AssertEquality firstPhrase __
AssertEquality firstPhrase "Hi my name is Shaun"

person.Name <- "Shaun of the Dead"
let secondPhrase = person.Speak()
AssertEquality secondPhrase __
AssertEquality secondPhrase "Hi my name is Shaun of the Dead"
6 changes: 3 additions & 3 deletions FSharpKoans/AboutDiscriminatedUnions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module ``about discriminated unions`` =

let choice = Mustard

AssertEquality (toColor choice) __
AssertEquality (toColor choice) "yellow"

(* TRY IT: What happens if you remove a case from the above pattern
match? *)
Expand All @@ -51,5 +51,5 @@ module ``about discriminated unions`` =
let bourbonResult = saySomethingAboutYourFavorite <| Bourbon "Maker's Mark"
let numberResult = saySomethingAboutYourFavorite <| Number 7

AssertEquality bourbonResult __
AssertEquality numberResult __
AssertEquality bourbonResult "I prefer Bookers to Maker's Mark"
AssertEquality numberResult "me too!"
20 changes: 10 additions & 10 deletions FSharpKoans/AboutDotNetCollections.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module ``about dot net collections`` =
fruits.Add("apple")
fruits.Add("pear")

AssertEquality fruits.[0] __
AssertEquality fruits.[1] __
AssertEquality fruits.[0] "apple"
AssertEquality fruits.[1] "pear"

[<Koan>]
let CreatingDotNetDictionaries() =
Expand All @@ -29,8 +29,8 @@ module ``about dot net collections`` =
addressBook.["Chris"] <- "Ann Arbor"
addressBook.["SkillsMatter"] <- "London"

AssertEquality addressBook.["Chris"] __
AssertEquality addressBook.["SkillsMatter"] __
AssertEquality addressBook.["Chris"] "Ann Arbor"
AssertEquality addressBook.["SkillsMatter"] "London"

[<Koan>]
let YouUseCombinatorsWithDotNetTypes() =
Expand All @@ -49,15 +49,15 @@ module ``about dot net collections`` =
// that you can combine to perform operations on types implementing
// seq/IEnumerable.

AssertEquality verboseBook.[0] __
AssertEquality verboseBook.[1] __
AssertEquality verboseBook.[0] "Name: Chris - City: Ann Arbor"
AssertEquality verboseBook.[1] "Name: SkillsMatter - City: London"

[<Koan>]
let SkippingElements() =
let original = [0..5]
let result = Seq.skip 2 original
let result = Seq.skip 2 original |> Seq.toList

AssertEquality result __
AssertEquality result [2; 3; 4; 5]

[<Koan>]
let FindingTheMax() =
Expand All @@ -71,7 +71,7 @@ module ``about dot net collections`` =

let result = Seq.max values

AssertEquality result __
AssertEquality result 20

[<Koan>]
let FindingTheMaxUsingACondition() =
Expand All @@ -81,4 +81,4 @@ module ``about dot net collections`` =
let names = [| "Harry"; "Lloyd"; "Nicholas"; "Mary"; "Joe"; |]
let result = Seq.maxBy getNameLength names

AssertEquality result __
AssertEquality result "Nicholas"
20 changes: 10 additions & 10 deletions FSharpKoans/AboutFiltering.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module ``about filtering`` =
names
|> List.filter (fun name -> name.StartsWith( "A" ))

AssertEquality actual_names [ __ ]
AssertEquality actual_names [ "Alice" ]

//Or passing a function to filter
let startsWithTheLetterB (s: string) =
Expand All @@ -41,18 +41,18 @@ module ``about filtering`` =
names
|> List.filter startsWithTheLetterB

AssertEquality namesBeginningWithB [ __ ]
AssertEquality namesBeginningWithB [ "Bob" ]

[<Koan>]
let FindingJustOneItem() =
let names = [ "Alice"; "Bob"; "Eve"; ]
let names = [ "Alice"; "Bob"; "Eve" ]
let expected_name = "Bob"

// find will return just one item, or throws an exception

let actual_name =
names
|> List.find (fun name -> name = __ )
|> List.find (fun name -> name = "Bob" )

//??? What would happen if there are 2 Bobs in the List?

Expand All @@ -70,8 +70,8 @@ module ``about filtering`` =
names
|> List.tryFind (fun name -> name = "Zelda" )

AssertEquality eve.IsSome __
AssertEquality zelda.IsSome __
AssertEquality eve.IsSome true
AssertEquality zelda.IsSome false

[<Koan>]
let ChoosingItemsFromAList() =
Expand All @@ -83,7 +83,7 @@ module ``about filtering`` =
numbers
|> List.choose someIfEven

AssertEquality evenNumbers [ __ ]
AssertEquality evenNumbers [ 2 ]

//You can also use the "id" function on types of 'a option list
//"id" will return just those that are "Some"
Expand All @@ -94,7 +94,7 @@ module ``about filtering`` =
|> List.choose id

//Notice the type of namesWithValue is 'string list', whereas optionNames is 'string option list'
AssertEquality namesWithValue [ __ ]
AssertEquality namesWithValue [ "Alice" ]

[<Koan>]
let PickingItemsFromAList() =
Expand All @@ -106,7 +106,7 @@ module ``about filtering`` =
numbers
|> List.pick someIfEven

AssertEquality firstEven __
AssertEquality firstEven 6

//As with choose, you can also use the "id" function on types of 'a option list
//to return just those that are "Some"
Expand All @@ -116,6 +116,6 @@ module ``about filtering`` =
optionNames
|> List.pick id

AssertEquality firstNameWithValue __
AssertEquality firstNameWithValue "Alice"

//There is also a tryPick which works like tryFind, returning "None" instead of throwing an exception.
10 changes: 5 additions & 5 deletions FSharpKoans/AboutFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module ``about functions`` =
let result1 = add 2 2
let result2 = add 5 2

AssertEquality result1 __
AssertEquality result2 __
AssertEquality result1 4
AssertEquality result2 7

[<Koan>]
let NestingFunctions() =
Expand All @@ -36,7 +36,7 @@ module ``about functions`` =
double(double(x))

let result = quadruple 4
AssertEquality result __
AssertEquality result 16

[<Koan>]
let AddingTypeAnnotations() =
Expand All @@ -48,7 +48,7 @@ module ``about functions`` =
text.Replace(" ", "")

let auctioneered = sayItLikeAnAuctioneer "going once going twice sold to the lady in red"
AssertEquality auctioneered __
AssertEquality auctioneered "goingoncegoingtwicesoldtotheladyinred"

//TRY IT: What happens if you remove the type annotation on text?

Expand All @@ -63,7 +63,7 @@ module ``about functions`` =

let caffeinatedReply = caffeinate "hello there"

AssertEquality caffeinatedReply __
AssertEquality caffeinatedReply "HELLO THERE!!!"

(* NOTE: Accessing the suffix variable in the nested caffeinate function
is known as a closure.
Expand Down
12 changes: 6 additions & 6 deletions FSharpKoans/AboutLet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module ``about let`` =
let LetBindsANameToAValue() =
let x = 50

AssertEquality x __
AssertEquality x 50

(* In F#, values created with let are inferred to have a type like
"int" for integer values, "string" for text values, and "bool"
Expand All @@ -28,7 +28,7 @@ module ``about let`` =

let y = "a string"
let expectedType = y.GetType()
AssertEquality expectedType typeof<FILL_ME_IN>
AssertEquality expectedType typeof<string>

[<Koan>]
let YouCanMakeTypesExplicit() =
Expand All @@ -38,8 +38,8 @@ module ``about let`` =
let y:string = "forty two"
let typeOfY = y.GetType()

AssertEquality typeOfX typeof<FILL_ME_IN>
AssertEquality typeOfY typeof<FILL_ME_IN>
AssertEquality typeOfX typeof<int>
AssertEquality typeOfY typeof<string>

(* You don't usually need to provide explicit type annotations types for
local variables, but type annotations can come in handy in other
Expand Down Expand Up @@ -68,7 +68,7 @@ module ``about let`` =
let mutable x = 100
x <- 200

AssertEquality x __
AssertEquality x 200

[<Koan>]
let YouCannotModifyALetBoundValueIfItIsNotMutable() =
Expand All @@ -82,4 +82,4 @@ module ``about let`` =
// to reuse the name of a value in some cases using "shadowing".
let x = 100

AssertEquality x __
AssertEquality x 100
Loading

0 comments on commit 5073668

Please sign in to comment.