Fork me on GitHub

is there a builtin for (defn avg [lst] (/ (apply + lst) (count lst))) ?


@qqq Don't forget to special-case the empty collection.


good point; actually, what is the correct behaviour of (avg ) ? should it be nil? 0? throw exception?

Matthew Davidson (kingmob)21:09:26

Logically, it should probably be nil. Or throw an exception. But definitely not 0, as that’s a totally valid mean value, e.g. (avg [-2 -1 1 2])


@qqq average is defined in incanter.stats (mean)


Hi guys. I am trying to run-all-tests of my lein project in a repl. I'd need to load/require all my test namespaces, so that I could run them with (run-all-tests #"mynamespace.*"). Is there a similar way to load/require all the test namespaces by a project-specific package regex? Thanks


@andras.liter you could create a namespace that requires all the namespaces you want to test


is there a reason why you don't use lein test from the command line ?


thanks @fmind, I am looking for fast feedback loops for test development with Eclipse, and lein test is incredibly slow (to run on each small change), thus want to have a repl running


in that case, you ca simply create a namespace that loads all the others you want to test


yes, will try that - hoping that listing all test namespaces there & run-all-tests then results in the same outcome as running lein test in cmdline and on our build server


run-all-tests doesn't work for you?


is there a way to have assoc stay with array maps even when they grow past size 10 without switching to a third party implementation? i.e. assoc switches to PersistentHashMap from PersistentArrayMap when the maps become large enough (> 10) and I need the map to stay ordered


if you need to have an ordered map, can't you use sorted-map?


I think sorted map is sorted according to a comparator or natural ordering


I need insertion order


unless I'm mistaken?


no, i think you're right.


seems to be the case, so I need the keys I add to the map to stay in the order I add them, which array map does and hash maps doesn't


if you're cool with a dep


yeah, was hoping not to..


i think the array-map to hash-map conversion is just how clojure works, no way around it


maybe I can always transform the map keys and values to sequences and call (apply array-map <seq>)


i.e. take the original map, get keys and values, append a key and a value, call array-map to create a new map


ugly, but should probably work...was hoping there was something less messy than that


sorry 😛


: ) it is what it is, thank you for your help and the pointer!


his implementation is a bit more complex, and involves protocols


I saw that but am trying to avoid extra dependencies...


Since it’s pure clojure you could just paste the sources you need into your project, no extra dependencies that way


Having some real weird problem in a large-ish project: I removed expectations code (everything works) and then I removed the dependency and suddenly my uberjar build fails with: java.lang.ClassNotFoundException: cljs.tagged_literals.JSValue


I removed all transitive dependencies from the expectations dependency via :exclusions — everything works — once I remove expectations itself things break


When I create a diff of the lein deps :tree output before and after removal of expectations it’s a one line difference (i.e. only expectations itself)


Seems that if I add a Clojurescript dependency to my regular build (i.e. not just :dev) things work. Just wrecks my brain what expectations has to do with all of that 😄


which is more idiomatic for filtering out nils, (filter identity coll) or (filter (complement nil?) coll)?


I realize the material difference if coll contains booleans


actually, maybe (remove nil? coll) is better


This kind of makes me thing I would need to add my src dir manually, but it seems it’s implicit


I'm almost certain it's not implicit... Are you seeing behavior otherwise?


try clj -S — I see src listed at the end @ghadi

Alex Miller (Clojure team)15:09:59

The default deps.edn includes it now

Alex Miller (Clojure team)16:09:30

It's not implicit but you're getting it by the user level defaults


oh, neat.


@U064X3EF3 not sure I understand what that means? not implicit but still a default in some way?

Alex Miller (Clojure team)12:09:13

It's not implicit because it's not hard coded into the library or the algorithm. You're getting it due to the merge of user level deps.edn and project level deps.edn. And both of those are under your control.


@U064X3EF3 I never created any deps.edn file though? Is there some default thats used if there is no file?

Alex Miller (Clojure team)13:09:35

Probably at ~/.clojure/deps.edn


$ cat ~/.clojure/deps.edn
cat: /Users/martin/.clojure/deps.edn: No such file or directory
$ clj -S
The guide doesn’t seem to mention any default values for when that file does not exist

Alex Miller (Clojure team)14:09:44

If it doesn't exist, one is created

Alex Miller (Clojure team)14:09:36

Do you have the XDG env vars set up? Might be in ~/.config/clojure if so


duh, ok, yeah, I have XDG_CONFIG_HOME set up 🙂


Hi guys, we just had an interesting discovery when working with clojure: we can call functions like clojure.string/replace throughout our project without explicitly having to require the clojure.string namespace. I assumed the same would hold true for the entire core lib, so I called clojure.set/intersection without requiring clojure.set. This worked in the REPL, but when trying to build an uberjar I get a compilation exception: java.lang.ClassNotFoundException: clojure.set, [...] Exception in thread "main" java.lang.ClassNotFoundException. To get it working I had to require clojure.set at the top of my file. Is there a reason why this behaviour differs between different core libs and between REPL/uberjar?


if you refer to the namespace qualified symbol clojure.string/includes? and the namespace clojure.string has already been loaded (by something else), this is fine.


you should still :require namespaces explicitly, and not assume that something else loaded them

Alex Miller (Clojure team)16:09:10

Yes, this. Right now some namespaces are loaded as a side effect of startup. But that may not always be the case (there are some pending patches that improve startup time by loading less for example)


also, some namespaces might be loaded implicitly because of the internals of your tooling, but hey you have different tooling in dev vs. production so hello bug that only shows up when you deploy


for cljs, you’d encounter a similar bug in case your prod build uses advanced compilation and your dev build does not (which I think is a very normal setting - I encountered it with some template project.clj settings, at least)


cljs.pprint/pprint was undefined in my prod build, but worked fine in my dev build


once I :required it, no problem in either build


Hey fellas! Does anyone happen to know what serialization format nippy uses? (its the serializer that Carmine uses, which is the library for interacting with redis via clojure)


Its own format. The code is fairly readable and the binary format quite simple. But it s full of custom stuff to support backward compat, compression, pojo serialization etc, I wouldn't advise trying to reverse it in another lang.


I had to share a redis instance that had a good amount of netty specific stuff with an erlang server, l ended up converting all the redis data to something more portable. One alternative is to write a little service that acts as encoder/decoder in clj, it s a bit naive but if it s not a hot path in your app it could work


Lesson learned: better to avoid language specific storage formats :)


yeah, so I discovered. You can get Carmine to store it raw though with a little work.


Aaah, ok. So every ns needs to be required explicitly, even when it's in the core lib? The reason I got so far without requiring clojure.string is probably because it is so common that in most cases one of my dependencies will probably already have required it without me knowing...


That's a good insight. Thanks!


we've got other services in Nodejs that have to read values in redis which were stored via clojure


so presumably we have to deserialize it into edn somehow, and then convert it to json


it's most common to alias namespaces @vincentdm ... (:require [clojure.string :as string]) then call (string/replace...)


Yeah, I know, but we have a convention of not aliasing clojure.string to make it more portable when copy-pasting code between files. I guess we better start doing so, because then my IDE (Cursive) will notify me of omitted requires.


noob question, I have two functions which both return a reify implementing a number of protocols, there is a fair amount of code duplication between the implementations of the two reifys, how do you go about sharing code in this scenario? just move things to functions in the enclosing namespace? I realize this question might be too open, but if I at least understand the options, maybe I can make a better choice


in case anyone was wondering, its a nippy specific format. its not language agnostic. I need to find some way to change the serialization that carmine does.


hi clojure. is there a trick to using gen-class?


Is there a builtin way to go from a java.util.Enumeration to a seq? I have:

(defn enum->seq
  [^java.util.Enumeration enum]
   (when (.hasMoreElements enum) (cons (.nextElement enum) (enum->seq enum)))))
which works fine, but less code is better




i get ClassNotFoundException whenever i try and use a class i've defined elsewhere 😞


(ns model.document (:gen-class :name model.document :implements :state "state" :init "init" :constructors {[String String String] []} :methods [[getContent [] String] [getTitle [] String] [getUrl [] String]])) (defn -init [content title url] [[] (atom {:content content :title title :url url})]) (defn- get-field [this k] (@(.state this) k)) (defn getContent [this] (get-field this :content)) (defn getTitle [this] (get-field this :title)) (defn getUrl [this] (get-field this :url))


(ns classification-server.classifier (:require [model.document :refer :all])) (new model.document "my-content" "my-title" "my-url")


you can't name the class the same thing as the namespace


you also have to aot compile to use gen-class


how do i do that in leinigin?


@ben.mumford you can provide an :aot key in project.clj, see lein help sample for a lot more info



;; These namespaces will be AOT-compiled. Needed for gen-class and
  ;; other Java interop functionality. Put a regex here to compile all
  ;; namespaces whose names match. If you only need AOT for an uberjar
  ;; gen-class, put `:aot :all` in the :uberjar profile and see :target-path for
  ;; how to enable profile-based target isolation.
  :aot [org.example.sample]


thanks for the pointer, i'll give that a try