Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created February 12, 2014 07:04
Show Gist options
  • Save tiensonqin/8951154 to your computer and use it in GitHub Desktop.
Save tiensonqin/8951154 to your computer and use it in GitHub Desktop.
%% erlang pattern matching and list comprehension vs clojure destructure and hof
-module(shop).
-export([total/1]).
cost(oranges) -> 5;
cost(newspaper) -> 8;
cost(apples) -> 2;
cost(pears) -> 9;
cost(milk) -> 7.
sum([H | T]) -> H + sum(T);
sum([]) -> 0.
total(Items) ->
sum([cost(A) * B || {A, B} <- Items]).
(def m {:orange {:quantity 4, :price 3}, :apple {:quantity 3, :price 2}})
(letfn [(per-count [item]
(let [{:keys [quantity price]} (val item)]
(* quantity price)))]
(reduce + (map per-count m)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment