Skip to content

Commit

Permalink
Use list instead of Set for getTransitiveClosure
Browse files Browse the repository at this point in the history
  • Loading branch information
ggzor authored and chpatrick committed Jun 3, 2024
1 parent 1ea2172 commit 6ac2ad4
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/Data/Aeson/TypeScript/Recursive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,26 @@ import Data.Function
import qualified Data.List as L
import Data.Maybe
import Data.Proxy
import qualified Data.Set as S
import Data.String.Interpolate
import Language.Haskell.TH as TH
import Language.Haskell.TH.Datatype
import Language.Haskell.TH.Syntax hiding (lift)

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.7

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.2.8

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.8.1

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.0.2

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.8.1

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.6.3

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 9.4.7

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / stack / ghc 8.10.7

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.2.8

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.0.2

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 8.10.7

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.4.7

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.8.1

The import of ‘Language.Haskell.TH.Syntax’ is redundant

Check warning on line 34 in src/Data/Aeson/TypeScript/Recursive.hs

View workflow job for this annotation

GitHub Actions / macOS-latest / ghc 9.6.3

The import of ‘Language.Haskell.TH.Syntax’ is redundant
import Data.Containers.ListUtils (nubOrd)


getTransitiveClosure :: S.Set TSType -> S.Set TSType
getTransitiveClosure = fix $ \loop items ->
let items' = S.unions (items : [getMore x | x <- S.toList items])
in if
| items' == items -> items
| otherwise -> loop items'

where getMore :: TSType -> S.Set TSType
getMore (TSType x) = S.fromList $ getParentTypes x
getTransitiveClosure :: [TSType] -> [TSType]
getTransitiveClosure initialTypes = fix (\loop items -> let items' = nubOrd (items ++ concatMap getMore items) in
if | items' == items -> items
| otherwise -> loop items'
) initialTypes
where getMore :: TSType -> [TSType]
getMore (TSType x) = getParentTypes x

getTypeScriptDeclarationsRecursively :: (TypeScript a) => Proxy a -> [TSDeclaration]
getTypeScriptDeclarationsRecursively initialType = S.toList $ S.fromList declarations
getTypeScriptDeclarationsRecursively initialType = nubOrd declarations
where
closure = getTransitiveClosure (S.fromList [TSType initialType])
declarations = mconcat [getTypeScriptDeclarations x | TSType x <- S.toList closure]
closure = getTransitiveClosure [TSType initialType]
declarations = mconcat [getTypeScriptDeclarations x | TSType x <- closure]


-- * Recursively deriving missing TypeScript interfaces
Expand Down

0 comments on commit 6ac2ad4

Please sign in to comment.