This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-08-31
Channels
- # architecture (1)
- # aws (23)
- # beginners (13)
- # boot (18)
- # cider (5)
- # clara (1)
- # cljs-dev (22)
- # cljsjs (9)
- # cljsrn (28)
- # clojure (120)
- # clojure-canada (12)
- # clojure-dev (6)
- # clojure-italy (4)
- # clojure-korea (1)
- # clojure-russia (18)
- # clojure-sg (8)
- # clojure-spec (45)
- # clojure-uk (12)
- # clojurescript (240)
- # component (4)
- # cursive (17)
- # datomic (91)
- # editors-rus (4)
- # figwheel (2)
- # flambo (6)
- # hoplon (163)
- # instaparse (6)
- # jobs (1)
- # leiningen (2)
- # luminus (5)
- # om (22)
- # om-next (2)
- # onyx (35)
- # perun (15)
- # play-clj (1)
- # protorepl (4)
- # re-frame (106)
- # reagent (4)
- # ring (106)
- # schema (1)
- # spacemacs (17)
- # untangled (40)
- # yada (14)
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?
I created a default play-clj project on the command line using lein new play-clj hello-world
@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"))
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
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...
@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?
functions that work on data structures take the data structure first (get, assoc, conj, merge, etc)
and work well in chains of ->
functions that work on sequences take the sequence last and work well with ->>
map, filter, and reduce are sequence functions so take the sequence last
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)
with sequences you are chaining lazy calls and building a nested pipeline so the sequence goes last
clojure.string/join is a rare exception to these rules
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?
Can I have an ordered map? All my attempts fail.
Where br
is a map
(into {} (sort-by first (br)))
@iku000888 yes, but it’s starting to dawn upon me that I’m working at different levels of something in the same thing
@urbanslug (sorted-map (sort-by first (br))
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.
Can’t figure this out https://clojuredocs.org/clojure.pprint/write
@urbanslug could you rebind *out*
to an outputstream that you control?
I guess you’d need to use something else than spit
like https://clojuredocs.org/clojure.java.io/writer
(spit “file.txt” (with-out-str (pprint …)))
?
hi guys
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 $& legal Clojure? I find references to it on SO and in a 4Clojure solution, but not in any documentation I can search.
Hi, does anyone heard about similar lib for Clojure https://github.com/App-vNext/Polly ?
…used in for e.g when dealing with unreliable services by setting retry policy, so when call to ws fails it gets repeated etc
@kosecki Might want to look at https://github.com/sunng87/diehard
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 https://github.com/josephwilk/circuit-breaker 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)
but then if performance is not that important I would just pick option 2 because it looks cleaner
benchmarking, however, is tricky, specially because the JVM does a bunch of optimizations as code runs
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.
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.
@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?
@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))
(go-loop[]
(when (and (true? @engine-start?)
(new-clock-value... elided...))
(recur))
(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).@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.
so do you just want to start reading once the engine has started?
I edited my snippet above
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.
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.
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: https://gist.github.com/gfredericks/2369143507f032f4190d37e0185e9908
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 :)