2

In the excellent wreq Haskell library it is easy to add one or more query parameters to an URL:

opts = defaults & param "key" .~ ["value"]

however what I'm struggling to do is adding a list of parameters at a time:

params = [("key1", "value1"), ("key2", "value2"), ("key3", "value3")]

I know that there is function params but I could not find any example on how to use it.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187

1 Answers1

3

Both param <key> and params are lenses:

param  :: Text -> Lens' Options [Text] 
params ::         Lens' Options [(Text, Text)]

Without going too much into details, you can think of a lens focusing something, e.g. param "foo" focuses on some [Text] in Options that belong to the parameter foo (*). You can then change/query/manipulate those values with the correct function (see the lens package).

You've already used (.~) to replace the current values, and you can use it again with params:

default & params .~ [("key1", "value1"), ("key2", "value2"), ("key3", "value3")]

You can think of (.~) in this context as

(.~) :: Lens' a b -> b -> a -> a
-- concrete:
(.~) :: Lens' Options [(Text, Text)] -> [(Text, Text)] -> Options -> Options

(*) That's not 100% true, since lenses allow you to do all kinds of stuff, but good enough for this context.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Thank you so much Zeta, who would have thought it would be so simple! The usual me, trying to overcomplicate easy things -_- ! – Simone Trubian Feb 29 '16 at 15:44
  • @SimoneTrubian You're welcome. If this answer satisfies you, make sure [to accept it](http://meta.stackexchange.com/a/5235/191939), otherwise it will stay as "unanswered" in several filters. – Zeta Mar 01 '16 at 12:40