-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathProgram.cs
110 lines (93 loc) · 3.56 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using LanguageExt;
using static LanguageExt.Prelude;
namespace IOExamples;
class Program
{
static void Main(string[] args) =>
infiniteIterator();
//infiniteLoop(0).Run();
static void infiniteIterator()
{
// NOTE: This should be run in Release mode, otherwise you might get a space leak
for(var iter = Naturals.GetIterator(); !iter.IsEmpty; iter = iter.Tail)
{
if (iter.Head % 10000 == 0)
{
Console.WriteLine(iter.Head);
}
}
}
static IO<Unit> infiniteLoop(int value) =>
from _ in value % 10000 == 0
? writeLine($"{value}")
: Pure(unit)
from r in tail(infiniteLoop(value + 1))
select unit;
static IO<Unit> infiniteLoop1(int value) =>
(value % 10000 == 0
? writeLine($"{value}")
: Pure(unit))
.Bind(_ => infiniteLoop1(value + 1));
static IO<int> recursiveAskForNumber =>
from n in askForNumber(1)
from _ in writeLine($"Your number is: {n}")
select n;
static IO<int> askForNumber(int attempts) =>
from _ in writeLine($"Enter a number (attempt number: {attempts})")
from l in readLine
from n in int.TryParse(l, out var v)
? Pure(v)
: from _ in writeLine("That's not a number!")
from r in askForNumber(attempts + 1)
select r
select n;
// static readonly Transducer<int, int> folder =
// foldUntil(Schedule.spaced(TimeSpan.FromMilliseconds(100)),
// 0,
// (int s, int x) => s + x,
// valueIs: x => x % 10 == 0);
//
// static IO<Unit> folding =>
// from n in many(Range(1, 25))
// from _ in writeLine($"item flowing in: {n}")
// from s in n | folder
// from r in writeLine($"Total {s}")
// select unit;
// static IO<Unit> retrying =>
// from ix in many(Range(1, 10))
// from _1 in retry(from _2 in writeLine($"Enter a number to add to {ix}")
// from nm in readLine.Map(parseInt)
// from _3 in guard(nm.IsSome, Error.New("Please enter a valid integer"))
// from _4 in writeLine($"Number {ix} + {(int)nm} = {ix + (int)nm}")
// select unit)
// from _4 in waitOneSecond
// select unit;
static IO<Unit> repeating =>
repeat(Schedule.recurs(5),
from x in readNumber("Enter the first number to add")
from y in readNumber("Enter the second number to add")
from _ in writeLine($"{x} + {y} = {x + y}")
from t in waitOneSecond
select unit).As()
| writeLine("Obviously you don't know what a number is so I'm off.");
static IO<int> readNumber(string question) =>
retry(Schedule.recurs(3),
from _1 in writeLine(question)
from nx in readLine.Map(int.Parse)
select nx).As();
static readonly IO<string> readLine =
lift(() => Console.ReadLine() ?? "");
static IO<Unit> writeLine(string line) =>
lift(() =>
{
Console.WriteLine(line);
return unit;
});
static IO<Unit> waitOneSecond =>
wait(1000);
static IO<Unit> wait(int milliseconds) =>
yieldFor(milliseconds);
static IO<DateTime> now =>
lift(() => DateTime.Now);
}