Last active
December 30, 2015 09:18
-
-
Save jtbandes/c8adf9ab67ea636a2592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Arrow ( (&&&) ) | |
import qualified Control.Arrow ( first, second ) | |
import Data.Maybe ( fromMaybe ) | |
-- this also exists in Data.Graph.Inductive.Query.Monad | |
(><) :: (a -> c) -> (b -> d) -> (a, b) -> (c, d) | |
(><) f g = Control.Arrow.first f . Control.Arrow.second g | |
first :: [a] -> Maybe a | |
first [] = Nothing | |
first (x:_) = Just x | |
rest :: [a] -> [a] | |
rest [] = error "scoundrel!" | |
rest (_:xs) = xs | |
data Command = Cmd deriving (Show) -- or something more useful | |
lookUpCommand :: String -> Maybe Command | |
lookUpCommand = undefined -- or something more useful | |
fallbackCommand = Cmd -- or something more useful | |
commandAndRemainingArgs :: [String] -> (Command, [String]) | |
commandAndRemainingArgs args = | |
let commandName = first args in | |
let maybeCommandAndMaybeRest = ((>>= lookUpCommand) &&& ((rest args) <$)) commandName in | |
let commandAndRest = (fromMaybe fallbackCommand >< fromMaybe []) maybeCommandAndMaybeRest in | |
commandAndRest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While my original using
>>=
/&&&
/<$
/><
is horrible, I like that it makes a very clear pipeline: