Skip to content

Instantly share code, notes, and snippets.

@kozross

kozross/Route.hs Secret

Created January 7, 2021 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kozross/fd5a1ec16403de70190dc61b5655e635 to your computer and use it in GitHub Desktop.
Save kozross/fd5a1ec16403de70190dc61b5655e635 to your computer and use it in GitHub Desktop.
mkFreshRoute :: (Functor f) => URL -> (Maybe a -> f a) -> [Int] -> f (RouteState a)
mkFreshRoute url@(URL v) f = \case
[] -> Terminal . RouteEnd . (url,) <$> f Nothing
(ix : ixes) -> let next = mkFreshRoute url f ixes in
NonTerminal Nothing <$> case v V.! ix of
Capture _ -> This . WildcardLink <$> next
NonCapture lit -> That . LiteralLink . HM.singleton lit <$> next
@treblacy
Copy link

treblacy commented Jan 7, 2021

-- You made the very good move of isolation out "next = mkFreshRoute url f ixes" i.e. "next = simple recursive call on ixes"
-- so the rest is easy:

mkFreshRoute url@(URL v) f = foldr z op
  where
    z = Terminal . RouteEnd . (url,) <$> f Nothing
    op ix next =
        NonTerminal Nothing <$> case v V.! ix of
            Capture _      -> This . WildcardLink <$> next
            NonCapture lit -> That . LiteralLink . HM.singleton lit <$> next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment