Mod Syntax in Clojure - clojure

How do I write modulus syntax in programming language clojure?
For example the symbols money %= money_value.

There are two functions in Clojure you might want to try: mod and rem. They work the same for positive numbers, but differently for negative numbers. Here's an example from the docs:
(mod -10 3) ; => 2
(rem -10 3) ; => -1
Update:
If you really want to convert your code to Clojure, you need to realize that an idiomatic Clojure solution probably won't look anything like your JavaScript solution. Here's a nice solution that I think does roughly what you want:
(defn change [amount]
(zipmap [:quarters :dimes :nickels :pennies]
(reduce (fn [acc x]
(let [amt (peek acc)]
(conj (pop acc)
(quot amt x)
(rem amt x))))
[amount] [25 10 5])))
(change 142)
; => {:pennies 2, :nickels 1, :dimes 1, :quarters 5}
You can look up any of the functions you don't recognize on ClojureDocs. If you just don't understand the style, then you probably need some more experience programming with higher-order functions. I think 4Clojure is a good place to start.

Related

Sum of the first 200 numbers in Clojure?

I'm trying to write a block of Clojure code that will calculate the sum of the first 200 integers. The answer I'm trying to get as output is 20100. Here is my closest attempt.
(reduce + (range 200))
The above code outputs: 19900.
Can someone explain where I'm going wrong here and how to go about solving this?
From range's doc:
Returns a lazy seq of nums from start (inclusive) to end
(exclusive)
BTW, unless you HAVE to use reduce, it would be better to use Gauss's formula:
(defn sum-first-n [n]
(/ (* n (inc n)) 2))
Proof:
user=> (defn sum-first-n [n]
#_=> (/ (* n (inc n)) 2))
#'user/sum-first-n
user=> (sum-first-n 200)
20100
Other users have given 2 great solutions to your problem, however I'm not sure they answered "how to go about solving this?".
In a traditional language I'd fire up a debugger and inspect the collection I'm reducing over. In Clojure it's much easier. I'm at the REPL so I can examine things directly.
;; I always forget how range works after 10 years hacking clj
(reduce + (range 200))
;; => 19900
;; that's not what I expect
(last (range 200))
;; => 199
;; aha!
(last (range 201))
;; => 200
(reduce + (range 201))
;; => 20100
The power of this can't be overestimated. For things which take more effort than generating a range of numbers, you might store the data in a var to inspect it.
(def x (unnecessary-webservice/range 200))
;; => #'user/x
(reduce + x)
;; => 19900
(last x)
;; => 199
;; etc
(range 200) generates a sequence from 0 to 199. To get the Sum of integers from 0 to 200, it would be
(reduce + (range 201))

What would be the functional way of getting the mean of each vector in Clojure?

I am a beginner to functional programming and Clojure programming language and I'm resorting to recur pretty much for everything. I have a dataset in csv, imported as a map. I have extracted the info I need to use as vectors. Each column is a vector [1 5 10 8 3 2 1 ...] and I want to calculate the mean of each column. I wrote the following function:
(defn mean
"Calculate the mean for each column"
([columns]
(mean columns []))
([columns
means]
(if (empty? columns)
means
(recur (rest columns)
(conj means (float (/ (reduce + (first columns)) (count (first columns)))))))))
;; Calcule the mean for the following vectors
(mean [[1 2 3] [1 2 3] [1 2 3]])
; => [2.0 2.0 2.0]
Is this a functional way of solving this problem?
I'd break it down a little farther and use map instead of for. I personally like having many, smaller functions:
(defn mean [row]
(/ (apply + row) (count row)))
(defn mean-rows [rows]
(map mean rows))
But this is the same general idea as #Alan's answer.
The way you're doing it is already considered "functional". It should be said though that while using recur is good, usually you can achieve what you need more easily using reduce, or at least map. These options eliminate the need for explicit recursion, and generally lead to simpler, easier to understand code.
Here is a simple answer:
(defn mean
"Calculate the mean of each column"
[cols]
(vec ; this is optional if you don't want it
(for [col cols]
(/ ; ratio of num/denom
(reduce + col) ; calculates the numerator
(count col))))) ; the number of items
(mean [[1 2 3] [1 2 3] [1 2 3]]) => [2 2 2]
If you haven't seen it yet, you can get started here: https://www.braveclojure.com/
I recommend buying the printed version of the book, as it has more than the online version.

Concise syntax for partial in Clojure

Learning Haskell some time ago, I felt in love with pointfree notation and especially convenient partial function application - just supply args you know. In Clojure, I have partial all the time. I think having a special syntax for partial in reader will be nice to have.
Look at the sample code:
; Notation with points:
(map (+ 10 (* % 2)) [1 2 3])
; With partial:
(map (comp (partial + 10) (partial * 2)) [1 2 3])
; Let #[] syntax means partial application in reader:
(map (comp #[+ 10] #[* 2]) [1 2 3])
This is so nice! Is there something like this? Is there possibility to define custom reader macro?
An anonymous function syntax #(...) can be used similarly to what you are trying to do:
user=> (map (comp (partial + 10) (partial * 2)) [1 2 3])
(12 14 16)
is equivalent to:
user=> (map (comp #(+ 10 %) #(* 2 %)) [1 2 3])
(12 14 16)
A tiny little difference is % which just means a function argument, in this case the first and only.
I really like your idea for a partial function notation using the #[ ] literal.
Unfortunately, Clojure does not let us enhance the #() notation directly, but we can define ourselves a macro like #p for a partial application.
Given you have a function
(defn partial-wrap
[args]
(concat '(partial) args))
defined in myapp.core.
You can add the following entry into the data_readers.clj at the top of your classpath:
{p myapp.core/partial-wrap}
(usually one should use a namespace qualified symbol here like a/p, since unqualified symbols are reserved for Clojure. Nevertheless unqualified symbols do work, you need to rely on Clojure not overwriting them in a future version).
This finally allows to do almost what you asked for:
(map (comp #p[+ 10] #p[* 2]) [1 2 3])
=> (12 14 16)
I found an approach to have partial in cases like in my question: (map #(+ 10 (* 2 %)) [1 2 3). Use ->> macro: (map #(->> % (* 2) (+ 10)) [1 2 3]. Similar macros are ->, .. and doto applicable in different situations.
If you write code in vim, place the following code in your equivalent of ~/.vim/after/syntax/clojure.vim, and turn conceal on by setting conceallevel to 2:
syntax keyword clojureFunc partial conceal cchar=$
This shortens partial on screen for the reader and writer but doesn’t make a mess of things for others.
I use this trick for making shorthand notations in most languages (e.g., the anonymous function keyword—fn, lambda, whatever—is concealed to the greek letter lambda in many languages).

In clojure, how does one thrush through a list of functions?

In clojure, I would like to push a thrush value through a list of functions, but I'm not sure how to do so in an idiomatic way. The idea is that I will have a list containing an unknown number of functions, and I'd like to take advantage of the variadic nature of thrush.
So, something like this...
(->> 1 inc inc inc)
; 4
(->> 1 '(inc inc inc))
; does not work, of course
reduce does this nicely in this context
user> (reduce #(%2 %1) 1 [inc inc inc])
4
user> (defn thrush [val funs] (reduce #(%2 %1) val funs))
#'user/thrush
user> (thrush 1 [inc inc inc dec])
3
When possible sitck to functions over macros for such things
I think comp is the most idiomatic option here, though it will have prefix syntax instead. This is also more in keeping with normal Clojure (fn args) notation.
=> ((comp inc inc inc) 1)
4
Combine it with the idiomatic apply if a function normally takes variadic arguments, but you want to feed it a collection.
=> ((apply comp (repeat 3 inc)) 1)
4
Be aware though it threads from right to left
=> ((comp str inc inc inc) 1)
"4"
=> ((comp inc inc inc str ) 1)
ClassCastException java.lang.String cannot be cast to java.lang.Number
This also complies more with Clojure/Lisp s-expressions.
If you want more 'easy' human readable notation, Arthur Ulfeldt's answer is perfecly acceptable, and a nice example of reduce and functional programming. Using it 'as is' might get in the way of getting acquainted with the 'simplicity' of s-expressions though..
Be careful with macro's !
The reason why -> and ->> are macro's is that they actively rewrite forms, so you can use normally incomplete argument notation like (filter odd?) in their scope without having to resort to overuse of partial. This can't be done with normal function compostition.
Best learn to make the most of normal function composition before turning to macro's. There's a lot of pitfalls in them for the unaccustomed, and should be used sparingly.
How about:
user=> (def my-fns [inc inc inc])
#'user/my-fns
user=> (->> 1 ((apply comp my-fns)))
4
I suppose you could do this:
(defmacro thrush-list [x flist] `(->> ~x ~#flist))
(thrush-list 1 [inc inc inc])
; 4

How To Define Function From Closure

This question is related to one I asked recently.
If I rewrite (fn [n] (fn [x] (apply * (repeat n x)))) as
(defn power [n] (fn [x] (apply * (repeat n x))))`
it works just fine when used like this
((power 2) 16)
I can substitute 2 with another power, but I'd like to make a function just for squares, cubed, and so on. What is the best way to do that? I've been fiddling with it in the REPL, but no luck so far.
Using a macro for this goes entirely around his question, which was "I have a function that generates closures, how do I give those closures names?" The simple solution is:
(letfn [(power [n]
(fn [x]
(apply * (repeat n x))))]
(def square (power 2))
(def cube (power 3)))
If you really truly hate repeating def and power a few times, then and only then is it time to get macros involved. But the amount of effort you'll spend on even the simplest macro will be wasted unless you're defining functions up to at least the tenth power, compared to the simplicity of doing it with functions.
Not quite sure if this is what you're searching for, but macro templates might be it. Here's how I would write your code:
(use 'clojure.template)
(do-template [name n]
(defn name [x] (apply * (repeat n x)))
square 2
cube 3)
user> (cube 3)
;=> 27
For more complex type of similar tasks, you could write a macro that wrap do-template to perform some transformation on its arguments, e.g.:
(defmacro def-powers-of [& ns]
(let [->name #(->> % (str "power") symbol)]
`(do-template [~'name ~'n]
(defn ~'name [~'x] (apply * (repeat ~'n ~'x)))
~#(->> ns
(map #(vector (->name %) %))
flatten))))
(def-powers-of 2 3 4 5)
user> (power3 3)
;=> 27
user> (power5 3)
;=> 243
P.S.: That macro might look awkward if you're just starting with Clojure though, don't give up because of it! ;-)