From 03f09725cb5f162ae19eba5bba188f085a0c11a7 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Thu, 16 Feb 2017 08:18:44 -0500 Subject: Use an interface for dispatching. --- Dispatch.idr | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/Dispatch.idr b/Dispatch.idr index d7a8f97..c9883ab 100644 --- a/Dispatch.idr +++ b/Dispatch.idr @@ -3,13 +3,25 @@ module Main %default total data Matcher = - Directory String Matcher - | Capture Matcher + MDirectory String Matcher + | MCapture Matcher | End + +data Commands = Capture + +infixr 8 // +interface Matchable a where + (//) : a -> Matcher -> Matcher +Matchable String where + x // m = MDirectory x m + +Matchable Commands where + Capture // m = MCapture m + HandlerType : Matcher -> Type -HandlerType (Directory s m) = HandlerType m -HandlerType (Capture m) = String -> HandlerType m +HandlerType (MDirectory s m) = HandlerType m +HandlerType (MCapture m) = String -> HandlerType m HandlerType End = String Handler : Type @@ -26,10 +38,10 @@ dispatch handlers path = go handlers components where go ((m ** f) :: hs) cs = let next = go hs cs in case m of - Directory dir matcher => case cs of + MDirectory dir matcher => case cs of (c :: cs') => if c == dir then go ((matcher ** f) :: hs) cs' else next _ => next - Capture matcher => case cs of + MCapture matcher => case cs of (c :: cs') => go ((matcher ** f c) :: hs) cs' _ => next End => Just f @@ -39,11 +51,11 @@ helloHandler firstName lastName = "Hello, " ++ firstName ++ " " ++ lastName weatherHandler : String -> String weatherHandler place = "The weather in " ++ place ++ " is cold." - + handlers : List Handler handlers = [ - (Directory "hello" (Capture (Capture End)) ** helloHandler) - , (Directory "weather" (Capture End) ** weatherHandler) + ("hello" // Capture // Capture // End ** helloHandler) + , ("weather" // Capture // End ** weatherHandler) ] main : IO () -- cgit v1.2.3