Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update snippets
  • Loading branch information
albert-du committed Dec 11, 2021
commit 8bed98a930129f2f4fbedfd63cc3efb784041e85
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let pointFToPoint (pf: PointF) =
Point(int pf.X, int pf.Y)

// Create an array of PointF objects.
let apf =
let apf =
[| PointF(27.8f, 32.62f)
PointF(99.3f, 147.273f)
PointF(7.5f, 1412.2f) |]
Expand All @@ -26,14 +26,13 @@ printfn ""
for p in ap do
printfn $"{p}"

(* This code example produces the following output:
// This code example produces the following output:
// {X=27.8, Y=32.62}
// {X=99.3, Y=147.273}
// {X=7.5, Y=1412.2}
//
// {X=27,Y=32}
// {X=99,Y=147}
// {X=7,Y=1412}

{X=27.8, Y=32.62}
{X=99.3, Y=147.273}
{X=7.5, Y=1412.2}

{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
*)
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,26 @@ let goMesozoic = { Dinosaurs = dinosaurs }
goMesozoic.DiscoverAll()
goMesozoic.DiscoverByEnding "saurus"

(* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

Array.Exists(dinosaurs, "saurus"): true

Array.TrueForAll(dinosaurs, "saurus"): false

Array.Find(dinosaurs, "saurus"): Amargasaurus

Array.FindLast(dinosaurs, "saurus"): Dilophosaurus

Array.FindAll(dinosaurs, "saurus"):
Amargasaurus
Dilophosaurus
*)
// This code example produces the following output:
// Compsognathus
// Amargasaurus
// Oviraptor
// Velociraptor
// Deinonychus
// Dilophosaurus
// Gallimimus
// Triceratops
//
// Array.Exists(dinosaurs, "saurus"): true
//
// Array.TrueForAll(dinosaurs, "saurus"): false
//
// Array.Find(dinosaurs, "saurus"): Amargasaurus
//
// Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
//
// Array.FindAll(dinosaurs, "saurus"):
// Amargasaurus
// Dilophosaurus
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ open System
// Search predicate returns true if a string ends in "saurus".
let endsWithSaurus (s: string) =
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"
let dinosaurs =

let dinosaurs =
[| "Compsognathus"; "Amargasaurus"
"Oviraptor"; "Velociraptor"
"Deinonychus"; "Dilophosaurus"
Expand All @@ -24,21 +24,22 @@ Array.FindIndex(dinosaurs, 2, endsWithSaurus)
Array.FindIndex(dinosaurs, 2, 3, endsWithSaurus)
|> printfn "\nArray.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): %i"

(* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

Array.FindIndex(dinosaurs, EndsWithSaurus): 1

Array.FindIndex(dinosaurs, 2, EndsWithSaurus): 5
// This code example produces the following output:
//
// Compsognathus
// Amargasaurus
// Oviraptor
// Velociraptor
// Deinonychus
// Dilophosaurus
// Gallimimus
// Triceratops
//
// Array.FindIndex(dinosaurs, EndsWithSaurus): 1
//
// Array.FindIndex(dinosaurs, 2, EndsWithSaurus): 5
//
// Array.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): -1

Array.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): -1
*)
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open System
let endsWithSaurus (s: string) =
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"

let dinosaurs =
let dinosaurs =
[| "Compsognathus"; "Amargasaurus"
"Oviraptor"; "Velociraptor"
"Deinonychus"; "Dilophosaurus"
Expand All @@ -20,25 +20,26 @@ Array.FindLastIndex(dinosaurs, endsWithSaurus)

Array.FindLastIndex(dinosaurs, 4, endsWithSaurus)
|> printfn "\nArray.FindLastIndex(dinosaurs, 4, EndsWithSaurus): %i"

Array.FindLastIndex(dinosaurs, 4, 3, endsWithSaurus)
|> printfn "\nArray.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): %i"

(* This code example produces the following output:

Compsognathus
Amargasaurus
Oviraptor
Velociraptor
Deinonychus
Dilophosaurus
Gallimimus
Triceratops

Array.FindLastIndex(dinosaurs, EndsWithSaurus): 5

Array.FindLastIndex(dinosaurs, 4, EndsWithSaurus): 1
// This code example produces the following output:
//
// Compsognathus
// Amargasaurus
// Oviraptor
// Velociraptor
// Deinonychus
// Dilophosaurus
// Gallimimus
// Triceratops
//
// Array.FindLastIndex(dinosaurs, EndsWithSaurus): 5
//
// Array.FindLastIndex(dinosaurs, 4, EndsWithSaurus): 1
//
// Array.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): -1

Array.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): -1
*)
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
open System

let compareDinosByLength (x: string) (y: string) =
if isNull x then
// If x is null and y is null, they're equal.
if isNull y then 0
// If x is null and y is not null, y is greater.
else -1
else
// If x is not null and y is null, x is greater.
if isNull y then 1
// If x is not null and y is not null, compare the lengths of the two strings.
match x with
// If x is null and y is null, they're equal.
| null when isNull y -> 0
// If x is null and y is not null, y is greater.
| null -> -1
// If x is not null and y is null, x is greater.
| _ when isNull y -> 1
// If x is not null and y is not null, compare the lengths of the two strings.
| _ ->
let retval = x.Length.CompareTo y.Length
if retval <> 0 then
// If the strings are not of equal length, the longer string is greater.
retval
else
let retval = x.Length.CompareTo y.Length
if retval <> 0 then
// If the strings are not of equal length, the longer string is greater.
retval
else
// If the strings are of equal length, sort them with ordinary string comparison.
x.CompareTo y
// If the strings are of equal length, sort them with ordinary string comparison.
x.CompareTo y

let display arr =
printfn ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let showWhere (array: 'a []) index =
else
printfn $"Found at index {index}."

let dinosaurs =
let dinosaurs =
[| "Pachycephalosaurus"
"Amargasaurus"
"Tyrannosaurus"
Expand All @@ -48,28 +48,29 @@ printfn "\nBinarySearch for 'Tyrannosaurus':"
Array.BinarySearch(dinosaurs, "Tyrannosaurus")
|> showWhere dinosaurs

(* This code example produces the following output:

Pachycephalosaurus
Amargasaurus
Tyrannosaurus
Mamenchisaurus
Deinonychus
Edmontosaurus
// This code example produces the following output:
//
// Pachycephalosaurus
// Amargasaurus
// Tyrannosaurus
// Mamenchisaurus
// Deinonychus
// Edmontosaurus
//
// Sort
//
// Amargasaurus
// Deinonychus
// Edmontosaurus
// Mamenchisaurus
// Pachycephalosaurus
// Tyrannosaurus
//
// BinarySearch for 'Coelophysis':
// Not found. Sorts between: Amargasaurus and Deinonychus.
//
// BinarySearch for 'Tyrannosaurus':
// Found at index 5.

Sort

Amargasaurus
Deinonychus
Edmontosaurus
Mamenchisaurus
Pachycephalosaurus
Tyrannosaurus

BinarySearch for 'Coelophysis':
Not found. Sorts between: Amargasaurus and Deinonychus.

BinarySearch for 'Tyrannosaurus':
Found at index 5.
*)
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let showWhere (array: 'a []) index =
else
printfn $"Found at index {index}."

let dinosaurs =
let dinosaurs =
[| "Pachycephalosaurus"
"Amargasaurus"
"Tyrannosaurus"
Expand Down Expand Up @@ -57,28 +57,28 @@ printfn "\nBinarySearch for 'Tyrannosaurus':"
Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc)
|> showWhere dinosaurs

(* This code example produces the following output:

Pachycephalosaurus
Amargasaurus
Tyrannosaurus
Mamenchisaurus
Deinonychus
Edmontosaurus
// This code example produces the following output:
// Pachycephalosaurus
// Amargasaurus
// Tyrannosaurus
// Mamenchisaurus
// Deinonychus
// Edmontosaurus
//
// Sort
//
// Tyrannosaurus
// Pachycephalosaurus
// Mamenchisaurus
// Edmontosaurus
// Deinonychus
// Amargasaurus
//
// BinarySearch for 'Coelophysis':
// Not found. Sorts between: Deinonychus and Amargasaurus.
//
// BinarySearch for 'Tyrannosaurus':
// Found at index 0.

Sort

Tyrannosaurus
Pachycephalosaurus
Mamenchisaurus
Edmontosaurus
Deinonychus
Amargasaurus

BinarySearch for 'Coelophysis':
Not found. Sorts between: Deinonychus and Amargasaurus.

BinarySearch for 'Tyrannosaurus':
Found at index 0.
*)
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@ printValues myIntArray
printf "Object array: "
printValues myObjArray

(*
This code produces the following output.

Initially,
integer array: 1 2 3 4 5
Object array: 26 27 28 29 30
// This code produces the following output.
// Initially,
// integer array: 1 2 3 4 5
// Object array: 26 27 28 29 30
//
// After copying the first two elements of the integer array to the Object array,
// integer array: 1 2 3 4 5
// Object array: 1 2 28 29 30
//
// After copying the last two elements of the Object array to the integer array,
// integer array: 1 2 3 29 30
// Object array: 1 2 28 29 30

After copying the first two elements of the integer array to the Object array,
integer array: 1 2 3 4 5
Object array: 1 2 28 29 30

After copying the last two elements of the Object array to the integer array,
integer array: 1 2 3 29 30
Object array: 1 2 28 29 30
*)
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ let myObjectEven: obj = 6
findMyObject myIntArray myObjectEven

// This code produces the following output:
//
//The int array contains the following:
// 2 3 6 7 8
//The object to search for (1) is not found. The next larger object is at index 0.
//The object to search for (6) is at index 2.
// The int array contains the following:
// 2 3 6 7 8
// The object to search for (1) is not found. The next larger object is at index 0.
// The object to search for (6) is at index 2.

// </Snippet1>
Loading