Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Boolean F# snippets #7430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 102 additions & 0 deletions snippets/fsharp/System/Boolean/CompareTo/cat.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//<snippet1>
// This example demonstrates the generic and non-generic versions of the
// CompareTo method for several base types.
// The non-generic version takes a parameter of type Object, while the generic
// version takes a type-specific parameter, such as Boolean, Int32, or Double.
open System

let show caption (var1: obj) (var2: obj) resultGeneric resultNonGeneric =
printf "%s" caption
if resultGeneric = resultNonGeneric then
let relation =
if resultGeneric < 0 then "less than"
elif resultGeneric > 0 then "greater than"
else "equal to"
printfn $"{var1} is {relation} {var2}"

// The following condition will never occur because the generic and non-generic
// CompareTo methods are equivalent.
else
printfn $"Generic CompareTo = {resultGeneric} non-generic CompareTo = {resultNonGeneric}"

let now = DateTime.Now
// Time span = 11 days, 22 hours, 33 minutes, 44 seconds
let tsX = TimeSpan(11, 22, 33, 44)
// Version = 1.2.333.4
let versX = Version "1.2.333.4"
// Guid = CA761232-ED42-11CE-BACD-00AA0057B223
let guidX = Guid "{CA761232-ED42-11CE-BACD-00AA0057B223}"

let a1, a2 = true, true
let b1, b2 = 1uy, 1uy
let c1, c2 = -2s, 2s
let d1, d2 = 3, 3
let e1, e2 = 4L, -4L
let f1, f2 = -5.5m, 5.5m
let g1, g2 = 6.6f, 6.6f
let h1, h2 = 7.7, -7.7
let i1, i2 = 'A', 'A'
let j1, j2 = "abc", "abc"
let k1, k2 = now, now
let l1, l2 = tsX, tsX
let m1, m2 = versX, Version "2.0"
let n1, n2 = guidX, guidX

// The following types are not CLS-compliant.
let w1, w2 = 8y, 8y
let x1, x2 = 9us, 9us
let y1, y2 = 10u, 10u
let z1, z2 = 11uL, 11uL

printfn "\nThe following is the result of using the generic and non-generic\nversions of the CompareTo method for several base types:\n"
try
// The second and third show function call parameters are automatically boxed because
// the second and third show function declaration arguments expect type Object.
show "Boolean: " a1 a2 (a1.CompareTo a2) (a1.CompareTo (a2 :> obj))

show "Byte: " b1 b2 (b1.CompareTo b2) (b1.CompareTo (b2 :> obj))
show "Int16: " c1 c2 (c1.CompareTo c2) (c1.CompareTo (c2 :> obj))
show "Int32: " d1 d2 (d1.CompareTo d2) (d1.CompareTo (d2 :> obj))
show "Int64: " e1 e2 (e1.CompareTo e2) (e1.CompareTo (e2 :> obj))
show "Decimal: " f1 f2 (f1.CompareTo f2) (f1.CompareTo (f2 :> obj))
show "Single: " g1 g2 (g1.CompareTo g2) (g1.CompareTo (g2 :> obj))
show "Double: " h1 h2 (h1.CompareTo h2) (h1.CompareTo (h2 :> obj))
show "Char: " i1 i2 (i1.CompareTo i2) (i1.CompareTo (i2 :> obj))
show "String: " j1 j2 (j1.CompareTo j2) (j1.CompareTo (j2 :> obj))
show "DateTime: " k1 k2 (k1.CompareTo k2) (k1.CompareTo (k2 :> obj))
show "TimeSpan: " l1 l2 (l1.CompareTo l2) (l1.CompareTo (l2 :> obj))
show "Version: " m1 m2 (m1.CompareTo m2) (m1.CompareTo (m2 :> obj))
show "Guid: " n1 n2 (n1.CompareTo n2) (n1.CompareTo (n2 :> obj))

printfn "\nThe following types are not CLS-compliant:"
show "SByte: " w1 w2 (w1.CompareTo w2) (w1.CompareTo (w2 :> obj))
show "UInt16: " x1 x2 (x1.CompareTo x2) (x1.CompareTo (x2 :> obj))
show "UInt32: " y1 y2 (y1.CompareTo y2) (y1.CompareTo (y2 :> obj))
show "UInt64: " z1 z2 (z1.CompareTo z2) (z1.CompareTo (z2 :> obj))
with e -> printfn $"{e}"


// This example produces the following results:
// The following is the result of using the generic and non-generic versions of the
// CompareTo method for several base types:
// Boolean: True is equal to True
// Byte: 1 is equal to 1
// Int16: -2 is less than 2
// Int32: 3 is equal to 3
// Int64: 4 is greater than -4
// Decimal: -5.5 is less than 5.5
// Single: 6.6 is equal to 6.6
// Double: 7.7 is greater than -7.7
// Char: A is equal to A
// String: abc is equal to abc
// DateTime: 12/1/2003 5:37:46 PM is equal to 12/1/2003 5:37:46 PM
// TimeSpan: 11.22:33:44 is equal to 11.22:33:44
// Version: 1.2.333.4 is less than 2.0
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223 is equal to ca761232-ed42-11ce-bacd-00
// aa0057b223
// The following types are not CLS-compliant:
// SByte: 8 is equal to 8
// UInt16: 9 is equal to 9
// UInt32: 10 is equal to 10
// UInt64: 11 is equal to 11
//</snippet1>
9 changes: 9 additions & 0 deletions snippets/fsharp/System/Boolean/CompareTo/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="cat.fs" />
</ItemGroup>
</Project>
28 changes: 28 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/binary1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module binary1

// <Snippet1>
open System

let getBinaryString (value: byte) =
let retValue = Convert.ToString(value, 2)
String('0', 8 - retValue.Length) + retValue

let flags = [ true; false ]
for flag in flags do
// Get binary representation of flag.
let value = BitConverter.GetBytes(flag)[0];
printfn $"Original value: {flag}"
printfn $"Binary value: {value} ({getBinaryString value})"
// Restore the flag from its binary representation.
let newFlag = BitConverter.ToBoolean([|value|], 0)
printfn $"Restored value: {newFlag}\n"

// The example displays the following output:
// Original value: True
// Binary value: 1 (00000001)
// Restored value: True
//
// Original value: False
// Binary value: 0 (00000000)
// Restored value: False
// </Snippet1>
29 changes: 29 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/conversion1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module conversion1

// <Snippet6>
open System

let byteValue = 12uy
printfn $"{Convert.ToBoolean byteValue}"
let byteValue2 = 0uy
printfn $"{Convert.ToBoolean byteValue2}"
let intValue = -16345
printfn $"{Convert.ToBoolean intValue}"
let longValue = 945L
printfn $"{Convert.ToBoolean longValue}"
let sbyteValue = -12y
printfn $"{Convert.ToBoolean sbyteValue}"
let dblValue = 0.0
printfn $"{Convert.ToBoolean dblValue}"
let sngValue = 0.0001f
printfn $"{Convert.ToBoolean sngValue}"

// The example displays the following output:
// True
// False
// True
// True
// True
// False
// True
// </Snippet6>
25 changes: 25 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/conversion3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module conversion3

// <Snippet8>
open System

let flag = true

let byteValue = Convert.ToByte flag
printfn $"{flag} -> {byteValue}"

let sbyteValue = Convert.ToSByte flag
printfn $"{flag} -> {sbyteValue}"

let dblValue = Convert.ToDouble flag
printfn $"{flag} -> {dblValue}"

let intValue = Convert.ToInt32(flag);
printfn $"{flag} -> {intValue}"

// The example displays the following output:
// True -> 1
// True -> 1
// True -> 1
// True -> 1
// </Snippet8>
42 changes: 42 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/format3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module format3

// <Snippet5>
open System
open System.Globalization

type BooleanFormatter(culture) =
interface ICustomFormatter with
member this.Format(_, arg, formatProvider) =
if formatProvider <> this then null
else
match arg with
| :? bool as value ->
match culture.Name with
| "en-US" -> string arg
| "fr-FR" when value -> "vrai"
| "fr-FR" -> "faux"
| "ru-RU" when value -> "верно"
| "ru-RU" -> "неверно"
| _ -> string arg
| _ -> null
interface IFormatProvider with
member this.GetFormat(formatType) =
if formatType = typeof<ICustomFormatter> then this
else null
new() = BooleanFormatter CultureInfo.CurrentCulture

let cultureNames = [ ""; "en-US"; "fr-FR"; "ru-RU" ]
for cultureName in cultureNames do
let value = true
let culture = CultureInfo.CreateSpecificCulture cultureName
let formatter = BooleanFormatter culture

String.Format(formatter, "Value for '{0}': {1}", culture.Name, value)
|> printfn "%s"

// The example displays the following output:
// Value for '': True
// Value for 'en-US': True
// Value for 'fr-FR': vrai
// Value for 'ru-RU': верно
// </Snippet5>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="tostring1.fs" />
<Compile Include="tostring2.fs" />
<Compile Include="format3.fs" />
<Compile Include="conversion1.fs" />
<Compile Include="conversion3.fs" />
<Compile Include="parse2.fs" />
<Compile Include="parse3.fs" />
<Compile Include="operations1.fs" />
<Compile Include="size1.fs" />
<Compile Include="binary1.fs" />
<Compile Include="operations2.fs" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/operations1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module operations1

// <Snippet10>
open System
open System.IO
open System.Threading

let showSyntax errMsg =
printfn $"{errMsg}\n\nSyntax: Example [[/f <filename> [/b]]\n"

let mutable isRedirected = false
let mutable isBoth = false
let mutable fileName = ""

let rec parse = function
| [] -> ()
| "-b" :: rest
| "/b" :: rest ->
isBoth <- true
// Parse remaining arguments.
parse rest
| "-f" :: file :: rest
| "/f" :: file :: rest ->
isRedirected <- true
fileName <- file
// Parse remaining arguments.
parse rest
| "-f" :: []
| "/f" :: [] ->
isRedirected <- true
// No more arguments to parse.
| x -> showSyntax $"The {x} switch is not supported"

Environment.GetCommandLineArgs()[1..]
|> List.ofArray
|> parse

// If isBoth is True, isRedirected must be True.
if isBoth && not isRedirected then
showSyntax "The /f switch must be used if /b is used."
// If isRedirected is True, a fileName must be specified.
elif fileName = "" && isRedirected then
showSyntax "The /f switch must be followed by a filename."
else
use mutable sw = null

// Handle output.
let writeLine =
if isRedirected then
sw <- new StreamWriter(fileName)
if isBoth then
fun text ->
printfn "%s" text
sw.WriteLine text
else sw.WriteLine
else printfn "%s"

writeLine $"Application began at {DateTime.Now}"
Thread.Sleep 5000
writeLine $"Application ended normally at {DateTime.Now}"

// </Snippet10>
module Evaluation =
let someFunction () =
let booleanValue = false

// <Snippet11>
if booleanValue = true then
// </Snippet11>
()

// <Snippet12>
if booleanValue then
// </Snippet12>
()
17 changes: 17 additions & 0 deletions snippets/fsharp/System/Boolean/Overview/operations2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module operations2

// <Snippet13>
let hasServiceCharges = [ true; false ]
let subtotal = 120.62M
let shippingCharge = 2.50M
let serviceCharge = 5.00M

for hasServiceCharge in hasServiceCharges do
let total =
subtotal + shippingCharge + if hasServiceCharge then serviceCharge else 0M
printfn $"hasServiceCharge = {hasServiceCharge}: The total is {total:C2}."

// The example displays output like the following:
// hasServiceCharge = True: The total is $128.12.
// hasServiceCharge = False: The total is $123.12.
// </Snippet13>
Loading