Skip to content

Instantly share code, notes, and snippets.

@hukl
Created November 14, 2011 19:54
Show Gist options
  • Save hukl/1364952 to your computer and use it in GitHub Desktop.
Save hukl/1364952 to your computer and use it in GitHub Desktop.
Ruby vs Erlang building URL query params for a GET request
-module(myjoin).
-export([join/1, join/2, start/0]).
start() ->
join( [{foo,"bar"},{baz,"bang"}] ).
join([H|T]) ->
{K,V} = H,
Acc = atom_to_list(K) ++ "=" ++ V,
join(T, Acc).
join([H|T], Acc) ->
{K,V} = H,
Addition = "&" ++ atom_to_list(K) ++ "=" ++ V,
join(T, lists:append(Acc, Addition));
join([], Acc) ->
error_logger:info_msg("Result ~p ~n", [Acc]).
% More concise variant
secondjoin([{Key,Value}|T], Separator) ->
lists:flatten(
Key ++ "=" ++ Value ++ [Separator ++ K ++ "=" ++ V || {K,V} <- T]
).
thirdjoin([{Key,Value}|T], Separator) ->
lists:flatten(
[Key, "=", Value, [[Separator, K, "=", V] || {K,V} <- T]]
).
@hanshuebner
Copy link

;; this is teh common lisp
(format nil "{(A)=A^&~}" '(:foo bar :baz bla))

@hukl
Copy link
Author

hukl commented Nov 14, 2011

… I hope there is a more concise variant in erlang …

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment