Created
April 30, 2014 16:03
-
-
Save CraZySacX/e965decfd846fc6a8a17 to your computer and use it in GitHub Desktop.
Clojure to Haskell
This file contains 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
(defn- target | |
"generate a target from a user and host string. | |
If user and host are nil, evaluates to localhost. | |
If host is nil, evaluates to user@localhost. | |
If user is nil, evaluates to host. | |
Otherwise evaluates to user@host." | |
[user host] | |
(let [host (if (string? host) host "localhost") | |
user (if (string? user) (str user "@") "")] | |
(str user host))) |
This file contains 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
target :: String -> String -> String | |
target user host = user ++ "@" ++ host | |
{- How do I do the cases where host and user are not supplied? | |
My hunch is new functions with less args, but maybe there | |
is a simpler way -} | |
targetNoHost :: String -> String | |
targetNoHost user = target user "localhost" | |
targetNoUser :: String -> String | |
targetNoUser host = host |
I think this solution gives the desired API.
data NameAndHost = Name String | Host String | Both String String deriving Show
buildHostString :: NameAndHost -> String
buildHostString (Name n) = n ++ "@localhost"
buildHostString (Host h) = "anonymous@" ++ h
buildHostString (Both n h) = n ++ "@" ++ h
This code snippet can be used as such...
*Main> buildHostString $ Name "ben"
"ben@localhost"
*Main> buildHostString $ Host "google.com"
"[email protected]"
*Main> buildHostString $ Both "ben" "google.com"
"[email protected]"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you nee a Maybe instead of a string.