-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.fs
More file actions
126 lines (91 loc) · 3.3 KB
/
Utils.fs
File metadata and controls
126 lines (91 loc) · 3.3 KB
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
module Utils
open System
open System.Collections.Generic
let printlist li =
for i in li do
printfn "%A" i
li
let printmap (m: Dictionary<'k, 'v>) =
for k in m.Keys do
printfn "%A %A" k m[k]
type Array2DWithMetadata<'T> =
{ Array: 'T array2d
Height: int
Width: int
MaxI: int
MaxJ: int }
member this.Item(i, j) = this.Array[i, j]
let printArray2D formatting (arr: Array2DWithMetadata<'a>) =
for i in [ 0 .. arr.MaxI ] do
for j in [ 0 .. arr.MaxJ ] do
printf formatting arr[i, j]
printfn ""
arr
let sequencesToArray2D (seqArr: 'a seq array) =
let arr2D =
try
array2D seqArr
with _ ->
failwith "Lines are not all of the same length"
{ Array = arr2D
Height = Array2D.length1 arr2D
Width = Array2D.length2 arr2D
MaxJ = Array2D.length1 arr2D - 1
MaxI = Array2D.length2 arr2D - 1 }
let asArray2D (strings: string array) =
let arr2D =
try
array2D strings
with _ ->
failwith "Lines are not all of the same length"
{ Array = arr2D
Height = Array2D.length1 arr2D
Width = Array2D.length2 arr2D
MaxJ = Array2D.length1 arr2D - 1
MaxI = Array2D.length2 arr2D - 1 }
let asIntArray2d (strings: string array) =
strings
|> Array.map (Seq.map (Char.GetNumericValue >> int))
|> sequencesToArray2D
let isWithinBounds arr (i, j) =
i >= 0 && arr.Width > i && j >= 0 && arr.Height > j
// Filter, but returns a tuple of the list of true and false evaluated values
// Returns: (truelist, falselist)
let filter2 predicate (s: 'a seq) : ('a list * 'a list) =
let accumulate (accT, accF) i =
if predicate i then i :: accT, accF else accT, i :: accF
Seq.fold accumulate ([], []) s
let getAllPossiblePairings indexedCollection =
let maxIdx = (List.length indexedCollection) - 1
[ for i in [ 0..maxIdx ] do
for j in [ i + 1 .. maxIdx ] do
yield indexedCollection[i], indexedCollection[j] ]
let rec getCombinations uniquePicks combinationLength pool =
match combinationLength with
| 1 -> pool |> List.map (fun x -> [ x ])
| _ ->
pool
|> List.mapi (fun i x ->
getCombinations uniquePicks (combinationLength - 1) (if uniquePicks then (List.removeAt i pool) else pool)
|> List.map (fun c -> x :: c))
|> List.collect id
let array2dFilterIj (predicate: 'a -> bool) (arr: 'a array2d) : ('a * (int * int)) list =
let w, h = Array2D.length2 arr, Array2D.length1 arr
[ for i in [ 0 .. (h - 1) ] do
for j in [ 0 .. (w - 1) ] do
if predicate arr[i, j] then
yield (arr[i, j], (i, j)) ]
let asNodeSequence (list: LinkedList<'T>) : LinkedListNode<'T> seq =
let iterate (node: LinkedListNode<'T>) =
if node <> null then Some(node, node.Next) else None
list.First |> Seq.unfold iterate
let collect (mapping: 'T -> 'T array) (list: LinkedList<'T>) =
for n in (asNodeSequence list) do
match mapping n.Value with
| [||] -> list.Remove(n)
| [| a |] -> n.Value <- a
| vals ->
n.Value <- vals[vals.Length - 1]
vals[0 .. vals.Length - 2]
|> Array.iter (fun x -> list.AddBefore(n, x) |> ignore)
list