Skip to content

Commit c1770b0

Browse files
albert-dubaronfel
andauthored
System.Boolean F# snippets (#7430)
* boolean f# snippets * Update Boolean.xml * Update xml/System/Boolean.xml Co-authored-by: Chet Husk <[email protected]> * Update samples/snippets/fsharp/VS_Snippets_CLR_System/system.boolean.structure/fs/format3.fs Co-authored-by: Chet Husk <[email protected]> * string interpolation * Move snippets Co-authored-by: Chet Husk <[email protected]>
1 parent 75a8eb4 commit c1770b0

File tree

19 files changed

+572
-4
lines changed

19 files changed

+572
-4
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//<snippet1>
2+
// This example demonstrates the generic and non-generic versions of the
3+
// CompareTo method for several base types.
4+
// The non-generic version takes a parameter of type Object, while the generic
5+
// version takes a type-specific parameter, such as Boolean, Int32, or Double.
6+
open System
7+
8+
let show caption (var1: obj) (var2: obj) resultGeneric resultNonGeneric =
9+
printf "%s" caption
10+
if resultGeneric = resultNonGeneric then
11+
let relation =
12+
if resultGeneric < 0 then "less than"
13+
elif resultGeneric > 0 then "greater than"
14+
else "equal to"
15+
printfn $"{var1} is {relation} {var2}"
16+
17+
// The following condition will never occur because the generic and non-generic
18+
// CompareTo methods are equivalent.
19+
else
20+
printfn $"Generic CompareTo = {resultGeneric} non-generic CompareTo = {resultNonGeneric}"
21+
22+
let now = DateTime.Now
23+
// Time span = 11 days, 22 hours, 33 minutes, 44 seconds
24+
let tsX = TimeSpan(11, 22, 33, 44)
25+
// Version = 1.2.333.4
26+
let versX = Version "1.2.333.4"
27+
// Guid = CA761232-ED42-11CE-BACD-00AA0057B223
28+
let guidX = Guid "{CA761232-ED42-11CE-BACD-00AA0057B223}"
29+
30+
let a1, a2 = true, true
31+
let b1, b2 = 1uy, 1uy
32+
let c1, c2 = -2s, 2s
33+
let d1, d2 = 3, 3
34+
let e1, e2 = 4L, -4L
35+
let f1, f2 = -5.5m, 5.5m
36+
let g1, g2 = 6.6f, 6.6f
37+
let h1, h2 = 7.7, -7.7
38+
let i1, i2 = 'A', 'A'
39+
let j1, j2 = "abc", "abc"
40+
let k1, k2 = now, now
41+
let l1, l2 = tsX, tsX
42+
let m1, m2 = versX, Version "2.0"
43+
let n1, n2 = guidX, guidX
44+
45+
// The following types are not CLS-compliant.
46+
let w1, w2 = 8y, 8y
47+
let x1, x2 = 9us, 9us
48+
let y1, y2 = 10u, 10u
49+
let z1, z2 = 11uL, 11uL
50+
51+
printfn "\nThe following is the result of using the generic and non-generic\nversions of the CompareTo method for several base types:\n"
52+
try
53+
// The second and third show function call parameters are automatically boxed because
54+
// the second and third show function declaration arguments expect type Object.
55+
show "Boolean: " a1 a2 (a1.CompareTo a2) (a1.CompareTo (a2 :> obj))
56+
57+
show "Byte: " b1 b2 (b1.CompareTo b2) (b1.CompareTo (b2 :> obj))
58+
show "Int16: " c1 c2 (c1.CompareTo c2) (c1.CompareTo (c2 :> obj))
59+
show "Int32: " d1 d2 (d1.CompareTo d2) (d1.CompareTo (d2 :> obj))
60+
show "Int64: " e1 e2 (e1.CompareTo e2) (e1.CompareTo (e2 :> obj))
61+
show "Decimal: " f1 f2 (f1.CompareTo f2) (f1.CompareTo (f2 :> obj))
62+
show "Single: " g1 g2 (g1.CompareTo g2) (g1.CompareTo (g2 :> obj))
63+
show "Double: " h1 h2 (h1.CompareTo h2) (h1.CompareTo (h2 :> obj))
64+
show "Char: " i1 i2 (i1.CompareTo i2) (i1.CompareTo (i2 :> obj))
65+
show "String: " j1 j2 (j1.CompareTo j2) (j1.CompareTo (j2 :> obj))
66+
show "DateTime: " k1 k2 (k1.CompareTo k2) (k1.CompareTo (k2 :> obj))
67+
show "TimeSpan: " l1 l2 (l1.CompareTo l2) (l1.CompareTo (l2 :> obj))
68+
show "Version: " m1 m2 (m1.CompareTo m2) (m1.CompareTo (m2 :> obj))
69+
show "Guid: " n1 n2 (n1.CompareTo n2) (n1.CompareTo (n2 :> obj))
70+
71+
printfn "\nThe following types are not CLS-compliant:"
72+
show "SByte: " w1 w2 (w1.CompareTo w2) (w1.CompareTo (w2 :> obj))
73+
show "UInt16: " x1 x2 (x1.CompareTo x2) (x1.CompareTo (x2 :> obj))
74+
show "UInt32: " y1 y2 (y1.CompareTo y2) (y1.CompareTo (y2 :> obj))
75+
show "UInt64: " z1 z2 (z1.CompareTo z2) (z1.CompareTo (z2 :> obj))
76+
with e -> printfn $"{e}"
77+
78+
79+
// This example produces the following results:
80+
// The following is the result of using the generic and non-generic versions of the
81+
// CompareTo method for several base types:
82+
// Boolean: True is equal to True
83+
// Byte: 1 is equal to 1
84+
// Int16: -2 is less than 2
85+
// Int32: 3 is equal to 3
86+
// Int64: 4 is greater than -4
87+
// Decimal: -5.5 is less than 5.5
88+
// Single: 6.6 is equal to 6.6
89+
// Double: 7.7 is greater than -7.7
90+
// Char: A is equal to A
91+
// String: abc is equal to abc
92+
// DateTime: 12/1/2003 5:37:46 PM is equal to 12/1/2003 5:37:46 PM
93+
// TimeSpan: 11.22:33:44 is equal to 11.22:33:44
94+
// Version: 1.2.333.4 is less than 2.0
95+
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 is equal to ca761232-ed42-11ce-bacd-00
96+
// aa0057b223
97+
// The following types are not CLS-compliant:
98+
// SByte: 8 is equal to 8
99+
// UInt16: 9 is equal to 9
100+
// UInt32: 10 is equal to 10
101+
// UInt64: 11 is equal to 11
102+
//</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="cat.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module binary1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let getBinaryString (value: byte) =
7+
let retValue = Convert.ToString(value, 2)
8+
String('0', 8 - retValue.Length) + retValue
9+
10+
let flags = [ true; false ]
11+
for flag in flags do
12+
// Get binary representation of flag.
13+
let value = BitConverter.GetBytes(flag)[0];
14+
printfn $"Original value: {flag}"
15+
printfn $"Binary value: {value} ({getBinaryString value})"
16+
// Restore the flag from its binary representation.
17+
let newFlag = BitConverter.ToBoolean([|value|], 0)
18+
printfn $"Restored value: {newFlag}\n"
19+
20+
// The example displays the following output:
21+
// Original value: True
22+
// Binary value: 1 (00000001)
23+
// Restored value: True
24+
//
25+
// Original value: False
26+
// Binary value: 0 (00000000)
27+
// Restored value: False
28+
// </Snippet1>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module conversion1
2+
3+
// <Snippet6>
4+
open System
5+
6+
let byteValue = 12uy
7+
printfn $"{Convert.ToBoolean byteValue}"
8+
let byteValue2 = 0uy
9+
printfn $"{Convert.ToBoolean byteValue2}"
10+
let intValue = -16345
11+
printfn $"{Convert.ToBoolean intValue}"
12+
let longValue = 945L
13+
printfn $"{Convert.ToBoolean longValue}"
14+
let sbyteValue = -12y
15+
printfn $"{Convert.ToBoolean sbyteValue}"
16+
let dblValue = 0.0
17+
printfn $"{Convert.ToBoolean dblValue}"
18+
let sngValue = 0.0001f
19+
printfn $"{Convert.ToBoolean sngValue}"
20+
21+
// The example displays the following output:
22+
// True
23+
// False
24+
// True
25+
// True
26+
// True
27+
// False
28+
// True
29+
// </Snippet6>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module conversion3
2+
3+
// <Snippet8>
4+
open System
5+
6+
let flag = true
7+
8+
let byteValue = Convert.ToByte flag
9+
printfn $"{flag} -> {byteValue}"
10+
11+
let sbyteValue = Convert.ToSByte flag
12+
printfn $"{flag} -> {sbyteValue}"
13+
14+
let dblValue = Convert.ToDouble flag
15+
printfn $"{flag} -> {dblValue}"
16+
17+
let intValue = Convert.ToInt32(flag);
18+
printfn $"{flag} -> {intValue}"
19+
20+
// The example displays the following output:
21+
// True -> 1
22+
// True -> 1
23+
// True -> 1
24+
// True -> 1
25+
// </Snippet8>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module format3
2+
3+
// <Snippet5>
4+
open System
5+
open System.Globalization
6+
7+
type BooleanFormatter(culture) =
8+
interface ICustomFormatter with
9+
member this.Format(_, arg, formatProvider) =
10+
if formatProvider <> this then null
11+
else
12+
match arg with
13+
| :? bool as value ->
14+
match culture.Name with
15+
| "en-US" -> string arg
16+
| "fr-FR" when value -> "vrai"
17+
| "fr-FR" -> "faux"
18+
| "ru-RU" when value -> "верно"
19+
| "ru-RU" -> "неверно"
20+
| _ -> string arg
21+
| _ -> null
22+
interface IFormatProvider with
23+
member this.GetFormat(formatType) =
24+
if formatType = typeof<ICustomFormatter> then this
25+
else null
26+
new() = BooleanFormatter CultureInfo.CurrentCulture
27+
28+
let cultureNames = [ ""; "en-US"; "fr-FR"; "ru-RU" ]
29+
for cultureName in cultureNames do
30+
let value = true
31+
let culture = CultureInfo.CreateSpecificCulture cultureName
32+
let formatter = BooleanFormatter culture
33+
34+
String.Format(formatter, "Value for '{0}': {1}", culture.Name, value)
35+
|> printfn "%s"
36+
37+
// The example displays the following output:
38+
// Value for '': True
39+
// Value for 'en-US': True
40+
// Value for 'fr-FR': vrai
41+
// Value for 'ru-RU': верно
42+
// </Snippet5>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="tostring1.fs" />
8+
<Compile Include="tostring2.fs" />
9+
<Compile Include="format3.fs" />
10+
<Compile Include="conversion1.fs" />
11+
<Compile Include="conversion3.fs" />
12+
<Compile Include="parse2.fs" />
13+
<Compile Include="parse3.fs" />
14+
<Compile Include="operations1.fs" />
15+
<Compile Include="size1.fs" />
16+
<Compile Include="binary1.fs" />
17+
<Compile Include="operations2.fs" />
18+
</ItemGroup>
19+
</Project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module operations1
2+
3+
// <Snippet10>
4+
open System
5+
open System.IO
6+
open System.Threading
7+
8+
let showSyntax errMsg =
9+
printfn $"{errMsg}\n\nSyntax: Example [[/f <filename> [/b]]\n"
10+
11+
let mutable isRedirected = false
12+
let mutable isBoth = false
13+
let mutable fileName = ""
14+
15+
let rec parse = function
16+
| [] -> ()
17+
| "-b" :: rest
18+
| "/b" :: rest ->
19+
isBoth <- true
20+
// Parse remaining arguments.
21+
parse rest
22+
| "-f" :: file :: rest
23+
| "/f" :: file :: rest ->
24+
isRedirected <- true
25+
fileName <- file
26+
// Parse remaining arguments.
27+
parse rest
28+
| "-f" :: []
29+
| "/f" :: [] ->
30+
isRedirected <- true
31+
// No more arguments to parse.
32+
| x -> showSyntax $"The {x} switch is not supported"
33+
34+
Environment.GetCommandLineArgs()[1..]
35+
|> List.ofArray
36+
|> parse
37+
38+
// If isBoth is True, isRedirected must be True.
39+
if isBoth && not isRedirected then
40+
showSyntax "The /f switch must be used if /b is used."
41+
// If isRedirected is True, a fileName must be specified.
42+
elif fileName = "" && isRedirected then
43+
showSyntax "The /f switch must be followed by a filename."
44+
else
45+
use mutable sw = null
46+
47+
// Handle output.
48+
let writeLine =
49+
if isRedirected then
50+
sw <- new StreamWriter(fileName)
51+
if isBoth then
52+
fun text ->
53+
printfn "%s" text
54+
sw.WriteLine text
55+
else sw.WriteLine
56+
else printfn "%s"
57+
58+
writeLine $"Application began at {DateTime.Now}"
59+
Thread.Sleep 5000
60+
writeLine $"Application ended normally at {DateTime.Now}"
61+
62+
// </Snippet10>
63+
module Evaluation =
64+
let someFunction () =
65+
let booleanValue = false
66+
67+
// <Snippet11>
68+
if booleanValue = true then
69+
// </Snippet11>
70+
()
71+
72+
// <Snippet12>
73+
if booleanValue then
74+
// </Snippet12>
75+
()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module operations2
2+
3+
// <Snippet13>
4+
let hasServiceCharges = [ true; false ]
5+
let subtotal = 120.62M
6+
let shippingCharge = 2.50M
7+
let serviceCharge = 5.00M
8+
9+
for hasServiceCharge in hasServiceCharges do
10+
let total =
11+
subtotal + shippingCharge + if hasServiceCharge then serviceCharge else 0M
12+
printfn $"hasServiceCharge = {hasServiceCharge}: The total is {total:C2}."
13+
14+
// The example displays output like the following:
15+
// hasServiceCharge = True: The total is $128.12.
16+
// hasServiceCharge = False: The total is $123.12.
17+
// </Snippet13>

0 commit comments

Comments
 (0)