From bc9b618a8f6db5806b1d9c27cbae59eaa1ec70f2 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 23 Nov 2014 16:54:25 +0100 Subject: [PATCH] Update samples. --- FSharpRef/Functional.fs | 18 ++++++++++++------ FSharpRef/Main.fs | 6 +++++- FSharpRef/Object.fs | 1 + 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/FSharpRef/Functional.fs b/FSharpRef/Functional.fs index 424dc35..a793722 100644 --- a/FSharpRef/Functional.fs +++ b/FSharpRef/Functional.fs @@ -3,9 +3,6 @@ open System.IO open System.Linq -// For dynmic typing. To print anything in our case. -let inline print a = printfn "%A" a - // Write something on a file. let appendFile (filename: string) (text: string) = use sw = new StreamWriter(filename, true) @@ -59,10 +56,19 @@ let deckOfCards = yield NumCard(n, suit) ] -// Records. -type Person = { First: string; Last: string; Age: int } +// Records with custom equality. +[] +type Person = + { First: string; Last: string; Age: int } + override this.Equals(other) = + match other with + | :? Person as p -> p.First = this.First && p.Last = this.Last + | _ -> false + override this.GetHashCode() = + this.First.GetHashCode() ^^^ this.Last.GetHashCode() + let steve = { First = "Steve"; Last = "Holt"; Age = 17 } -let steves'twin = { steve with First = "Paul" } +let stevesTwin = { steve with First = "Paul" } // Queries. Should be used with SQL or other external data sources. let youngPersonNames persons = diff --git a/FSharpRef/Main.fs b/FSharpRef/Main.fs index d070829..33fc574 100644 --- a/FSharpRef/Main.fs +++ b/FSharpRef/Main.fs @@ -3,6 +3,9 @@ open System.Linq open Functional +// For dynamic typing. To print anything in our case. +let inline print a = printfn "%A" a + [] let main args = appendFile "test.txt" "Pouet" @@ -11,8 +14,9 @@ let main args = print (isEven (fac 4)) // print steve.First + printfn "Person equality: %A" ({ First = "Steve"; Last = "Holt"; Age = 42 } = steve) printfn "Young persons: " - (youngPersonNames (Queryable.AsQueryable [steve ; steves'twin ; { First = "Paul"; Last = "Atreides"; Age = 11 }])) |> Seq.iter (fun name -> printfn " name: %s" name) + (youngPersonNames (Queryable.AsQueryable [steve ; stevesTwin ; { First = "Paul"; Last = "Atreides"; Age = 11 }])) |> Seq.iter (fun name -> printfn " name: %s" name) // print <| Seq.take 10 allPositiveInts print <| Seq.take 10 (fibs 1 1) diff --git a/FSharpRef/Object.fs b/FSharpRef/Object.fs index c89cd72..3ba7bdc 100644 --- a/FSharpRef/Object.fs +++ b/FSharpRef/Object.fs @@ -1,2 +1,3 @@ module Object + -- 2.43.0