Fork me on GitHub

Hi! I'm starting a new project and I'm considering using Clojure, in docker-based microservices. What I'm worried about is, will the JVM and its high memory allocation be an issue in the beginning, when I'm planning to run on small instances with not much RAM? I'm thinking that I'll not be able to put too many containers in the same instances, as every container will run a JVM. Any one has experiences with this? Does fine tuning the JVM mitigate the issue? Should I set a low memory limit, monitor and scale when needed?


Hey. How to write clojure.spec for a map which keywords match a specific predicate, for example, say I’d like this map to conform spec that validates keys prefixed with foo-: {:foo-1 “value1” :foo-bar “value2” :foo-anything “bar”} Anyone have a clue how to achieve that without enumerating all keys explicitly? Thanks in advance for any clues.


Should I use map-of somehow maybe?


@pawel.kapala Yeah, I think map-of is what you want


;; From the spec guide
(s/def ::scores (s/map-of string? int?))
(s/conform ::scores {"Sally" 1000, "Joe" 500})
;=> {"Sally" 1000, "Joe" 500}


Here you could change string? to your custom prefix predicate


@curlyfry Thanks this is the way!


Why does the category not be printed here :

(deftype Razzie [category]
  (present [this recipient]
    (print (str "You're really "
                (:category this) " , "

Alex Miller (Clojure team)14:11:09

The (:category this) isn't meaningful for deftype a


oke, and why does it work here :

(defrecord Oscar [category]
  (present [this recipient]
    (print (str "Congratulations on your "
                (:category this) " Oscar, "

Alex Miller (Clojure team)14:11:04

Keyword lookup access is built into defrecords by default but deftype has almost nothing pre-built - it's up to you to build it

Alex Miller (Clojure team)14:11:30

You could just use category in the defrecord too (and you should)


oke, another exercise for learning the difference between defrecord and deftype

Alex Miller (Clojure team)14:11:11

The fields are available by name in the scope of both the record or type definition

Alex Miller (Clojure team)14:11:48

And that is more efficient than anything else in the generated bytecode


admittedly not completely clojure related, but is there a way to reference a file from leiningen that's contained inside a maven dependency? there's a .less file in a dep that i want to include in my own project, and i can see it when i inspect the jar in ~/.m2

seancorfield15:11:05 Good to know -- I've always done (:key this) thinking that would be the most efficient and idiomatic way to do field access in a record. I'll create some tickets in my projects to change that!

Alex Miller (Clojure team)15:11:28

:key is definitely good outside the record, but inside, using the field name will just be a direct field reference basically rather than a call

Alex Miller (Clojure team)15:11:04

@joshkh any file inside a jar can be loaded as a resource

Alex Miller (Clojure team)15:11:24

assuming it’s on your classpath (specified as a dep in your project)


okie dokie, good to know. thanks.


speaking of deps, how are different versions handled? let's say dep A relies on clojure 1.8, and another dep on clojure 1.9. are both bundled into the final jar?


@joshkh pretty sure only one version is included. The underlying maven dependency resolution machinery will choose the best version to include (I think in this case it would use 1.9). You can control which transitive dependencies to exclude using :exclusions, check out the example here (where they bring in log4j but exclude some of log4j’s dependencies):


so, for example, if you wanted to make sure your project uses clojure 1.8, you could use :exclude to exclude the transitive clojure dependency from both dep A and B, and then add an explicit dependency on clojure 1.8 inside your project.clj. Hope that helps and isn’t too confusing 😉


Can someone explain this one :

"Notice when metadata carries over"
  (= {:foo :bar} (meta (merge '^{:foo :bar} {:a 1 :b 2}
                     {:b 3 :c 4})))  


is the answer {:foo :bar} because there is a reader ^ before it


so the rest get ignored ?


yep, with quote everything stays the same


note it's putting {:foo :bar} into the meta data of the first map


and then its showing that the end result of the merge has the meta data still intact


"Notice when metadata carries over"


but where stays the {:a 1, :b 3, :c 4} part then ?


I think I can better do the rest of the koans when Im more familiair with clojure


that's in the map


but you're asking the map for its meta information


(meta (merge '^{:foo :bar} ....))


so you're merging maps and then asking for the meta info of the resulting map


and your koan is asserting that its equal to {:foo :bar}


oke, on that way


very confusing stuff when never working with clojure


so the part with ^ will become the metadata ?


oke, I think I begin to understand metadata


but I wonder when you use it


I've used it to check the arity of a function


in cider, you can "refresh" your repl


and you can specify functions to call after or before refreshing


but they can only be functions that take no arguments, as what arguments could cider give


but some functions have optional arugments


but its not technically a funciton of zero arguments


but "could" be a function of zero arguments


when you define a function it puts its function args in its metadata


so i checked those to see if it could be called with zero arguments


so in my case i was looking at the :arglists slot of functions


Thanks for the info


i've seen a library that tucks quite a bit of stuff inside of meta data. Wasn't sure why and it didn't have to be that way, but it was


hey question this is clearly very contrived but suppose you had the form (+ '(1 2 3)) plus expects individual values so I could do (apply + '(1 2 3)) but suppose I had the equally contrived (+ '(1 2 3) 4 5) what is the best way to break the list out of the interior list?


I can do (apply + (flatten (list '(1 2 3) 4 5))) I was wondering if there was a function where I could do (somefn '(1 2 3) 4 5) and do the lot or if I should create one or if I am just doing it suboptimally


wondering if there is a generic function I could wrap '(1 2 3) that would result in it being lifted into the above function


if you can change the order, i believe apply can handle the list at the end


(apply + 1 2 '(3 4 5))


interesting thanks


@dpsutton it is wise to include metadata in every big project in every function that you made. Or is it better only to use it in API and that sort of libraries ?


its wise to ask that question


i have no idea about the answer except that my suspicion is that you should have a good reason to use meta

Alex Miller (Clojure team)21:11:57

meta is useful only in some pretty narrow use cases

Alex Miller (Clojure team)21:11:15

note that it can be tricky to know what fns preserve meta and which do not

Alex Miller (Clojure team)21:11:32

and that meta is not considered when comparing for equality

Alex Miller (Clojure team)21:11:15

almost every time I have used or seen someone use meta they ultimately found that what they were putting in meta was important enough that it really should be treated as data, not metadata

Alex Miller (Clojure team)21:11:35

that said, var and ns meta is pretty useful in the language itself


oke, so almost I do not have to use meta


Thanks, for the answer

Alex Miller (Clojure team)21:11:47

the primary reason you will use meta in most Clojure code is to apply things like type hints or ^:private to vars


Both against thanks


i thought i remember either xml or zip using a bunch of meta for some reason


might be zip that stored a ton of stuff in meta