Reputation:
I have the following methods:
mainFunction arg1 = "http://someBaseUrl.com/" ++ arg1
url1 x = mainFunction x ++ "/subUrl1"
url2 x = mainFunction x ++ "/subUrl2"
url3 x = mainFunction x ++ "/subUrl3"
url4 x = mainFunction x ++ "/subUrl4"
My gut tells me that it has to be simplified. But I can't figure out how.
I can't do this, though, because it's not the same:
url1 = (mainFunction .) ++ "/subUrl1"
Your suggestions?
Upvotes: 0
Views: 129
Reputation: 22626
You can do
url s x = mainFunction x ++ s
url1 = url "/subUrl1"
url2 = url "/subUrl2"
etc ...
If you really to write as less code as possible and avoid to call url
tree times, you can use this trick (however, I'm not sure it is recommended)
[url1, url2, url3, url4] = map url ["/subUrl1", "/subUrl2", "subUrl3", "subUrl4"]
This will declare the four functions url1
, ... url4
.
Upvotes: 5
Reputation: 1986
You can concatenate strings to make your URL. To convert a non-string value to a string, you can use show
if it is an instance of Show.
url a b = "http://someBaseUrl.com/" ++ a ++ "/subUrl" ++ (show b)
The url function takes a "string" (list of chars) [Char] and another type 'a' (which is also an instance of Show) and returns a new "string".
Prelude> let url a b = "http://someBaseUrl.com/" ++ a ++ "/subUrl" ++ (show b)
Prelude> url "foo" 3
"http://someBaseUrl.com/foo/subUrl3"
Prelude> :t url
url :: Show a => [Char] -> a -> [Char]
Upvotes: 0
Reputation: 1076
Why are you not using a single function with two arguments?
url :: (Show a) => String -> a -> String
url x n = "www" ++ x ++ "/vvv" ++ show n
You should also use Text
when dealing with URLs and unicode in general.
Upvotes: 1