-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSI.txt
203 lines (163 loc) · 5.97 KB
/
FSI.txt
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> # silentCd @"c:\Users\scott\source\repos\cps-demo";;
- # 1 @"c:\Users\scott\source\repos\cps-demo\Prototype.fsx"
- ;;
> #I __SOURCE_DIRECTORY__ // Add the script file location to the include file path.
-
- open System.IO
- open System.Text.RegularExpressions
-
- let replaceFirstItem itm replacement lst =
- let rec replace isFound acc list =
- match list with
- | [] -> acc |> List.rev
- | h :: t ->
- if h = itm then
- if isFound then
- replace true (h :: acc) t
- else
- replace true (replacement :: acc) t
- else
- replace isFound (h :: acc) t
-
- replace false [] lst
-
- // let testList = [ 1; 4; 6; 4; 7 ]
- // let test = testList |> replaceFirstItem 4 42
-
- let replaceFirstItemCPS item replacement list =
- let rec replace cont = function
- | x :: xs when x = item -> cont (replacement :: xs)
- | x :: xs -> replace (fun ys -> x :: ys |> cont) xs
- | [] -> list
- replace id list
-
- let load filePath =
- filePath
- |> File.ReadAllLines
-
- let words line= Regex.Matches(line, "\w+(-\w+)?") |> Seq.cast |> Seq.map (fun (x:Match) -> x.Value)
-
- let getAllWordsSeq lines =
- seq { for line in lines do
- yield! words line
- }
-
- let getAllTheWordsAsList () =
- // https://www.gutenberg.org/files/135/135-0.txt (Les Mis�rables by Victor Hugo)
- let lines = load (__SOURCE_DIRECTORY__ + @"\Les Misérables.txt")
- let allWords = getAllWordsSeq lines
- printfn "Number of lines read from file: %i" lines.Length
- printfn "Number of words read from file: %i" (Seq.length allWords)
- // allWords |> Seq.take 1000 |> Seq.iter (fun w -> printfn "%s" w)
- allWords
- |> List.ofSeq
-
- let allTheWords = getAllTheWordsAsList ()
-
- let repeat f item replacement list =
- [1..20] |> List.iter (fun _ -> f item replacement list |> ignore)
-
- let needle = "MISÉRABLES" // occurs near the beginning of the file
- printfn "1. replaceFirstItem with needle = '%s'" needle
- #time
- allTheWords |> repeat replaceFirstItem needle "PERSONNES-CONTENUES"
- #time
-
- printfn "1.CPS. replaceFirstItemCPS with needle = '%s'" needle
- #time
- allTheWords |> repeat replaceFirstItemCPS needle "PERSONNES-CONTENUES"
- #time
-
- let needle2 = "lichens" // occurs in the last chapter of the book/file
- printfn "2. replaceFirstItem with needle = '%s'" needle2
- #time
- allTheWords |> repeat replaceFirstItem needle2 "BANANAS"
- #time
-
- printfn "2.CPS. replaceFirstItemCPS with needle = '%s'" needle2
- #time
- allTheWords |> repeat replaceFirstItemCPS needle2 "BANANAS"
- #time
-
- let needle3 = "aWordThatNeverOccursInLesMisérables"
- printfn "3. replaceFirstItem with needle = '%s'" needle3
- #time
- allTheWords |> repeat replaceFirstItem needle3 "BANANAS"
- #time
-
- printfn "3.CPS. replaceFirstItemCPS with needle = '%s'" needle3
- #time
- allTheWords |> repeat replaceFirstItemCPS needle3 "BANANAS"
- #time
-
- printfn "Done."
-
- ;;
--> Added 'c:\Users\scott\source\repos\cps-demo' to library include path
Number of lines read from file: 67663
Number of words read from file: 578334
1. replaceFirstItem with needle = 'MISÉRABLES'
val replaceFirstItem :
itm:'a -> replacement:'a -> lst:'a list -> 'a list when 'a : equality
val replaceFirstItemCPS :
item:'a -> replacement:'a -> list:'a list -> 'a list when 'a : equality
val load : filePath:string -> string []
val words : line:string -> seq<string>
val getAllWordsSeq : lines:seq<string> -> seq<string>
val getAllTheWordsAsList : unit -> string list
val allTheWords : string list =
["The"; "Project"; "Gutenberg"; "EBook"; "of"; "Les"; "Misérables"; "by";
"Victor"; "Hugo"; "This"; "eBook"; "is"; "for"; "the"; "use"; "of";
"anyone"; "anywhere"; "at"; "no"; "cost"; "and"; "with"; "almost"; "no";
"restrictions"; "whatsoever"; "You"; "may"; "copy"; "it"; "give"; "it";
"away"; "or"; "re"; "use"; "it"; "under"; "the"; "terms"; "of"; "the";
"Project"; "Gutenberg"; "License"; "included"; "with"; "this"; "eBook";
"or"; "online"; "at"; "www"; "gutenberg"; "org"; "Title"; "Les";
"Misérables"; "Complete"; "in"; "Five"; "Volumes"; "Author"; "Victor";
"Hugo"; "Translator"; "Isabel"; "F"; "Hapgood"; "Release"; "Date"; "June";
"22"; "2008"; "EBook"; "135"; "Last"; "Updated"; "January"; "18"; "2016";
"Language"; "English"; "Character"; "set"; "encoding"; "UTF"; "8"; "START";
"OF"; "THIS"; "PROJECT"; "GUTENBERG"; "EBOOK"; "LES"; "MISÉRABLES";
"Produced"; "by"; ...]
val repeat :
f:('a -> 'b -> 'c -> 'd) -> item:'a -> replacement:'b -> list:'c -> unit
val needle : string = "MISÉRABLES"
--> Timing now on
Real: 00:00:03.992, CPU: 00:00:03.515, GC gen0: 125, gen1: 54, gen2: 7
val it : unit = ()
--> Timing now off
1.CPS. replaceFirstItemCPS with needle = 'MISÉRABLES'
val it : unit = ()
--> Timing now on
Real: 00:00:00.004, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
val it : unit = ()
--> Timing now off
2. replaceFirstItem with needle = 'lichens'
val needle2 : string = "lichens"
--> Timing now on
Real: 00:00:03.164, CPU: 00:00:02.968, GC gen0: 127, gen1: 51, gen2: 8
val it : unit = ()
--> Timing now off
2.CPS. replaceFirstItemCPS with needle = 'lichens'
val it : unit = ()
--> Timing now on
Real: 00:00:02.849, CPU: 00:00:02.703, GC gen0: 122, gen1: 46, gen2: 5
val it : unit = ()
--> Timing now off
3. replaceFirstItem with needle = 'aWordThatNeverOccursInLesMisérables'
val needle3 : string = "aWordThatNeverOccursInLesMisérables"
--> Timing now on
Real: 00:00:02.859, CPU: 00:00:02.781, GC gen0: 125, gen1: 48, gen2: 6
val it : unit = ()
--> Timing now off
3.CPS. replaceFirstItemCPS with needle = 'aWordThatNeverOccursInLesMisérables'
val it : unit = ()
--> Timing now on
Real: 00:00:01.662, CPU: 00:00:01.640, GC gen0: 63, gen1: 24, gen2: 4
val it : unit = ()
--> Timing now off
Done.
val it : unit = ()