Skip to content

Commit 7f0c293

Browse files
committed
Core,Tests: use Map instead of Dictionary in hint parser
To make code less reliant on mutable state.
1 parent 253e185 commit 7f0c293

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/FSharpLint.Core/Framework/HintParserUtilities.fs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module MergeSyntaxTrees =
5959

6060
and [<CustomEquality; NoComparison>] Edges =
6161
{
62-
Lookup: Dictionary<int, Node>
62+
Lookup: Map<int, Node>
6363
AnyMatch: (char option * Node) list
6464
}
6565

@@ -79,7 +79,7 @@ module MergeSyntaxTrees =
7979

8080
static member Empty =
8181
{
82-
Lookup = Dictionary<_, _>()
82+
Lookup = Map.empty
8383
AnyMatch = List.Empty
8484
}
8585

@@ -270,15 +270,15 @@ module MergeSyntaxTrees =
270270

271271
let mergeHints hints =
272272
let rec getEdges transposed =
273-
let map = Dictionary<_, _>()
274-
275-
transposed
276-
|> List.choose (function
277-
| HintNode(expr, depth, rest) -> Some(getKey expr, expr, depth, rest)
278-
| EndOfHint(_) -> None)
279-
|> List.filter (isAnyMatch >> not)
280-
|> Seq.groupBy (fun (key, expr, _, _) -> Utilities.hash2 key (getHashCode expr))
281-
|> Seq.iter (fun (hashcode, items) -> map.Add(hashcode, mergeHints (getHints items)))
273+
let map =
274+
transposed
275+
|> List.choose (function
276+
| HintNode(expr, depth, rest) -> Some(getKey expr, expr, depth, rest)
277+
| EndOfHint(_) -> None)
278+
|> List.filter (isAnyMatch >> not)
279+
|> Seq.groupBy (fun (key, expr, _, _) -> Utilities.hash2 key (getHashCode expr))
280+
|> Seq.map (fun (hashcode, items) -> (hashcode, mergeHints (getHints items)))
281+
|> Map.ofSeq
282282

283283
let anyMatches =
284284
transposed

tests/FSharpLint.Core.Tests/Framework/TestHintParser.fs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ type TestMergeSyntaxTrees() =
1515

1616
[<Test>]
1717
member _.``Merge two function applications of same function with diff arguments, merged list has common prefix till args.``() =
18-
let toDictionary list =
19-
let dictionary = Dictionary<int, Node>()
20-
for (x, y) in list do dictionary.Add(x, y)
21-
dictionary
22-
2318
match (run phint "List.map id ===> id", run phint "List.map woof ===> woof") with
2419
| Success(hint, _, _), Success(hint2, _, _) ->
2520
let expectedEdges =
@@ -32,15 +27,15 @@ type TestMergeSyntaxTrees() =
3227
MatchedHint = [hint2] })
3328
(Utilities.hash2 SyntaxHintNode.Identifier "id",
3429
{ Edges = Edges.Empty
35-
MatchedHint = [hint] }) ] |> toDictionary
30+
MatchedHint = [hint] }) ] |> Map.ofList
3631
AnyMatch = [] }
37-
MatchedHint = [] }) ] |> toDictionary
32+
MatchedHint = [] }) ] |> Map.ofList
3833
AnyMatch = [] }
3934

4035
let expectedRoot =
4136
{ Lookup =
4237
[(Utilities.hash2 SyntaxHintNode.FuncApp 0,
43-
{ Edges = expectedEdges; MatchedHint = [] })] |> toDictionary
38+
{ Edges = expectedEdges; MatchedHint = [] })] |> Map.ofList
4439
AnyMatch = [] }
4540

4641
let mergedHint = MergeSyntaxTrees.mergeHints [hint; hint2]

0 commit comments

Comments
 (0)