diff options
author | eric thul <thul.eric@gmail.com> | 2015-05-02 10:27:36 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2015-05-02 10:27:36 -0400 |
commit | d997476f011ae50036ffcabc03af2eaf85f33967 (patch) | |
tree | 9c39c031c0489a63ff63a899e702fe4449600770 /src/Loader.purs | |
parent | 3dd30238e4edbf7c39455762ae696dac15d6ddec (diff) | |
download | purs-loader-d997476f011ae50036ffcabc03af2eaf85f33967.tar.gz purs-loader-d997476f011ae50036ffcabc03af2eaf85f33967.tar.zst purs-loader-d997476f011ae50036ffcabc03af2eaf85f33967.zip |
Optimizing dependency list generation
Improving the generation of the dependencies in `mkDeps` to avoid a
`RangeError` on large inputs.
Resolves #9
Diffstat (limited to 'src/Loader.purs')
-rw-r--r-- | src/Loader.purs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/Loader.purs b/src/Loader.purs index 523aa7a..aae51c0 100644 --- a/src/Loader.purs +++ b/src/Loader.purs | |||
@@ -9,9 +9,11 @@ import Control.Monad.Eff (Eff()) | |||
9 | import Control.Monad.Eff.Class (liftEff) | 9 | import Control.Monad.Eff.Class (liftEff) |
10 | import Control.Monad.Eff.Exception (error) | 10 | import Control.Monad.Eff.Exception (error) |
11 | 11 | ||
12 | import Data.Array ((!!), catMaybes, concat, nub, null) | 12 | import Data.Array ((!!), catMaybes, concat, filter, null) |
13 | import Data.Foldable (foldl) | ||
13 | import Data.Function (Fn2(), mkFn2) | 14 | import Data.Function (Fn2(), mkFn2) |
14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | 15 | import Data.Maybe (Maybe(..), fromMaybe, maybe) |
16 | import Data.Set (Set(), empty, insert, member, toList, unions) | ||
15 | import Data.String (joinWith, split) | 17 | import Data.String (joinWith, split) |
16 | import Data.String.Regex (Regex(), match, noFlags, regex) | 18 | import Data.String.Regex (Regex(), match, noFlags, regex) |
17 | import Data.StrMap (StrMap(), fromList, lookup) | 19 | import Data.StrMap (StrMap(), fromList, lookup) |
@@ -60,11 +62,16 @@ mkGraph files = (fromList <<< catMaybes) <$> sequence (parse <$> files) | |||
60 | return $ (\a -> tuple2 a { file: file, imports: imports }) <$> key | 62 | return $ (\a -> tuple2 a { file: file, imports: imports }) <$> key |
61 | 63 | ||
62 | mkDeps :: forall eff. String -> Graph -> [String] | 64 | mkDeps :: forall eff. String -> Graph -> [String] |
63 | mkDeps key graph = nub $ go [] key | 65 | mkDeps key graph = toList $ go empty key |
64 | where go acc key = | 66 | where |
65 | maybe acc (\a -> if null a.imports | 67 | go :: Set String -> String -> Set String |
66 | then acc | 68 | go acc key = |
67 | else concat $ go (acc <> a.imports) <$> a.imports) (lookup key graph) | 69 | let node = fromMaybe {file: "", imports: []} (lookup key graph) |
70 | uniq = filter (not <<< flip member acc) node.imports | ||
71 | acc' = foldl (flip insert) acc node.imports | ||
72 | in if null uniq | ||
73 | then acc' | ||
74 | else unions $ go acc' <$> uniq | ||
68 | 75 | ||
69 | addDeps :: forall eff. LoaderRef -> Graph -> [String] -> Eff (loader :: Loader | eff) Unit | 76 | addDeps :: forall eff. LoaderRef -> Graph -> [String] -> Eff (loader :: Loader | eff) Unit |
70 | addDeps ref graph deps = const unit <$> (sequence $ add <$> deps) | 77 | addDeps ref graph deps = const unit <$> (sequence $ add <$> deps) |