Fork me on GitHub
Bert Weidenflunders02:12:57

hey all, I'm still trying to get the cljs version of my factorial function to work for big numbers

Bert Weidenflunders02:12:19

(defn- factorial-helper [n result]
  (if (= n 1)
    (recur (dec n) (* n result))))

(defn factorial [n] 
  (factorial-helper n 1))

Bert Weidenflunders02:12:56

when I try with large numbers it returns "Infinity"

Bert Weidenflunders02:12:26

someone mentioned using a js or cljs library that keeps the exact digits of the number, but I'm having trouble implementing it.

Bert Weidenflunders04:12:55

I tried like this:

(defn- factorial-helper [n result]
  (if (= n 1)
    (recur (e/dec n) (e/* n result))))

(defn factorial [n] 
  (factorial-helper n 1))

Bert Weidenflunders04:12:29

(:require [com.gfredericks.exact :as e]))

Bert Weidenflunders04:12:46

and just calling it like this: (factorial 10000)


@info Im not sure but does writing it like this help?

(def ONE (e/native->integer 1))

(defn- factorial-helper [n result]
  (if (e/= n ONE)
    (recur (e/dec n) (e/* n result))))

(defn factorial [n]
  (factorial-helper n ONE))

(factorial (e/native->integer 10000))


Curious about this. At my work we use Java, and DDD is something they are quite religious about. One pattern that I keep seeing is the use of converters. Some new class implements Converter<A,B>, which then implements the function: public B convert(A a) So I wanted to see what this would look like in clojure… I have something like this:

(defn defconverter [map-for-conversion defaultfn]
  (fn [key]
    (if (contains? map-for-conversion key)
        (get map-for-conversion key)
        (defaultfn key))))

(def a-to-b
    {:a 1
     :b 2
     :c 3}
    (fn [key] (throw (IllegalArgumentException. (str "Could not find " key))))))

(a-to-b :a) => 1
(a-to-b "asd") => IllegalArgumentException Could not find asd
I was wondering what I would have to do to be able to define a converter in the following way:
(defconverter a-to-b
  {:a 1
   :b 2
   :c 3}
  (fn [key] (throw (IllegalArgumentException. (str "Could not find " key)))))
My guess is that I would have to use macros? What would that look like?


Figured it out 🙂

(defmacro defconverter [fn-name map-for-conversion defaultfn]
    'def fn-name (fn [key]
                    (if (contains? map-for-conversion key)
                        (get map-for-conversion key)
                        (eval (list defaultfn key))))))


eval inside a macro is usually wrong


@noisesmith Before that i tried without the eval and without the list on the last line and it would not work. It was complaining... but this works as expected. Also just saw the case function may be suitable for this scenario as well. Can you explain why eval in macro is usually wrong?


it’s spinning up a whole new compilation context in order to compile one line inside another compilation that’s already running


and it’s not quoted, so it’s not running the compiler at runtime - which means what you are doing there should be possible without needing eval


@noisesmith here’s the output if I drop the eval and list:

(defmacro defconverter [fn-name map-for-conversion defaultfn]
    'def fn-name (fn [key]
                    (if (contains? map-for-conversion key)
                        (get map-for-conversion key)
                        (defaultfn key)))))

(defconverter a-to-b
  {:a 1
   :b 2
   :c 3}
  (fn [key] (throw (IllegalArgumentException. (str "Could not find " key)))))

(a-to-b :a) => 1
(a-to-b :d) =>
ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn  user/defconverter/fn--10571 (form-init8994047963818758313.clj:27)
Any suggestions on how to get it to work without eval?


in general, ` makes writing macros much easier


anyway, with your current approach, turn (fn ...) to (list 'fn ...) and ensure that only things that the macro is meant to evaluate are unquoted; this gets very tedious and that’s why ` makes things easier


anyone has a clojure project that uses any google api java library?


I want to know how can I integrate them in clojure project, for example this one


Hi, I have been interested in Clojure for years and have been reading about it on and off but never really wrote any code. (I am an experienced Python programmer though.) Now I am trying my hand at actually writing some Clojure code in the the Halite competition ( which seems like a fun way to experiment with various aspects of the language. I found that there is plenty of Clojure documentation (books, blog posts, web sites etc) on the basics but it's harder for me to find higher-level information on how to architect things. A specific example I am wondering about is how to write a function that produces the best possible answer to a question within a time limit of 2 seconds (which is what Halite robots need to do). I am assuming that conceptually the answer is to break it into two functions: the first one just produces (without regard to wall clock time elapsed) a sequence of answers which get better and better; and a second function which just watches this sequence for 1.99 seconds then returns the best answer seen so far and terminates the efforts of the first function. However, I am not sure how to put this together in Clojure. Channels? Threads? Agents? How do I implement a timeout? Any hints (even if vague) would be greatly appreciated! (I do feel capable of following up on a thread, it's just hard to find the right starting point.) Thank you very much!

New To Clojure14:12:16

@sabbatical2017 I would start from

(def *values* (atom []))
(def process (future
               (while true
                 (comment generate and add values to *values* collection))))

(deref process 1990 (last @*values*))


It’s an example of using timeout in the context of core.async channels.


Hi, anyone successfully set up lein-droid? Could not find template droid on the classpath.


sorry, took this to #clojure-android


Thank you very much, @ghsgd2 and @manutter51!


Good morning. I am struggling understanding the meaning of % in some functions in clojure for instance for swap! function where records is a map and id is a value of the :id, m is one of the key/value that I want to update the defn looks as follow: (defn update [id m] (swap! records #(if (% id) (update-in % [id] merge m)%))) - can someone read this function for me, especially the %. Thanks in advance


#(...) is an anonymous function literal. Within that form, % stands for the first argument to the function.


There’s also %2 for the second arg, %3 for the third, etc.


Does anyone know why leiningen seems to be trying to use localhost to hit clojars and the maven repos?


Though in general if you have more than a couple args you should write out (fn [arg1 arg2 arg3...] ...)


Thanks - so since I don't have %1,, % assumes that I only have one argument? Going further on the small function I have an if statement - that evaluates % with id. What is % evaluate to. Is it something to do with the "records" atom that I am trying to swap!.


I am just trying to read the whole statement like someone that really knows clojure would do. I am kind of new to the syntax and the concepts.


(defn update [id m] (swap! records #(if (% id) (update-in % [id] merge m)%)))


So… records must be a var containing atom. This update fn will, if the records atom contains a map with an entry for the given id, merge the entry’s value with the given map m, otherwise it will leave it alone.


There are some confusing things about this form though. I tend to avoid the anonymous fn literal, well all the time actually, but particularly when the arg appears more than once.


I remember seeing somewhere that there is a way to get automatic feedback about Clojure source code being idiomatic. For example (generalizing from my experience with Python) it probably is not idiomatic to say "(= foo true)" because you can say "foo" instead (perhaps this is not a good example because of how truthiness works in Clojure, but I can't think of a better one now.) Perhaps this checking of being idiomatic also has to do with spec somehow, I don't remember. So I vaguely recall that there is a script that looks at your source code and spits out diagnostics on how you could make it more idiomatic. Unfortunately, I was not able to Google my way to this, can anyone give a hint? Thank you!


Suppose i have a sequence of integers that i want to filter by a relation of each other. I.e. only keep members that are equals to the next one. What would be the idiomatic way to code this? A loop-recur would be easiest for me at the moment but perhaps keep-index/filter/reduce is better?


Thank you very much, @tbaldridge and @ghsgd2!


(partition-by identity [1 2 3 3 4])
=> ((1) (2) (3 3) (4))


that one’s fun too


(filter #(<= 2 (count %))
        (partition-by identity [1 2 2 3 4 4 4 5 6]))
=> ((2 2) (4 4 4))


also, with take-nth, so (= (take-nth 2 seq) (take-nth 2 (rest seq)))


oh u want the items as well, so filter would be the way


Thanks @pcbalodi @manutter51 i feel like i should be able to solve the issue now =)


Thanks Donald. I don't like the syntax, but I can convert it now to one that I can understand better. Thanks for taking time.

New To Clojure17:12:31

How to handle sequential updates of variables when each pair of values depends on previous pair differently? For example, could write

(let [x0 (...) y0 (...)]
    (let [(x1 (/ y0 (...)) y1 (* x0 (...))]
        (str x1 y1)))
or make each let [x y] a separate function or
(->> [a b]
    ((fn [[x y]] [(...) (...)])) 
    ((fn [[x y]] [(...) (...)])))
What's the best option?


let allows back references, you don’t need nested let


(let [x0 … y0 … x1 (/ y0 …) y1 (* x0 …)] (str x1 y1))

New To Clojure17:12:01

what if I don't need x0 and y0 after x1 and y1 are calculated? could I delete those bindings (variables) ?

New To Clojure17:12:13

to avoid their usage in let body


no, clojure is smart enough to know they are out of scope


it’s called “locals clearing” - in fact you need to turn it off explicitly to use debuggers


otherwise you try to refer to a value and find it no longer exists


(in the debugger)

New To Clojure17:12:59

i. e.

(let [x0 () y0 () x1 () y1 ()] 
 (comment body))

New To Clojure17:12:07

body can see x0 and y0


yes - but clojure knows whether it uses them


because we don’t have implicit scope


so either those symbols are in body or they aren’t


the compiler is good at clearing those locals

New To Clojure18:12:39

@noisesmith I mean that if I accidentally used x0 instead of x1 in let body there will be no compiler error about that


sure, nested let doesn’t help with that


it would have identical behavior


perhaps you want to rebind the same name?

New To Clojure18:12:57

@noisesmith I my case, each pair of values depends on previous pair i. e. [x0 y0] [x1(x0, y1) y1(x0 y1)] so I can't rebind x to x and lose previous value before calculating y.

New To Clojure18:12:08

But let is good enough for my purposes

New To Clojure18:12:23

Thanks, @noisesmith, for taking your time


I'm trying to understand ring-middleware better. When I write a wrap-something middleware, my understanding is that it fires once for the request, then again for the response. Is there a way to set things up so that it only does something on request or response and not the other? Or to deal with them differently?


it’s just a higher order function - it’s up to the author whether you do anything before or after the original


eg (defn wrap-foo [handler] (fn [request] (f (handler (g request)))) - either f or g could be identity


if g is identity, you only change the return value of the handler, if f is identity you only change the input, if both are identity you have the identity middleware


and it doesn’t fire more than once - there’s two places it can touch the data


@noisesmith: Thanks, that helps quite a bit!

Miķelis Vindavs20:12:53

@ghsgd2 you can rebind on the same names, since each line is evaluated at a time

(let [[x y] [1 2]
      [x y] [(+ x y) (- y x)]
      [x y] [(+ x y) (- y x)]]
  [x y])
=> [4 -2]

Miķelis Vindavs20:12:21

what i mean is — the right hand side gets evaluated first and then both rebinds happen at the same time

Miķelis Vindavs20:12:06

this also allows you to eg swap bindings

(let [[x y] [1 2]
      [x y] [y x]]
  [x y])
=> [2 1]

New To Clojure20:12:32

That's exactly what I was searching for. Thank you very much, @mikelis.vindavs!


I am trying to call a function while rebinding stdout to /dev/null. I was hoping that something like (binding [*out* (output-stream "/dev/null")] ...) would do the trick, but this fails with cannot be cast to Is there a standard or idiomatic way to discard *out*?


[The context to the question above is that I am trying to start a nightlight server in my app, but the standard nightlight.core/start will do a println to let me know that the server is up. However, that output showing up in the stdout screws up the other command that is consuming the stdout of my clojure app.]


the error is telling you it needs a writer

New To Clojure21:12:48

@sabbatical2017 That's not an exact answer but you could use with-out-str and ignore it's return value


user=> (io/writer (io/output-stream "/dev/null"))
#object[ 0x1c3c1eeb ""]
user=> (binding [*out* *1] (println "OK"))


all you need is to make a writer from the output-stream and it works


oh you can use io/writer directly on the file name, no need to create output-stream explicitly


@noisesmith, sorry for such a newbie question, but how do I avoid No such var: io/writer? I have to know what io is and mention it in my ns macro at the top of the file, right?


oh - I used (ns … (:require [ :as io]) ) - I figured you’d know how to find it since you found output-stream which is in the same ns


Thank you very much, @ghsgd2, @noisesmith, @admay! This is now working for me


it’s also possible without using “/dev/null” by creating a proxy of Writer

user=> (binding [*out* (proxy [] [] (append [c]) (flush []) (close []) (write [c]))] (println "OK") (println "yeah"))


all it needs to do is let something call a method and return nil