Fork me on GitHub

Question: What's the best way in Clojure to make a namespace use configurable. Mostly, I have a function that acquires a java object, and I'd like to be able to like override that function, but from another namespace. Should I put the function in an Atom, or can I just re-def the function from the other namespace?


Does anyone here use light table? I'm having issues with the instarepl


I created a default play-clj project on the command line using lein new play-clj hello-world


then brought that into light table


but my instarepl doesn't recognize the namesplaces of the project


for instance, it doesn't recognize hello-world.core.desktop-launcher


@didibus: the clean way to do it is to just pass everything in as arguments; the semidirty way to do it is with a dynamic var


@gfredericks Ya, I think a dynamic var in my case might be better. But I'm experimenting with using (intern). My use case doesn't need many different override, just a single universal one.


what would you do with intern?


I have a (get-java-object_ function in the namespace, which other functions in the namespace use to get that particular java object. So I'm thinking I could call (intern 'ns 'get-java-object (fn [] "alternate getter"))


To actually re-def the function itself


to a different implementation


alter-var-root is a more straightforward way to change a var's value


but that's a global change, so that can be weird if your program is multithreaded


thus dynamic vars


Hum, what would the threads have issue with? In my use case, I need it to be global, like I won't want different caller to each have their own different getter. So I need to override it for the whole application


okay, I'd use alter-var-root then


you could also set the var to the object directly instead of a function that returns it


That's true, but its some weird java object that does a bunch of side effect which fail on build, so if I have it as a var directly, it tries to instantiate the java object at build time and fails my build, because the object is looking for a config file


gotdang java objects


Never liked them, never will, hehe


Huh. clojure.string/join is [separator coll], but clojure.string/split is [string pattern].


Reminds me of constantly looking up the argument order of similar PHP functions.


collections are often the last arg


having written clojure since before the clojure.string namespace existed, I conveniently avoid this problem by using the .split method on String, and interpose and apply str instead of the functions from clojure.string


When using the ring wrap-reload middleware, it is possible to get notified when a reload occurs?


@amacdougall clojure.string/join is [coll] or [separator coll], which is why @gfredericks said collections usually come last... whereas clojure.string/split is a function on a string (with a regex and an optional limit as additional arguments). It's more consistent than you might initially think.


@seancorfield one could argue that a string is a collection of chars...

Alex Miller (Clojure team)07:08:02

@amacdougall if you read the clojure.string docstring you’ll see that the string in most functions is treated as a collection and thus is the first argument. join is an intentional exception to that to be more amenable to use with partial


@alexmiller: on a semi-irregular basis I start wondering about the order of arguments to functions in core, and I guess this is such a moment. Normally, I like to think that one passes the thing to be acted upon as the first argument, much like this often is an implicit first argument in OO languages, explicit in eg Perl. But map, filter, and reduce don’t follow this pattern. Is there any reason for this?

Alex Miller (Clojure team)07:08:53

functions that work on data structures take the data structure first (get, assoc, conj, merge, etc)

Alex Miller (Clojure team)07:08:04

and work well in chains of ->

Alex Miller (Clojure team)07:08:22

functions that work on sequences take the sequence last and work well with ->>

Alex Miller (Clojure team)07:08:52

map, filter, and reduce are sequence functions so take the sequence last

Alex Miller (Clojure team)07:08:34

with collections or other data structures the idea is that you invoking a function to act upon that structure so it comes first (like an object in Java)

Alex Miller (Clojure team)07:08:26

with sequences you are chaining lazy calls and building a nested pipeline so the sequence goes last

Alex Miller (Clojure team)07:08:10

clojure.string/join is a rare exception to these rules

Alex Miller (Clojure team)07:08:19

nth is kind of a weird case too as it works on both colls and seqs


But, if I have a pipeline of things working on a datastructure and then on collections, something like (contrived) (-> {:foo :bar} (assoc :baz :qix) (group-by key)) (which doesn’t work, since group-by expects the sequence to go last), am I doing something I shouldn’t?


You can do (-> {:foo :bar} (assoc :baz :qix) (->> (group-by key)))


Can I have an ordered map? All my attempts fail. Where br is a map

(into {} (sort-by first (br)))


the into unorders it again


@iku000888 yes, but it’s starting to dawn upon me that I’m working at different levels of something in the same thing


but (sort-by first (br)) gives me an ordered list of vectors


@urbanslug (sorted-map (sort-by first (br))


I wonder whether I can concat into a map


slipset Thanks


@slipset I guess I got too used to doing it and stopped thinking rich


Maybe silly question but can I pipe the result of a side-effecty function like pprint into a file? trying (spit “file.txt” (pprint …)) but it obviously writes nil.


truncates file to zero


@urbanslug could you rebind *out* to an outputstream that you control?


I guess you’d need to use something else than spit like

Rachel Westmacott08:08:05

(spit “file.txt” (with-out-str (pprint …)))?


Thanks guy(s)/girl(s)


what is the best way to do this in clojure: find <dir> -type d ?


return all depth dirs from <dir>


@abdullahibra something like (->> (file-seq <dir>) (filter #(.isDirectory %)))


Is anybody able to help me with a clojure.spec issue I’m having?


@mschmele You might want to try #clojure-spec Also "don't ask to ask, just ask"


ooh, thank you!


Is $& legal Clojure? I find references to it on SO and in a 4Clojure solution, but not in any documentation I can search.


you mean %& ?


That’d certainly make sense .. I wonder if those were typos.


Hi, does anyone heard about similar lib for Clojure ?


…used in for e.g when dealing with unreliable services by setting retry policy, so when call to ws fails it gets repeated etc


Hi folks.. what are some options for building microservices in clojure other than hystrix and finagle?


@shuaibiyy …diehard as mentioned by @dorab, circuit breaker is a important imho


@shuaibiyy it’s not purely for ‘building’ ms


never looked into diehard (though I’m going to now that I’ve heard of it), always used to do circuitbreakers around microservices


how do I retrieve a value of specific key from multiple maps? Why this is not working?

(def mp1 [{:foo 1} {:foo 2}])

(def mp2 [{:foo 3} {:foo 4}])

((juxt (partial map :foo)) mp1 mp2)


(map :foo [mp1 mp2]) ?


(nil nil)


mp1 is a vector of two maps is the reason


(mapcat (partial map :foo) [mp1 mp2])


so that’s why I need I need to map trough both vectors and grab :foo from every map


jr's got it it seems. You have to "descend" into a sequence twice


@ag you could also do (map :foo (concat mp1 mp2))


now I wonder which is slower


@ag you could check that with criterium


why not clojure.core/time?


if you want just a quick comparison you could that


but then if performance is not that important I would just pick option 2 because it looks cleaner


my guess would be the version without concat


benchmarking, however, is tricky, specially because the JVM does a bunch of optimizations as code runs


(and criterium takes care of that)


If I understand core.async correctly, then it's impossible to define go-loop that can be closed or killed after it is started?


@hlolli you have to roll your own way of controlling that yes, for instance when you receive a nil on the channel, which means the channel is closed, you don't call recur


ah makes sense, thanks @richiardiandrea


Could I make a controller like that in core.async. So on every recursion, take a boolean value from channel, if one exists, and proceed to recur if the bool returns true. It would mean many blocking takes I guess, and would quickly fill the que if Im not feeding the channel as quickly a bool as the channel wants to read, hope my question makes sense.


(go-loop [] (when (<! ch) (recur)))


ok, this makes me reconsider how Im solveing my problem. In my case (which is probably bad design), I have a (metronome) clock (go-loop [cnt 0] (when (not= (clock-value-from-external-app) cnt) (recur (clock-value-from-external-app))) something like that. Well, I could always use clojure.core loop and send it into a go loop and from there make callbacks in core.async land. Just thinking out loud.

Chris O’Donnell21:08:36

@hlolli: could you use clojure.core.async/timeout?


maybe 🙂 I've only used core.async very little, if I knew more low-level about it I would be happy. Wouldn't timeout create quickly thousands of waiting background threads?


what are you trying to achieve?


@codonnell this is a possible solution, what I'm trying to achive, (if I want to achieve that). I better explain it with a sample that fails, but maybe you see if my "heart" is at the right place:

(def engine-start? (atom false))
  (when (and (true? @engine-start?)
                            (new-clock-value... elided...))
(reset! engine-start? true)
your short snippet @jr solves it, or never has this problem in the firstplace. But I'd be curious to know if this engine-start? can be a value from a channel rather than atom (which doesnt work, except if @engine-start? returns true initially).

Chris O’Donnell21:08:52

@hlolli: what does new-clock-value do?


to be perfectly percise, it returns 0 if a audio signal was iterated (44,1k per second), and needs to be in a recursive loop to iterate trough audio files/signals.

Chris O’Donnell21:08:46

so do you just want to start reading once the engine has started?


yes, and stop, on command.

Chris O’Donnell21:08:42

I edited my snippet above

Chris O’Donnell21:08:29

engine-start is a channel, and once it received a message, you're ready to start processing. Also, clock-chan is a channel receiving clock values. f-chan is a channel, and once it receives a message you will stop processing.


ok, thanks for now, I will need to give this a try, cant see in this code how the loop can be terminated after it is started.

Chris O’Donnell21:08:46

alts!! reads from the first channel which receives message, clock-chan or f-chan. If clock-chan receives a message, the when condition is satisfied and recur is called. If f-chan receives a message, the when condition is not satisfied and it will drop out of the loop. Does that make sense?


yes, it does, I think this will be very clear when I start coding. This is the behaviour Im looking for in the end.

Chris O’Donnell21:08:20

I found tim baldridge's core.async videos to be a huge help in understanding how core.async can and should be used. They're a bit out of date (no transducers), but most of the insight is still very useful.


Yes core async seems to want different parts of the brain. I find atoms, agents, refs much easier to understand, even tough it could be that core.async is evens simpler, just new for me.


I made a quine that doesn't eval to itself but eval'd 1000 times it does:


I have a clojure gen-class, and I'm wirting a clojure test that tries to call it as a java method, its defined static, so: (com.bla.bla/myStaticGenMethod) but I always get NoClassDef


gen-class is so weird that it's worth asking why you need to use it :)