Skip to content

Commit 72433fa

Browse files
danslapmanclaude
andcommitted
Replace O(n²) nub with O(n) HashSet-based deduplication
Add Hashable instance to JsonSchemaTree via DeriveAnyClass, rewrite uniq in SequenceUtils to use a HashSet fold that preserves insertion order. Remove accidentally committed opl.csv. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ac70567 commit 72433fa

4 files changed

Lines changed: 12 additions & 12 deletions

File tree

opl.csv

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Schema.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
12
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE ImportQualifiedPost #-}
34
{-# LANGUAGE LambdaCase #-}
@@ -36,7 +37,7 @@ type JsonPath = Seq JsonPathElement
3637
data JsonSchemaTree
3738
= PathNode JsonPathElement (Seq JsonSchemaTree)
3839
| PathEnd
39-
deriving (Eq, Show, Typeable)
40+
deriving (Eq, Show, Typeable, Generic, Hashable)
4041

4142
type JsonSchema = Seq JsonSchemaTree
4243

src/SequenceUtils.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
module SequenceUtils where
22

3-
import Data.Foldable (find, toList)
4-
import qualified Data.List as L
3+
import Data.Foldable (find, foldl', toList)
4+
import Data.Hashable (Hashable)
5+
import qualified Data.HashSet as HS
56
import qualified Data.Maybe as Mb (mapMaybe)
67
import Data.Sequence
78
import Prelude hiding (concat, elem, foldl, foldl', foldl1, mapM, null, (++))
89

910
maybeNes :: Seq a -> Maybe (Seq a)
1011
maybeNes = find (not . null) . Just
1112

12-
uniq :: Eq a => Seq a -> Seq a
13-
uniq = fromList . L.nub . toList
13+
uniq :: (Hashable a, Eq a) => Seq a -> Seq a
14+
uniq = snd . foldl' step (HS.empty, empty)
15+
where
16+
step (seen, acc) x
17+
| HS.member x seen = (seen, acc)
18+
| otherwise = (HS.insert x seen, acc |> x)
1419

1520
mapMaybe :: (a -> Maybe b) -> Seq a -> Seq b
1621
mapMaybe _ Empty = empty

test/SequenceUtilsSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ testUniqEmptySequence :: Test
1414
testUniqEmptySequence = TestCase $ assertEqual "uniq empty sequence" (empty :: Seq Int) (uniq (empty :: Seq Int))
1515

1616
testUniqNonEmptySequence :: Test
17-
testUniqNonEmptySequence = TestCase $ assertEqual "uniq non-empty sequence" (fromList [1, 2, 3]) (uniq $ fromList [1, 2, 2, 3])
17+
testUniqNonEmptySequence = TestCase $ assertEqual "uniq non-empty sequence" (fromList [1, 2, 3] :: Seq Int) (uniq $ fromList [1, 2, 2, 3])
1818

1919
testMapMaybeEmptySequence :: Test
2020
testMapMaybeEmptySequence = TestCase $ assertEqual "mapMaybe empty sequence" (empty :: Seq Int) (mapMaybe (const Nothing) (empty :: Seq Int))

0 commit comments

Comments
 (0)