Fork me on GitHub

For those who use Docker, any reason to use java image instead of clojure image ?


I've found making an uberjar (either on my machine or a build server) and then deploying as if it was java keeps things simple


I was thinking of a workflow including ansible & docker. Maybe it's better to just stick to ansible

Ryan Radomski00:11:11

Is docker more popular right now for production Clojure apps?


I saw some snippets to build & deploy with docker, but it seems a bit old and use java image instead of clojure one

Ryan Radomski00:11:46

I'm about to deploy for the first time, what is the "safe" bet?


the simplest thing is to make an uberjar, make sure there's a jvm on your server, and run it

Ryan Radomski00:11:08

I was thinking that docker container networking might be a better fit for me because I want a local database connection. How do Clojarians typically handle that sort of situation?


I've used dedicated database servers, so can't offer anything on that


though with mongo, we do have a mongo server process for sharded dbs, that's set up by chef in our case


(I think using ansible would be very similar)

Ryan Radomski01:11:00

This seems to confirm my suspicion that the benefits of solo UberJar is that simple deployment is easy, but doing anything non-trivial becomes a nightmare and the benefits of containers being that it can manage high amounts of deployment complexity, but is pretty heavy and frustrating to set up. Thanks for the resources, I'll have to ponder more what fits for my use case


"nightmare" is not my experience

Ryan Radomski01:11:27

That's good to hear! Does the box running your UberJar have other dependencies that lein doesn't support?

Ryan Radomski01:11:57

I know you mentioned mongo, but I imagine you can keep the config inside of your .clj


it has a few local deps that are set up by chef (I am not the one managing ops / chef stuff though)


there's a mongo program that you need to run, it's not a client thing


(for sharded dbs)


basically it's a mongo server running on localhost that interacts with the sharded system, because the client can't use it directly

Ryan Radomski01:11:54

I'll have to consider the UberJar setup more seriously considering your positive experience. All of my professional dev is done on 'Nix boxes, and I'm a huge 'nix buff so I normally wouldn't shy away so much, but I have a few other portability considerations this time around.


yeah, even just the differences between OSX and Linux are a pain, and I see how a container helps there


Two startups in a row we’ve deployed services in docker and never regretted it. “Pretty heavy” and “frustrating” have not been our experience.


but an uberjar is an uberjar, and it really does work

Ryan Radomski01:11:51

Thanks @gonewest818, I haven't heard of many people regretting dockerizing. I didn't mean to use such bold words! 🙂 I'm just trying to view my options for their upsides and their downsides.

Ryan Radomski01:11:46

I really don't think either option will cause too many problems. I'm thinking docker would give me a bit of the agility I'm after

Ryan Radomski01:11:24

Thanks much for the answers


@gonewest818 did you use java or clojure image ? just curious 🙂


Custom images.


regardless of the solution you pick, I would like to advocate for having a separate build server or process, that isn't something run on your production server (eg. let circleci create your uberjar and upload it, and download it from your server process)


In our case we built the docker images too, and we tagged (w/ version numbers) and uploaded those to a local docker image repository.


Hi, If I want to find the index of the biggest element that is <= 5 , and the index of the biggest element that is <=8, in a sorted array. Can I get the two indices in one search in Clojure?


(defn tmp-search
  (let [v [1 2 3 4 5 6 7 8 9 10]]
    (loop [index1 9
           index2 9]
      (let [condition1 (<= (get v index1) 5)
            condition2 (<= (get v index1) 8)]
        (if (and condition1 condition2)
          [index1 index2]
           (if condition1 index1 (dec index1))
           (if condition2 index2 (dec index2))))))))


@cmal You could consider a one pass using reduce-kv

(reduce-kv (fn [[ndx1 ndx2] k v]
             (cond-> [ndx1 ndx2]
               (<= v 5) (assoc 0 k)
               (<= v 8) (assoc 1 k)))
  [nil nil]
  [1 2 3 4 5 6 7 8 9 10])


I suppose your approach is more efficient @cmal in that it traverses a part of the sequence from the end. You could force that into a single-pass reduce by using reduced to stop things. Thinking something like:

(take 2
  (reduce (fn [[ndx1 ndx2 ndx :as acc] v]
            (if (and ndx1 ndx2)
              (reduced acc)
              (cond-> [ndx1 ndx2 (dec ndx)]
                (and (nil? ndx1) (<= v 5)) (assoc 0 ndx)
                (and (nil? ndx2) (<= v 8)) (assoc 1 ndx))))
    [nil nil 9]
    (rseq [1 2 3 4 5 6 7 8 9 10])))


Would you recommend using on a project, or are there better FP librairies out there? (for simple functor/monad stuff)


I've only heard good things about it. I know that @U0524B4UW is a fan


cats is ok @hmaurer - it was the best of the clojure fp libs i tried. the context stuff is a bit awkward, and i end up writing sugar macros for (with context ... (mlet [...


they took out the monad transformer stuff - i had to resurrect it for one of my libs (uses a promise-state transformer)


and there is no reader or continuation or comonad stuff


but what is there is solid and straightforward - i’ve come across only a couple of bugs in 2 years of heavy use in both clj and cljs


and if you use either to model failure you might also want to take a look at


Actually, my only real use-case right now is using Either to model failure instead of throwing exceptions


Since these sorts of solutions slow the code down, I’ve never used the either in production. But what’s the driver to avoid the exceptions?


@U07TDTQNL I just thought it would be a neater way to differentiate between "expected" failure-like return values and actual exceptions


for example, I am implementing a unification algorithm. When unification fails it's not really an error, just a failure scenario that the caller will handle


a try/catch seemed a bit odd in that case


I could return a custom map with a success/failure tag, but in that case why not use functors and get chaining etc for free...


Well one reason is that they are opaque. That’s my biggest criticism against using functions instead of data, there are few ways to introspect them.


But either way the chaining isn’t free, since either method requires using non standard Clojure forms instead of let, do, etc


But I agree exceptions are not the answer for this.


@U07TDTQNL ah yes you are right, I was using the term "free" a bit too freely. I am open to better approaches!


This is something I've also been interested in, and has been one of the things I've felt myself wanting monads for.


@U09LZR36F have you ended up using this approach on any project?


Not yet, no.


cats is ok @hmaurer - it was the best of the clojure fp libs i tried. the context stuff is a bit awkward, and i end up writing sugar macros for (with context ... (mlet [...


is there a way to tell (for [[k v] someMap] ...) to process the elements in order sorted by 'k' ?


@qqq (for [[k v] (sort-by key someMap)] ...)


and, incidentally (sort someMap) will return key/value pairs in the same order as (sort-by key someMap) - when sorting collections it sorts by the size, then first item, then second item if first items sort equal


I guess technically sort-by key might be different if sort thought two of your keys were equal even though hash does not



+user=> (assoc {} [:a] 0 '(:a) 1)
{[:a] 1}


Is there a way to filter a collection with various rules? Fox example:

(def jobs [{:id "f26e890b-df8e-422e-a39c-7762aa0bac36",
            :type "type1",
            :urgent false}
           {:id "ed0e23ef-6c2b-430c-9b90-cd4f1ff74c88",
            :type "type2",
            :urgent true}
           {:id "690de6bc-163c-4345-bf6f-25dd0c58e864",
            :type "type3",
            :urgent false}
           {:id "c0033410-981c-428a-954a-35dec05ef1d2",
            :type "type2",
            :urgent false}])
I want to get the jobs with type 2, but considering this: return only urgent jobs if they exists. If not, return jobs with the same type that are not urgent. Just to avoid using lots of ifs… is there a way to do this with filter?


@tvalerio (or (seq (filter :urgent jobs)) ...)


oh, the type 2 part


(let [type2 (filter (comp #{"type2"} :type) jobs)] (or (seq (filter :urgent type2)) type2))


@noisesmith in this case type2 will have all the jobs right? Do I have to put the urgent part inside the let somehow?


type2 will have all the type2 jobs, (or (seq (filter ....)) ...) ensures that you look at the regular jobs if none of them are urgent


but otherwise you get the urgent ones only


I tried in my application and it works perfectly! thanks


What is wrong with this macro?

;; (comment java.lang.ClassCastException: clojure.lang.Symbol cannot be cast to clojure.lang.Keyword)

so the two 'small' examples work fine, but when I merge the maps, I get an error


(defmacro def-fsi [args]
     [email protected](for [[v ks] (sort-by key args)
             k      ks]
         `(clojure.spec.alpha/def ~k ~v))))

 '(def-fsi {number? [::a]}))
#_ (comment
     (do (clojure.spec.alpha/def :a.aa-util/a number?)))

 `(def-fsi {::foo [::cat]}))
#_ (comment
     (do (clojure.spec.alpha/def :a.aa-util/cat :a.aa-util/foo)))

    {number [::a]
     ::foo  [::cat]}))

Unandled java.lang.ClassCastException
   clojure.lang.Symbol cannot be cast to clojure.lang.Keyword


@qqq is it number that it doesn't like?


good cll; unfortunately

    {number? [::a]
     ::foo  [::cat]}))
also give same error


number? is also a symbol rather than a keyword


what I don't get is why does

;; works
 '(def-fsi {number? [::a]}))


{number? [::a]} and {::foo [::cat]} works, but when I merge the two maps -- BAM, error -- can't even macro expand


(sort-by key {'a 20 :fo 30}) -- totally your fault for suggesting (sort-by key ...) 🙂


so clojure's sort can't compare keyword vs symbol


you could provide a compare that knows how to


I actualy doh't care about the order so long as it's deterministic


maybe I'll just cast everything t strings


@qqq (fn [coll] (into [(type (first coll))] coll)) - that ensures that every item in the collection has an item of type Class as its first element


and the second elements would only be compared if the types of their first elements were the same, etc.


does that get around this issue here: (sort [[:foo] ["hello"]]) ?


hmm... it's not working as I expected...



+user=> (defn weird-compare [x y] (if (= (type x) (type y)) (compare x y) (compare (str (class x)) (str (class y)))))
+user=> (weird-compare :a :b)
+user=> (weird-compare :a "b")
+user=> (sort weird-compare [:a "b" 'c 3])
(:a c 3 "b")
+user=> (sort weird-compare [:a "b" 'c 3 66 2 50])
(:a c 2 3 50 66 "b")


its order is stable, and you get arbitrary order if they are not equal


it's not quite right yet for k/v or collections yet


If you search for 'cc-cmp' on this page, you can find a function I wrote a couple years back that tries to be a 'universal comparator' between objects of different classes, sorting first by class names alphabetically, then by class-specific means between objects of the same class:


It doesn't succeed at being universal, quite, but it goes a lot farther than clojure.core/compare does.