Skip to content

Commit f6af3fc

Browse files
committed
System.Console F# snippets
1 parent 1dcbb0b commit f6af3fc

File tree

78 files changed

+1924
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1924
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// <Snippet1>
2+
open System
3+
open System.IO
4+
5+
let tabSize = 4
6+
let usageText = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt"
7+
8+
[<EntryPoint>]
9+
let main args =
10+
if args.Length < 2 then
11+
printfn $"{usageText}"
12+
else
13+
try
14+
use writer = new StreamWriter(args[1])
15+
Console.SetOut writer
16+
Console.SetIn(new StreamReader(args[0]))
17+
let mutable i = Console.Read()
18+
while i <> -1 do
19+
let c = char i
20+
if c = '\t' then
21+
Console.WriteLine(("").PadRight(tabSize, ' '))
22+
else
23+
printf $"{c}"
24+
i <- Console.Read()
25+
// Recover the standard output stream so that a
26+
// completion message can be displayed.
27+
use standardOutput = new StreamWriter(Console.OpenStandardOutput())
28+
standardOutput.AutoFlush <- true
29+
Console.SetOut standardOutput
30+
printfn $"EXPANDTABSEX has completed the processing of {args[0]}."
31+
with :? IOException as e ->
32+
let errorWriter = Console.Error
33+
errorWriter.WriteLine e.Message
34+
errorWriter.WriteLine usageText
35+
0
36+
37+
// </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="expandtabsex.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//<snippet1>
2+
// This example demonstrates the Console.Beep() method.
3+
open System
4+
5+
[<EntryPoint>]
6+
let main args =
7+
if args.Length = 1 then
8+
match Int32.TryParse args[0] with
9+
| true, x when x >= 1 && x <= 9 ->
10+
for i = 1 to x do
11+
Console.WriteLine $"Beep number {i}."
12+
Console.Beep()
13+
| _ ->
14+
Console.WriteLine "Usage: Enter the number of times (between 1 and 9) to beep."
15+
else
16+
Console.WriteLine "Usage: Enter the number of times (between 1 and 9) to beep."
17+
18+
0
19+
20+
// This example produces the following results:
21+
22+
// >beep
23+
// Usage: Enter the number of times (between 1 and 9) to beep
24+
25+
// >beep 9
26+
// Beep number 1.
27+
// Beep number 2.
28+
// Beep number 3.
29+
// Beep number 4.
30+
// Beep number 5.
31+
// Beep number 6.
32+
// Beep number 7.
33+
// Beep number 8.
34+
// Beep number 9.
35+
//</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="beep.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//<snippet1>
2+
// This example demonstrates the Console.Beep(Int32, Int32) method
3+
open System
4+
open System.Threading
5+
6+
// Define the frequencies of notes in an octave, as well as
7+
// silence (rest).
8+
type Tone =
9+
| REST = 0
10+
| GbelowC = 196
11+
| A = 220
12+
| Asharp = 233
13+
| B = 247
14+
| C = 262
15+
| Csharp = 277
16+
| D = 294
17+
| Dsharp = 311
18+
| E = 330
19+
| F = 349
20+
| Fsharp = 370
21+
| G = 392
22+
| Gsharp = 415
23+
24+
// Define the duration of a note in units of milliseconds.
25+
type Duration =
26+
| WHOLE = 1600
27+
| HALF = 800
28+
| QUARTER = 400
29+
| EIGHTH = 200
30+
| SIXTEENTH = 100
31+
32+
// Define a note as a frequency (tone) and the amount of
33+
// time (duration) the note plays.
34+
[<Struct>]
35+
type Note =
36+
{ Tone: Tone
37+
Duration: Duration }
38+
39+
// Play the notes in a song.
40+
let play tune =
41+
for n in tune do
42+
if n.Tone = Tone.REST then
43+
Thread.Sleep(int n.Duration)
44+
else
45+
Console.Beep(int n.Tone, int n.Duration)
46+
47+
// Declare the first few notes of the song, "Mary Had A Little Lamb".
48+
let mary =
49+
[ { Tone = Tone.B; Duration = Duration.QUARTER }
50+
{ Tone = Tone.A; Duration = Duration.QUARTER }
51+
{ Tone = Tone.GbelowC; Duration = Duration.QUARTER }
52+
{ Tone = Tone.A; Duration = Duration.QUARTER }
53+
{ Tone = Tone.B; Duration = Duration.QUARTER }
54+
{ Tone = Tone.B; Duration = Duration.QUARTER }
55+
{ Tone = Tone.B; Duration = Duration.HALF }
56+
{ Tone = Tone.A; Duration = Duration.QUARTER }
57+
{ Tone = Tone.A; Duration = Duration.QUARTER }
58+
{ Tone = Tone.A; Duration = Duration.HALF }
59+
{ Tone = Tone.B; Duration = Duration.QUARTER }
60+
{ Tone = Tone.D; Duration = Duration.QUARTER }
61+
{ Tone = Tone.D; Duration = Duration.HALF } ]
62+
63+
// Play the song
64+
play mary
65+
66+
// This example plays the first few notes of "Mary Had A Little Lamb"
67+
// through the console speaker.
68+
//</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="b2.fs" />
8+
</ItemGroup>
9+
</Project>
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="hw.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//<snippet1>
2+
// This example demonstrates the Console.BufferHeight and
3+
// Console.BufferWidth properties.
4+
open System
5+
6+
printfn $"The current buffer height is {Console.BufferHeight} rows."
7+
printfn $"The current buffer width is {Console.BufferWidth} columns."
8+
9+
// This example produces the following results:
10+
//
11+
// The current buffer height is 300 rows.
12+
// The current buffer width is 85 columns.
13+
//</snippet1>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// <Snippet1>
2+
open System
3+
4+
let myHandler sender (args: ConsoleCancelEventArgs) =
5+
printfn "\nThe read operation has been interrupted."
6+
7+
printfn $" Key pressed: {args.SpecialKey}"
8+
9+
printfn $" Cancel property: {args.Cancel}"
10+
11+
// Set the Cancel property to true to prevent the process from terminating.
12+
printfn "Setting the Cancel property to true..."
13+
args.Cancel <- true
14+
15+
// Announce the new value of the Cancel property.
16+
printfn $" Cancel property: {args.Cancel}"
17+
printfn "The read operation will resume...\n"
18+
19+
// Establish an event handler to process key press events.
20+
Console.CancelKeyPress.AddHandler(ConsoleCancelEventHandler myHandler)
21+
22+
let mutable quit = false
23+
while not quit do
24+
printf "Press any key, or 'X' to quit, or "
25+
printfn "CTRL+C to interrupt the read operation:"
26+
27+
// Start a console read operation. Do not display the input.
28+
let cki = Console.ReadKey true
29+
30+
// Announce the name of the key that was pressed .
31+
printfn $" Key pressed: {cki.Key}\n"
32+
33+
// Exit if the user pressed the 'X' key.
34+
if cki.Key = ConsoleKey.X then
35+
quit <- true
36+
37+
// The example displays output similar to the following:
38+
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
39+
// Key pressed: J
40+
//
41+
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
42+
// Key pressed: Enter
43+
//
44+
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
45+
//
46+
// The read operation has been interrupted.
47+
// Key pressed: ControlC
48+
// Cancel property: False
49+
// Setting the Cancel property to true...
50+
// Cancel property: True
51+
// The read operation will resume...
52+
//
53+
// Key pressed: Q
54+
//
55+
// Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
56+
// Key pressed: X
57+
// </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="ckp.fs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)