/
CollectionInternals.fs
243 lines (219 loc) · 7.55 KB
/
CollectionInternals.fs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// $begin{copyright}
//
// This file is part of WebSharper
//
// Copyright (c) 2008-2016 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// $end{copyright}
/// Provides seq/list/array proxies
[<JavaScript>]
module internal WebSharper.CollectionInternals
open WebSharper.JavaScript
[<Name "WebSharper.Arrays.splitInto">]
let ArraySplitInto count (arr: 'T[]) =
if count <= 0 then failwith "Count must be positive"
let len = arr.Length
if len = 0 then
[| |]
else
let count = min count len
let res = Array.zeroCreate count : 'T[][]
let minChunkSize = len / count
let mutable startIndex = 0
for i = 0 to len % count - 1 do
res.JS.[i] <- Array.sub arr startIndex (minChunkSize + 1)
startIndex <- startIndex + minChunkSize + 1
for i = len % count to count - 1 do
res.JS.[i] <- Array.sub arr startIndex minChunkSize
startIndex <- startIndex + minChunkSize
res
[<Name "WebSharper.Arrays.contains">]
let ArrayContains (item: 'T) (arr: 'T[]) =
let mutable c = true
let mutable i = 0
let l = arr.Length
while c && i < l do
if arr.JS.[i] = item then
c <- false
else
i <- i + 1
not c
[<Name "WebSharper.Arrays.tryFindBack">]
let ArrayTryFindBack f (arr: _ []) =
let mutable res = None
let mutable i = Array.length arr - 1
while i >= 0 && Option.isNone res do
let r = arr.JS.[i]
if f r then res <- Some r
i <- i - 1
res
[<Name "WebSharper.Arrays.tryFindIndexBack">]
let ArrayTryFindIndexBack f (arr: _ []) =
let mutable res = None
let mutable i = Array.length arr - 1
while i >= 0 && Option.isNone res do
if f arr.[i] then res <- Some i
i <- i - 1
res
[<Name "WebSharper.Arrays.mapFold">]
let ArrayMapFold<'T, 'S, 'R> (f: 'S -> 'T -> 'R * 'S) (zero: 'S) (arr: 'T[]) : 'R[] * 'S =
let r = JavaScript.Array(Array.length arr)
let mutable acc = zero
for i = 0 to Array.length arr - 1 do
let a, b = f acc arr.JS.[i]
r.[i] <- a
acc <- b
r.Self, acc
[<Name "WebSharper.Arrays.mapFoldBack">]
let ArrayMapFoldBack<'T,'S,'R> (f: 'T -> 'S -> 'R * 'S) (arr: 'T[]) (zero: 'S) : 'R[] * 'S =
let r = JavaScript.Array<'R>(Array.length arr)
let mutable acc = zero
let len = Array.length arr
for j = 1 to len do
let i = len - j
let a, b = f arr.JS.[i] acc
r.[i] <- a
acc <- b
r.Self, acc
[<Name "WebSharper.Arrays.mapInPlace">]
let mapInPlace (f: 'T1 -> 'T2) (arr: 'T1 []) =
for i = 0 to Array.length arr - 1 do
arr.JS.[i] <- As (f arr.JS.[i])
[<Name "WebSharper.Arrays.mapiInPlace">]
let mapiInPlace (f: int -> 'T1 -> 'T2) (arr: 'T1 []) : 'T2[] =
for i = 0 to Array.length arr - 1 do
arr.JS.[i] <- As (f i arr.JS.[i])
As arr
[<Name "WebSharper.Arrays.sortInPlaceByDescending">]
let ArraySortInPlaceByDescending<'T,'U when 'U: comparison> (f: 'T -> 'U) (arr: 'T []) =
(mapiInPlace (fun i x -> x, (f x, i)) arr).JS.Sort(fun (x, y) -> - compare (snd x) (snd y)) |> mapInPlace fst
[<Name "WebSharper.Seq.tryHead">]
let SeqTryHead (s: seq<'T>) =
use e = Enumerator.Get s
if e.MoveNext() then Some e.Current else None
[<Name "WebSharper.Seq.tryItem">]
let SeqTryItem i (s: seq<'T>) =
if i < 0 then None else
let mutable j = 0
use e = Enumerator.Get s
let mutable go = true
while go && j <= i do
if e.MoveNext() then
j <- j + 1
else
go <- false
if go then Some e.Current else None
[<Name "WebSharper.Seq.tryLast">]
let SeqTryLast (s: seq<'T>) =
use e = Enumerator.Get s
if e.MoveNext() then
while e.MoveNext() do ()
Some e.Current
else None
[<Name "WebSharper.Seq.chunkBySize">]
let SeqChunkBySize (size: int) (s: seq<'T>) =
if size <= 0 then failwith "Chunk size must be positive"
Enumerable.Of <| fun () ->
let o = Enumerator.Get s
Enumerator.NewDisposing () (fun _ -> o.Dispose()) <| fun e ->
if o.MoveNext() then
let res = [|o.Current|]
while res.Length < size && o.MoveNext() do
res.JS.Push o.Current |> ignore
e.Current <- res
true
else false
[<Name "WebSharper.Arrays.countBy">]
let ArrayCountBy (f: 'T -> 'K) (a: 'T[]) : ('K * int)[] =
let d = System.Collections.Generic.Dictionary<'K, int>()
let keys = JavaScript.Array()
for i = 0 to a.Length - 1 do
let c = a.JS.[i]
let k = f c
if d.ContainsKey(k) then
d.[k] <- d.[k] + 1
else
keys.Push(k) |> ignore
d.Add(k, 1)
As<'K[]> keys |> mapInPlace (fun k -> (k, d.[k]))
As keys
[<Name "WebSharper.Seq.except">]
let SeqExcept (itemsToExclude: seq<'T>) (s: seq<'T>) =
Enumerable.Of <| fun () ->
let o = Enumerator.Get s
let seen = System.Collections.Generic.HashSet(itemsToExclude)
Enumerator.NewDisposing () (fun _ -> o.Dispose()) <| fun e ->
if o.MoveNext() then
let mutable cur = o.Current
let mutable has = seen.Add(cur)
while not has && o.MoveNext() do
cur <- o.Current
has <- seen.Add(cur)
if has then
e.Current <- cur
true
else
false
else
false
[<Name "WebSharper.List.skip">]
let ListSkip i (l : list<'T>) =
let mutable res = l
for j = 1 to i do
match res with
| _ :: t ->
res <- t
| [] -> failwith "Input list too short."
res
[<Name "WebSharper.Arrays.groupBy">]
let ArrayGroupBy (f: 'T -> 'K when 'K : equality) (a: 'T[]) : ('K * 'T[])[] =
let d = System.Collections.Generic.Dictionary<'K, 'T[]>()
let keys = JavaScript.Array()
for i = 0 to a.Length - 1 do
let c = a.JS.[i]
let k = f c
if d.ContainsKey(k) then
d.[k].JS.Push(c) |> ignore
else
keys.Push(k) |> ignore
d.Add(k, [| c |])
As<'K[]> keys |> mapInPlace (fun k -> (k, d.[k]))
As keys
[<Name "WebSharper.Seq.insufficient">]
let InsufficientElements() =
failwith "The input sequence has an insufficient number of elements."
[<Name "WebSharper.Seq.last">]
let SeqLast (s: seq<_>) =
use e = Enumerator.Get s
if not <| e.MoveNext() then InsufficientElements()
else
while e.MoveNext() do ()
e.Current
[<Name "WebSharper.Seq.contains">]
let SeqContains (el: 'T) (s: seq<'T>) =
use e = Enumerator.Get s
let mutable r = false
while not r && e.MoveNext() do
r <- e.Current = el
r
[<Name "WebSharper.List.skipWhile">]
let rec ListSkipWhile<'T> (predicate : 'T -> bool) (list : list<'T>) : list<'T> =
let mutable rest = list
while not (List.isEmpty rest) && predicate (List.head rest) do
rest <- List.tail rest
rest
[<Name "WebSharper.Seq.nonNegative">]
let InputMustBeNonNegative() =
failwith "The input must be non-negative."