Fork me on GitHub
Harley Waagmeester00:12:57

Would using def as a mutation mechanism cause any compiler or execution anomally ? i.e. (def a [1 2 3 4]), and then later on mutating a with (def a (conj a 5))


not sure what you mean by anomaly. would the following work for your use case?

(def a (atom [1 2 3 4]))

;; later on
(swap! a conj 5)

Harley Waagmeester00:12:15

I'm sure that would work. It is more idiomatic for sure.


def is thread safe, swap! is safer because it retries, to answer your question, no there are no compiler or execution anomolies caused by def (other than side effects inside def, which run while compiling, a common gotcha for making packages for deployment)


so for example someone runs $ lein uberjar and it never returns because it starts a server or something while compiling your code

👆 3

@U051SS2EU that’s gotta be one of the leading beginner gotchas 🙂 I remember running into that back in 2013 when I first tried making an uberjar


Are there any good books, guides, videos or other resources that focus on teaching the ecosystem surrounding Clojure? As someone who has barely touched Java I feel a lot more confused about maven and POM and deps and uberjars and nREPL etc etc than about LISP syntax or functional programming. I feel like a lot of the literature is aimed at people who are already familiar with the Java landscape, or it doesn’t touch the practicalities of running and deploying Clojure at all. I’ve read two Clojure books and I still feel like I don’t really understand how the REPL interaction with a running program works, or how I am supposed to deploy a Clojure program in a somewhat professional manner. In some ways I feel like starting out with Cursive has hampered my learning as the IntelliJ environment sets up a lot of facilities for me.


(n)REPL stuff is really orthogonal to the Java/JVM ecosystem. For books: • IIRC Clojure Applied has some good practical stuff. • Web development with Clojure has the "Deployment" chapter too. But I don't think you really need books for this. It comes to identifying which pieces confuses you and reading the docs or online guides (e.g. for maven deps or uberjars). I also barely understand nREPL and deal with the REPL just fine 🙂. When it comes to Java interop it's fine to learn as you go - you can read javadoc or try stackoverflow every now and then (but in my experience it's not needed that much; however, I do have a java background) One piece I find difficult is Java/JVM concurrency - if you need that read "Java Concurrency in Practice" or similar


clojure itself is the REPL, and happens to have functions that third party tools use to run in other ways nREPL is a tool that allows multiple parallel networked "sessions" talking to one clojure, and forwarding results to queries from each client, while also bolting on various convenient features (usually for editor integration) in a running program, you can (if you choose) serve up the singleton clojure instance via the network (with nREPL or the simpler socket REPL that clojure natively provides), this is optional and often a bad idea outside local debugging purproses


the tl;dr on maven: clojure programs are java programs, like all java programs they access runtime resources through the classpath, the dominant way of managing libraries via the classpath is maven which uses jars (zip files with a specific structure) and pom files (metadata about what is provided by the jar, and the dependencies it needs (other jars...))


you don't need a deep understanding of repls (outside clojure itself, which you are already learning and is a repl) - either you want the tooling (and therefore follow the recipe) or don't (and can ignore the recipe) you might end up needing a better understanding of deps (especially as they can conflict and break one another), but as a beginner you can ease yourself into this by avoiding external libraries not provided by some recipe on project creation


Thanks a lot @U051SS2EU, this really helps. I know about JARs in general, but are jars used together with maven essentially "packages and a system to manage these packages"? And pom files are manifests for the "packages"?


right, packages and manifests precisely - the special thing is that it's kept as a versioned cache, rather than global system or user level "installs" - so two clojure projects can each use conflicting versions of the same library


it's all under ~/.m2/ by default


That's great to hear. Lots of languages have had to solve that issue over the last decade or so.


right, clojure itself has never needed to care as it used java, which already solved the issue (very elegantly IMHO) via classpath and maven


And is there some sort of de facto standard "feed" for maven packages?


I put this in a deps.edn file and it works, but I have no idea where it's downloading it all from:

{:deps {org.clojure/data.csv {:mvn/version "1.0.0"}}}


maven repositories, we use and by default with all mainstream clojure package managers, and those can be queried via web UI


So clojars is the place to put clojure packages and is "everything Java"?


almost - clojure.core and all the important clojure libs are on maven - clojure is a java library


clojars is very clojure specific, and leiningen (once the only good clojure package management choice) makes it easy to put your code on clojars


And uberjars - is that creating a single .jar file containing all dependencies?


clojars has looser rules about package naming, versioning, etc.


Except a java runtime, I would assume?


right, the dependencies are all resources (.clj files, class files, etc.) and clojure itself is the java program which will end up using those resources, but only rarely is the vm itself packaged with all that


it is possible to make an "executable jar" containing vm plus deps, but most people don't need or want that


@U01GZD00HA8 a fun thing is that you can use clojure to examine the resources on your classpath (demo in a moment)

👀 3

(cmd)user=> +
#object[clojure.core$_PLUS_ 0x54336c81 "clojure.core$_PLUS_@54336c81"]
(ins)user=> ;; from the above, we can get a file name: clojure/core
(ins)user=> (io/resource "clojure/core.clj") ;gets us the handle to read it
#object[ 0x3fcdcf "jar:file:/home/justin/.m2/repository/org/clojure/clojure/1.10.1/clojure-1.10.1.jar!/clojure/core.clj"]
(ins)user=> ;; note that above you can see the path, plus the identifier of a resource inside that file
(ins)user=> (def source-code (slurp *1))
(ins)user=> (println (subs source-code 10000, 10200))
       m (if (map? (last fdecl))
                  (conj m (last fdecl))
              fdecl (if (map? (last fdecl))
                      (butlast fdecl)
(ins)user=> ;; source-code contains the full text source of clojure.core
so features you need tooling for in other languages (finding the source for some library etc.) can be done (somewhat tediously) in clojure's repl


I've often solved strange bugs by verifying the contents of what was packaged in some artifact


(which sometimes doesn't match what I'm seeing in git or my local filesystem for for complex reasons...)


Hah, that's beautiful! Also reminded me to start using the * repl shortcuts. I keep defing stuff just to use it on the next line...


also, any good programmer's editor can open a jar file as if it were a directory for exploring


it's a hobby horse of mine that clojure's tooling is not as needed as tooling for most languages, and that there's an advantage to learning the clojure introspection facilities that a lot of tooling is built on (especially when brittle tooling fails and you need to know what's actually happening)


@U01GZD00HA8 I have online books and videos that cover the use of core tooling, especially using Clojure CLI tools which has helped me understand the REPL more as well as the ecosystem. There is still lots of content in that area to add too. I've learnt a lot about deployment, specifically when deploying web applications


Thanks a lot @U05254DQM - will check out!


a few random questions: 1) does (last (take 1000 (some-infinite-lazy-seq)) end up being the same as (nth some-infinite-lazy-seq 1000) because of the laziness? or do the extra 999 from take occupy some space/time? 2) if I annotate values with type metadata, like ^long, do I need to annotate each fn that those values will travel through in order to take advantage of any potential performance gains? or just the place where they initially come into be? like if I annotate the return from a fn, do I also need to annotate functions where I'm passing that result or is just one place enough? 3) is there an idiomatic value to use for the placeholder in as->? I've been using _ though I see a lot of $. I'll probably keep using _ because I like it, but just wondering if there's convention there


3) replacing unused function/collection elements with _ is idiomatic. I see people use $ a lot for as->. I can't see anything in


I don't think anyone will come after you with pitch forks either way!


Yeah, like @UV1JWR18U says, _ would indicate that something that is unused, so other people reading your code might get confused.


I just use $, personally


Regarding 1, while far from comprehensive, this should give you an indication:

(dotimes [_ 5] (time (nth (repeat 1) 1000)))
"Elapsed time: 0.095773 msecs"
"Elapsed time: 0.069648 msecs"
"Elapsed time: 0.071448 msecs"
"Elapsed time: 0.068614 msecs"
"Elapsed time: 0.076651 msecs"
=> nil
(dotimes [_ 5] (time (last (take 1000 (repeat 1)))))
"Elapsed time: 0.243615 msecs"
"Elapsed time: 0.217264 msecs"
"Elapsed time: 0.208162 msecs"
"Elapsed time: 0.213417 msecs"
"Elapsed time: 0.206169 msecs"
=> nil


I'm not sure how nth works with lazy sequences, and I'm a Clojure beginner. But I would assume that last+take both have to traverse the elements in some way. nth should only have to do that once in this scenario.


If you expand the example to take the 10,000nd element the performance difference is a lot bigger in favour of nth.


Regarding 2: “Once a type hint has been placed on an identifier or expression, the compiler will try to resolve any calls to methods thereupon at compile time. In addition, the compiler will track the use of any return values and infer types for their use and so on, so very few hints are needed to get a fully compile-time resolved series of calls.”

Alex Miller (Clojure team)13:12:06

1 - yes they take space to allocate and gc as the take seq nodes (which nth will not) 2 - it depends. If the value is passing through multiple functions, each arg has to be marked as long or it will be converted to a boxed value 3 - don’t use _, but otherwise doesn’t matter


:thumbsup: thanks!

Janne Sauvala10:12:48

Is there a way to update/replace value in lazy seq? All my attempts have failed to complains that seq is not an associative structure. When I change my lazy seq to a vector then I can do it with (assoc my-seq 1 new-element)


I don't know of a single function for this, but it can be achieved with take and nthrest:

(defn update-seq [target i elem]
  (let [pre (take i target)
        post (nthrest target (inc i))]
    (concat pre [elem] post)))


user=> (take 10 (update-seq (range) 5 "hello"))
(0 1 2 3 4 "hello" 6 7 8 9)

Janne Sauvala10:12:32

Thanks, Balint. I wonder is there some reason why core functions doesn’t support this out of the box


There's replace if you really to to "update/replace value"

Alex Miller (Clojure team)13:12:52

Replacing nth value in an immutable list/seq is a slow (linear) op so there is no function for it.

👍 3
Alex Miller (Clojure team)13:12:30

Generally we try to think in terms of whole seq modifications, using map etc anyways

Janne Sauvala13:12:19

Thanks Alex, that makes sense

Janne Sauvala16:12:56

@U06BE1L6T I tested this but realised that it will replace elements by comparing the actual value but not by index. So if I have several elements with the same value I’ll be replacing them all

Alex Miller (Clojure team)16:12:42

I think generally if you get to the point of asking this question, you are probably going about something the wrong way

Janne Sauvala17:12:03

Agree. I ran into these situations while I’m solving advent of code problems. Now the easiest fix for the solution would have required me to just replace an element in a lazy-seq. I haven’t run into this before and wanted to confirm if this is even possible 🙂


Leiningen question: using lein uberjar to create a standalone jar for deployment, but for some reason it's taking ages. It takes 20min to end up with an uberjar of only 140M (So not exactly massive?). Any directions on how to improve this? Should I look into leiningen, or are jvm options to blame, or .... ? I have no proper Java experience, so often feel in the dark with these kind of Clojure ecosystem problems.


Are you sure you aren't doing any "real work" when compiling? In particular, if you use AOT compilation all the top level vars are evaluated. Sometimes this might mean that connections to external resources (like DBs) are established and so on.


No top level vars in my code. Also, the class generation part and the non-standalone jar are created in ok times, but the standalone one takes ages. Does this mean a dependency is the culprit or are there other potential reasons?

Alex Miller (Clojure team)13:12:17

One possible reason is you are including the uberjar in itself

Tim Robinson12:12:05

Hi all, I've written a function called map-in that lets you transform a single element of a (possibly) nested structure like this: (defn map-in [m ks f] (assoc-in m ks (f (get-in m ks)))) so let's say I want to increment the age of the first person in the list: ==>(map-in [{:name "John" :age 35},{:name "Sue" :age 29}] [0 :age] inc) [{:name "John", :age 36} {:name "Sue", :age 29}] Thing is, I don't want to invent the wheel and I'm sure this must be already in core or some common library, can anyone point me at it?


Vectors are associative, so if you know that m is a vector, you can simply use update-in:

user=> (update-in [{:name "John" :age 35},{:name "Sue" :age 29}] [0 :age] inc)
[{:name "John", :age 36} {:name "Sue", :age 29}]


> if you know that m is a vector Which I guess you're already relying on since you use get-in in your implementation.

Tim Robinson12:12:14

Yes thanks update-in is exactly what I was looking for but just couldn't find the right words to search for it. actually m will usually be a map but update-in works with both


How do I create a clojure app if clojure -X:new create :name myname/myapp gives me this error? Unqualified function can't be resolved: create


Which version of the clojure CLI tool do you have installed? You can use clojure --help to check. That command should also work.


clj -X:new :template app :name myname/myapp is the answer


where I can find documentation for running main function? I get all sorts of errors when I try examples found on the web


I would never guess reading the output of clj --help


thank you very much, I will try now]


clj -M -m Syntax error compiling at (cls/report_generation.clj:12:3). Unable to resolve symbol: simple-body-page in this context Full report at: /tmp/clojure-2027952924787189784.edn


There's a bug, move the routes after the 2 next fns used.


still the same problem


thank you very much for helping me with this frustrating problem


ah, I see now routes are twice there, just remove the first defroutes


Hurray, the browser says hello world. thank you for your patience and help!

👍 3
Tim Robinson18:12:29

try using some kind of linter that integrates with your editor. I found it difficult to set up but once it's working it's so much easier to find problems


Sorry to ping you directly @alexmiller, but I'm curious about Clojure Applied. Is it still mostly relevant and useful stuff, or have parts of it gotten outdated enough that you'd recommend something else? Or just some complementary reading? It's hard to know how much five years really is for a book when you're new to a community. And I figured you'd be the best source to ask here...

Alex Miller (Clojure team)19:12:21

I'd say it's still 80% relevant

Alex Miller (Clojure team)19:12:43

I am starting to look at 2nd edition, but it's likely to be at least a year before such a thing exists


Great! I'll read it with that in mind. Thanks a lot for answering. I'm on a Clojure reading binge at the moment (as most of my current free time is reading in bed while my daughter is sleeping, can't actually program there...).

Alex Miller (Clojure team)19:12:11

we call that "hammock time" :)


Yes, apart from reading I'm also looking at the massive backlog of talks, Hammock Driven Development being the one I just started.


Surprisingly I've found that reading about or formulating a problem early in the day and then having it simmer in my mind while I work, pick up groceries and so on usually lets me discard at least one or two false starts that would waste a few hours of coding...

Michael Stokley20:12:15

i find that some of the pretty printing doesn't return a value that can be read/evaluated. specifically, vectors become lists with a literal (). i often want to replace the data with a transformed version, and iterate on that. are there strategies or other pretty printing folks know of?


> vectors become lists with a literal () this is not true, you are changing your data type somewhere or are misusing the term "vector"

Alex Miller (Clojure team)20:12:28

if you quote those, you can read them as data

Michael Stokley20:12:53

with the repl... it feels like this should be or could be programmatic

Alex Miller (Clojure team)20:12:15

I don't find it to be so, depends on how you're using it

Alex Miller (Clojure team)20:12:56

if you def the result (or use *1 etc) you are still working on the data


i just want to point out that your specific complaint doesn't seem true. Vectors do not become lists. (clojure.pprint/pprint [1 2]) yields [1 2] not (1 2)

Alex Miller (Clojure team)20:12:16

I'm assuming he's seeing things like (map inc [1 2 3])

Alex Miller (Clojure team)20:12:33

applying a seq function to a vector will return a seq that prints as a list

Alex Miller (Clojure team)20:12:57

it is in fact very consistent if you understand the model :)

Michael Stokley20:12:31

maybe it's as simple as switching to mapv


The problem is, you start with a vector, but then are using sequence functions on it. Those will automatically convert the vector into a sequence and from that point on, you have a sequence which prints as a list


Instead, you need to use vector functions, those will keep things as a vector. The downside of this is that there's a lot less of them. You have mapv and filterv and that's about it


Another approach in those cases where you really care about the type of the collections is to use transducers instead


There's a lot more transducer functions then there are vector functions (though still not as many as their are sequence functions)


With transducers, you just tell it what type you want the resulting transformation to be in

🙌 3
🙏 3

For example this:

(->> [1 2 3 4 5]
     (filter odd?)
     (map inc)
     (take 5))
Takes a vector but then uses sequence functions on it, so it'll return '(2 4 6) You can switch to transducers and using into chose your output type:
(into []
   (comp (filter odd?)
         (map inc)
         (take 5))
  [1 2 3 4 5])
Now that will return [2 4 6]


And this is an example where you could have used filterv and mapv, but there is no takev, so ya transducers to the rescue!

Michael Stokley21:12:26

very slick, this should prove helpful to me


does using into with a transducer/xform give runtime benefits over doing it like the first example:

(->> [1 2 3 4 5]
     (filter odd?)
     (map inc)
     (take 5)
     (into []))
or using vec instead of the into?


Yes it does. Using transducers will be the fastest and use the less memory. Using sequence will be faster than using vector functions, at least for large collections, maybe not for small ones. But it will use more memory. Using vector functions will be slowest, especially for large collections, might be faster than sequence for small ones, won't be faster than transducers though.


The way to think of this is that, with vector functions like filterv and mapv, you are looping over the whole collection at every step. So first filterv everything, then mapv everything. So you're unnecessarily looping more than once over the collection. With transducer, you are looping only once, and applying the full chain of transformation each iteration. So you loop only once of each element, and filter map and take in one go. With sequence, you will be looping only once on the collection as well, like transducer, but after each iteration, you will create a remainder sequence, the creation of this intermediate sequence after each iteration is why it is slower than transducer. But this is also what makes it lazy, as the remainder sequence won't start the next iteration immediately, it'll wait for someone to request the next element from it to do so. Finally, calling vec after a sequence will cause a second loop, so the sequence chain will loop once and filter, map and take on each iteration (creating an intermediate sequence at each step), and finally when that is done, vec will loop over the result one last time to convert the sequence back into a vector. That is one extra loop over using transducers directly.

🙏 3

thanks, that was super helpful