Skip to content

Commit 954ae04

Browse files
authored
Add F# snippets (dotnet#7453)
1 parent 3533ff8 commit 954ae04

File tree

24 files changed

+760
-2
lines changed

24 files changed

+760
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="int32_equals.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// System.Int32.Equals(Object)
2+
3+
(*
4+
The following program demonstrates the 'Equals(Object)' method
5+
of struct 'Int32'. This compares an instance of 'Int32' with the
6+
passed in object and returns true if they are equal.
7+
*)
8+
9+
open System
10+
11+
// <Snippet1>
12+
let myVariable1 = 60
13+
let myVariable2 = 60
14+
15+
// Get and display the declaring type.
16+
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
17+
printfn $"Type of 'myVariable2' is '{myVariable2.GetType()}' and value is: {myVariable2}"
18+
19+
// Compare 'myVariable1' instance with 'myVariable2' Object.
20+
if myVariable1.Equals myVariable2 then
21+
printfn "\nStructures 'myVariable1' and 'myVariable2' are equal"
22+
else
23+
printfn "\nStructures 'myVariable1' and 'myVariable2' are not equal"
24+
25+
// </Snippet1>
26+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// <Snippet1>
2+
open System
3+
4+
type Comparison =
5+
| LessThan = -1
6+
| Equal = 0
7+
| GreaterThan = 1
8+
9+
let mainValue = 16325
10+
let zeroValue = 0
11+
let negativeValue = -1934
12+
let positiveValue = 903624
13+
let sameValue = 16325
14+
15+
printfn $"Comparing {mainValue} and {zeroValue}: {mainValue.CompareTo zeroValue} ({enum<Comparison>(mainValue.CompareTo zeroValue)})."
16+
17+
printfn $"Comparing {mainValue} and {sameValue}: {mainValue.CompareTo sameValue} ({enum<Comparison>(mainValue.CompareTo sameValue)})."
18+
19+
printfn $"Comparing {mainValue} and {negativeValue}: {mainValue.CompareTo negativeValue} ({enum<Comparison>(mainValue.CompareTo negativeValue)})."
20+
21+
printfn $"Comparing {mainValue} and {positiveValue}: {mainValue.CompareTo positiveValue} ({enum<Comparison>(mainValue.CompareTo positiveValue)})."
22+
23+
// The example displays the following output:
24+
// Comparing 16325 and 0: 1 (GreaterThan).
25+
// Comparing 16325 and 16325: 0 (Equal).
26+
// Comparing 16325 and -1934: 1 (GreaterThan).
27+
// Comparing 16325 and 903624: -1 (LessThan).
28+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="CompareTo1.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
open System
2+
3+
let callToString () =
4+
// <Snippet1>
5+
let numbers = [| -1403; 0; 169; 1483104 |]
6+
for number in numbers do
7+
// Display value using default formatting.
8+
printf $"{number,-8} --> "
9+
// Display value with 3 digits and leading zeros.
10+
printf $"{number,11:D3}"
11+
// Display value with 1 decimal digit.
12+
printf $"{number,13:N1}"
13+
// Display value as hexadecimal.
14+
printf $"{number,12:X2}"
15+
// Display value with eight hexadecimal digits.
16+
printfn $"{number,14:X8}"
17+
18+
19+
// The example displays the following output:
20+
// -1403 --> -1403 -1,403.0 FFFFFA85 FFFFFA85
21+
// 0 --> 000 0.0 00 00000000
22+
// 169 --> 169 169.0 A9 000000A9
23+
// 1483104 --> 1483104 1,483,104.0 16A160 0016A160
24+
// </Snippet1>
25+
26+
27+
let callConvert () =
28+
// <Snippet2>
29+
let numbers = [| -146; 11043; 2781913 |]
30+
printfn $"""{"Value",8} {"Binary",32} {"Octal",11} {"Hex",10}"""
31+
for number in numbers do
32+
printfn $"{number,8} {Convert.ToString(number, 2),32} {Convert.ToString(number, 8),11} {Convert.ToString(number, 16),10}"
33+
34+
// The example displays the following output:
35+
// Value Binary Octal Hex
36+
// -146 11111111111111111111111101101110 37777777556 ffffff6e
37+
// 11043 10101100100011 25443 2b23
38+
// 2781913 1010100111001011011001 12471331 2a72d9
39+
// </Snippet2>
40+
41+
callToString ()
42+
printfn "-----"
43+
callConvert ()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Formatting1.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
open System
2+
open System.Numerics
3+
4+
let instantiateByAssignment () =
5+
// <Snippet1>
6+
let number1 = 64301
7+
let number2 = 25548612
8+
// </Snippet1>
9+
printfn $"{number1} - {number2}"
10+
11+
let instantiateByNarrowingConversion () =
12+
// <Snippet2>
13+
let lNumber = 163245617L
14+
try
15+
let number1 = int lNumber
16+
printfn $"{number1}"
17+
with :? OverflowException ->
18+
printfn "{lNumber} is out of range of an Int32."
19+
20+
let dbl2 = 35901.997
21+
try
22+
let number2 = int dbl2
23+
printfn $"{number2}"
24+
with :? OverflowException ->
25+
printfn $"{dbl2} is out of range of an Int32."
26+
27+
let bigNumber = BigInteger 132451
28+
try
29+
let number3 = int bigNumber
30+
printfn $"{number3}"
31+
with :? OverflowException ->
32+
printfn $"{bigNumber} is out of range of an Int32."
33+
34+
// The example displays the following output:
35+
// 163245617
36+
// 35902
37+
// 132451
38+
// </Snippet2>
39+
40+
let parse () =
41+
// <Snippet3>
42+
let string1 = "244681"
43+
try
44+
let number1 = Int32.Parse string1
45+
printfn $"{number1}"
46+
with
47+
| :? OverflowException ->
48+
printfn "'{string1}' is out of range of a 32-bit integer."
49+
| :? FormatException ->
50+
printfn $"The format of '{string1}' is invalid."
51+
52+
let string2 = "F9A3C"
53+
try
54+
let number2 = Int32.Parse(string2, System.Globalization.NumberStyles.HexNumber)
55+
printfn $"{number2}"
56+
with
57+
| :? OverflowException ->
58+
printfn $"'{string2}' is out of range of a 32-bit integer."
59+
| :? FormatException ->
60+
printfn $"The format of '{string2}' is invalid."
61+
62+
// The example displays the following output:
63+
// 244681
64+
// 1022524
65+
// </Snippet3>
66+
67+
let instantiateByWideningConversion () =
68+
// <Snippet4>
69+
let value1 = 124y
70+
let value2 = 1618s
71+
72+
let number1 = int value1
73+
let number2 = int value2
74+
// </Snippet4>
75+
()
76+
77+
instantiateByAssignment ()
78+
printfn "----"
79+
instantiateByNarrowingConversion ()
80+
printfn "----"
81+
parse ()
82+
printfn "----"
83+
instantiateByWideningConversion ()
84+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Instantiate1.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Parse1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let values =
7+
[ "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
8+
"0xFA1B"; "163042"; "-10"; "007"; "2147483647"
9+
"2147483648"; "16e07"; "134985.0"; "-12034"
10+
"-2147483648"; "-2147483649" ]
11+
12+
for value in values do
13+
try
14+
let number = Int32.Parse value
15+
printfn $"{value} --> {number}"
16+
with
17+
| :? FormatException ->
18+
printfn $"{value}: Bad Format"
19+
| :? OverflowException ->
20+
printfn $"{value}: Overflow"
21+
22+
23+
// The example displays the following output:
24+
// +13230 --> 13230
25+
// -0 --> 0
26+
// 1,390,146: Bad Format
27+
// $190,235,421,127: Bad Format
28+
// 0xFA1B: Bad Format
29+
// 163042 --> 163042
30+
// -10 --> -10
31+
// 007 --> 7
32+
// 2147483647 --> 2147483647
33+
// 2147483648: Overflow
34+
// 16e07: Bad Format
35+
// 134985.0: Bad Format
36+
// -12034 --> -12034
37+
// -2147483648 --> -2147483648
38+
// -2147483649: Overflow
39+
// </Snippet1>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Parse2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
let convert value (style: NumberStyles) =
8+
try
9+
let number = Int32.Parse(value, style)
10+
printfn $"Converted '{value}' to {number}."
11+
with
12+
| :? FormatException ->
13+
printfn $"Unable to convert '{value}'."
14+
| :? OverflowException ->
15+
printfn $"'{value}' is out of range of the Int32 type."
16+
17+
convert "104.0" NumberStyles.AllowDecimalPoint
18+
convert "104.9" NumberStyles.AllowDecimalPoint
19+
convert " $17,198,064.42" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
20+
convert "103E06" NumberStyles.AllowExponent
21+
convert "-1,345,791" NumberStyles.AllowThousands
22+
convert "(1,345,791)" (NumberStyles.AllowThousands ||| NumberStyles.AllowParentheses)
23+
24+
25+
// The example displays the following output to the console:
26+
// Converted '104.0' to 104.
27+
// '104.9' is out of range of the Int32 type.
28+
// ' $17,198,064.42' is out of range of the Int32 type.
29+
// Converted '103E06' to 103000000.
30+
// Unable to convert '-1,345,791'.
31+
// Converted '(1,345,791)' to -1345791.
32+
// </Snippet2>

0 commit comments

Comments
 (0)