Addition/Multiplication operations on loop - clojure

I'm a newbie to Clojure and need some help with my code.
Here's the situation:
My first table (products) looks like : (product_name price)
([1 (candies 6.5)]
[2 (sweets 1.75)]
[3 (jam 2.99)]
[4 (gum 1.25)])
My first table (products) looks like : (customer_name product_name quantity)
([1 (Sara candies 3)]
[2 (Joe jam 3)]
[3 (Sara gum 1)])
What I'm trying to do is if i type for example Sara, I'll get the sum of the sales made by Sara, which means: (3 * $6.5 + 1 * $1.25) = $20.75 (in this case)
I'm fine for the input part (I get the name of the customer as an input from the terminal)
However, my code:
(defn sales_prices_with_cond [name_cus] (map
(fn [y z]
(if (= (str name_cus) (str (nth (first z) 1)))
list (* (Double. (nth (second y) 1)) (Integer/parseInt (nth (second z) 2))))
)
products
sales))
(println (reduce + sales_prices_with_cond "Sara"))
Gives me the sum of ALL the sales*quantities. It's like the condition is skipped or maybe not well written ...
I also tried with (some) and got the same result...
Please Help :') .

Your tables don't look like valid Clojure code which makes it difficult to help you - are they strings? I would transform the first one into a map of the form:
(def prices {"candies" 6.5}) ;etc
Your second table would be something like:
(def sales [["sarah" "candies" 3]]) ;etc
And then your main function:
(defn sales-by-customer [name-cus]
(->> sales
(filter #(= (first %) name-cus))
(map #(* (last %) (prices (second %)))
(reduce +)))

Related

Plotting two lists in Clojure

I have two lists. For e.g.: list A is [1 2 3 2 2 1] and list B is [1.2 2.2 1 1 1 1]. I want to have the unique numbers of list A on the x-axis and sum of the corresponding entries in list B. For eg: For the above example, I want to plot {(1,2.2),(2,4.2),(3,1)} as a histogram ( not a scatter plot).
My requirement involves two steps.
First to sum values in list B for each unique value in list A
Plotting these sums against the corresponding values in list A as a histogram.
Can you please help me.
Edit:
Here is my attempt, based on the little I could understand from reading other answers on SO:
(def A [1 2 3 2 1])
(def B [1.2 2.3 2 1 1])
(for [x (distinct A)] (map first
(filter #(= (second %) x)
(map-indexed vector A))))
;; This gives the indices for each unique element in A
;; In this case, it gives ((0 4) (1 3) (2))
I am unable to figure out how to find how to get corresponding sum from list B. I tried the following but it does not work.
(apply nth B (map first
(filter #(= (second %) 1)
(map-indexed vector A))) )
;; In this case, it gives on the first element i.e. 1.2
As you can see, I am new to Clojure and functional programming languages. Can you please point me towards some examples which have solved similar problems?
Thanks in advance.
Edit:
Final solution for the first task:
(for [x (distinct A)] (reduce + 0.0 (map #(nth B %) (map first
(filter #(= (second %) x)
(map-indexed vector A))) ) ) )
;; This gives me the correct output (2.2 3.3 2.0)
P.S: I did not understand this concept of using (map #(nth B%)... I just stumbled onto it from other examples.
For the first task, I guess this way is a bit simpler:
(def A [1 2 3 2 2 1])
(def B [1.2 2.2 1 1 1 1])
(def C
(reduce (partial merge-with +)
(map hash-map A B))) ; Vector of key-values [{1 1.2} {2 2.2} ...]
; {1 2.2, 2 4.2, 3 1}
For the second task, there are many chart library options out there. I picked up clj-xchart as an example:
(require '[com.hypirion.clj-xchart :as c])
(let [x-values (keys C)
min-x (apply min x-values)
max-x (apply max x-values)]
(c/view
(c/category-chart
{"C" C}
{:title "Example"
:legend {:visible? false}
:x-axis {:order (range min-x max-x)}
:theme :ggplot2})))
And the final plot:

Lazy Sequence Clojure

I was trying to modify a vector of vector but ended up with lazy-seq inside. I am new to clojure. Can someone help me to get this correctly?
(require '[clojure.string :as str])
;;READ CUST.TXT
(def my_str(slurp "cust.txt"))
(defn esplit [x] (str/split x #"\|" ))
(def cust (vec (sort-by first (vec (map esplit (vec (str/split my_str #"\n")))))))
;;func to print
(for [i cust] (do (println (str (subs (str i) 2 3) ": [" (subs (str i) 5 (count (str i)))))))
;;CODE TO SEARCH CUST
(defn cust_find [x] (for [i cust :when (= (first i) x )] (do (nth i 1))))
(type (cust_find "2"))
;;READ PROD.TXT
(def my_str(slurp "prod.txt"))
(def prod (vec (sort-by first (vec (map esplit (vec (str/split my_str #"\n")))))))
;;func to print
(for [i prod] (do (println (str (subs (str i) 2 3) ": [" (subs (str i) 5 (count (str i)))))))
;;CODE TO SEARCH PROD
(defn prod_find [x y] (for [i prod :when (= (first i) x )] (nth i y)))
(prod_find "2" 1)
(def my_str(slurp "sales.txt"))
(def sales (vec (sort-by first (vec (map esplit (vec (str/split my_str #"\n")))))))
; (for [i (range(count sales))] (cust_find (nth (nth sales i) 1)))
; (defn fill_sales_1 [x]
; (assoc x 1
; (cust_find (nth x 1))))
; (def sales(map fill_sales_1 (sales)))
(def sales (vec (for [i (range(count sales))] (assoc (nth sales i) 1 (str (cust_find (nth (nth sales i) 1)))))))
; (for [i (range(count sales))] (assoc (nth sales i) 2 (str (prod_find (nth (nth sales i) 2) 1))))
(for [i sales] (println i))
When I print sales vector I get
[1 clojure.lang.LazySeq#10ae5ccd 1 3]
[2 clojure.lang.LazySeq#a5d0ddf9 2 3]
[3 clojure.lang.LazySeq#a5d0ddf9 1 1]
[4 clojure.lang.LazySeq#d80cb028 3 4]
If you need the text files I will upload them as well.
In Clojure, for and map, as well as other functions and macros working with sequences, generate a lazy sequence instead of a vector.
In the REPL, lazy sequences are usually fully computed when printing - to have it printed, it's enough to remove the str in your second to last line:
(def sales (vec (for [i (range(count sales))] (assoc (nth sales i) 1 (cust_find (nth (nth sales i) 1))))))
Just in case, note that your code can be prettified / simplified to convey the meaning better. For example, you are just iterating over a sequence of sales - you don't need to iterate over the indices and then get each item using nth:
(def sales
(vec (for [rec sales])
(assoc rec 1 (cust_find (nth rec 1)))))
Second, you can replace nth ... 1 with second - it will be easier to understand:
(def sales
(vec (for [rec sales])
(assoc rec 1 (cust_find (second rec))))
Or, alternatively, you can just use update instead of assoc:
(def sales
(vec (for [rec sales])
(update rec 1 cust_find)))
And, do you really need the outer vec here? You can do most of what you intend without it:
(def sales
(for [rec sales])
(update rec 1 cust_find))
Also, using underscores in Clojure function names is considered bad style: dashes (as in cust-find instead of cust_find) are easier to read and easier to type.
(for [i sales] (println (doall i)))
doall realizes a lazy sequence. Bare in mind that if the sequence is huge, you might not want to do this.

Clojure : idiomatic weighted mean of vectors

I would like to compute the weighted mean of vectors in an idiomatic way.
To illustrate what I want, imagine I have this data :
data 1 = [2 1] , weight 1 = 1
data 2 = [3 4], weight 2 = 2
Then mean = [(2*1 + 3*2)/(1+2) (1*1 + 2*4)/(1+2)] = [2.67 3.0]
Here is my code :
(defn meanv
"Returns the vector that is the mean of input ones.
You can also pass weights just like apache-maths.stats/mean"
([data]
(let [n (count (first data))]
(->> (for [i (range 0 n)]
(vec (map (i-partial nth i) data)))
(mapv stats/mean))))
([data weights]
(let [n (count (first data))]
(->> (for [i (range 0 n)]
(vec (map (i-partial nth i) data)))
(mapv (i-partial stats/mean weights))))))
Then
(meanv [[2 1] [3 4]] [1 2]) = [2.67 3.0]
Few notes :
stats/means takes 1 or 2 inputs.
One input version has weights = 1 by default.
Two inputs is the weighted version.
i-partial is like partial but the fn has reversed args
Ex : ((partial / 2) 1) = 2
((i-partial / 2) 1 = 1/2
So my function works, no problem.
But in a way I would like to implement it in a more idiomatic Clojure.
I tried many combinations with things like (map (fn [&xs ... but it does not work.
Is it possible to take all nth elements of undefined number of vectors and directly apply stats/mean ? I mean a one-liner
Thanks
EDIT (birdspider answer)
(defn meanv
([data]
(->> (apply mapv vector data)
(mapv stats/mean)))
([data weights]
(->> (apply mapv vector data)
(mapv (i-partial stats/mean weights)))))
And with
(defn transpose [m]
(apply mapv vector m))
(defn meanv
([data]
(->> (transpose data)
(mapv stats/mean)))
([data weights]
(->> (transpose data)
(mapv (i-partial stats/mean weights)))))
(def mult-v (partial mapv *))
(def sum-v (partial reduce +))
(def transpose (partial apply mapv vector))
(defn meanv [data weights]
(->> data
transpose
(map (partial mult-v weights))
(map sum-v)
(map #(/ % (sum-v weights)))))
First thing you want to do is to transpose the matrix (get the firsts, seconds, thirds, etc.)
See this SO page.
; https://stackoverflow.com/a/10347404/2645347
(defn transpose [m]
(apply mapv vector m))
Then I would do it as follows, input checks are utterly absent.
(defn meanv
([data]
; no weigths default to (1 1 1 ...
(meanv data (repeat (count data) 1))))
([data weigths]
(let [wf (mapv #(partial * %) weigths) ; vector of weight mult fns
wsum (reduce + weigths)]
(map-indexed
(fn [i datum]
(/
; map over datum apply corresponding weight-fn - then sum
(apply + (map-indexed #((wf %1) %2) datum))
wsum))
(transpose data)))))
(meanv [[2 1] [3 4]] [1 2]) => (8/3 3) ; (2.6666 3.0)
Profit!

A sumifs in Clojure

I am trying to do a sumifs (from Excel) in Clojure. I have a csv file which has column size categorized by Big, Medium, Small. And there is another column called as Revenue. What I am trying to do is to sum the revenue for each company by size.
This is what I've tried so far:
(math
sum
($for [Size] [row (vals input-data) :let [Size (:Size row)]]
(+ (:Revenue row) 0 )))
This is a fork of Clojure.
Here is a conventional way to do a sumif using standard Clojure methods:
(defn sumif [pred coll]
(reduce + (filter pred coll)))
Here is an example of using sumif to sum all odd numbers from 0 to 9:
(sumif odd? (range 10)) ; => 25
Update:
But if you want to aggregate your data by Size, then you may apply fmap method from algo.generic library to the results of group-by aggregation:
(defn aggregate-by [group-key sum-key data]
(fmap #(reduce + (map sum-key %))
(group-by group-key data)))
Here is an example:
(defn aggregate-by [group-key sum-key data]
(fmap #(reduce + (map sum-key %))
(group-by group-key data)))
I would suggest the following, assuming that you have a seq of maps representing your data available:
(defn revenue-sum [props]
(reduce (fn [acc {:keys [size revenue]}]
(update-in acc
[size]
#(if % (+ revenue %) revenue)))
{}
props))

Applying multiple filters to a collection in a thrush in Clojure

The following code
(let [coll [1 2 3 4 5]
filters [#(> % 1) #(< % 5)]]
(->> coll
(filter (first filters))
(filter (second filters))))
Gives me
(2 3 4)
Which is great, but how do I apply all the filters in coll without having to explicitly name them?
There may be totally better ways of doing this, but ideally I'd like to know an expression that can replace (filter (first filters)) (filter (second filters)) above.
Thanks!
Clojure 1.3 has a new every-pred function, which you could use thusly:
(filter (apply every-pred filters) coll)
This should work :-
(let [coll [1 2 3 4 5]
filters [#(> % 1) #(< % 5)]]
(filter (fn [x] (every? #(% x) filters)) coll)
)
I can't say I'm very proud of the following, but at least it works and allows for infinite filters:
(seq
(reduce #(clojure.set/intersection
(set %1)
(set %2)) (map #(filter % coll) filters)))
If you can use sets in place of seqs it would simplify the above code as follows:
(reduce clojure.set/intersection (map #(filter % coll) filters))
(let [coll [1 2 3 4 5]
filters [#(> % 1) #(< % 5)]]
(reduce (fn [c f] (filter f c)) coll filters))