Fork me on GitHub
#clojure
<
2017-09-08
>
qqq03:09:48

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

madstap04:09:17

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

qqq15:09:12

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])

fmind06:09:11

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

andras.liter07:09:49

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

fmind08:09:18

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

fmind08:09:46

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

andras.liter08:09:39

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

fmind08:09:13

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

andras.liter08:09:27

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

didibus23:09:26

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

mbjarland08:09:01

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

sundarj08:09:10

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

mbjarland08:09:42

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

mbjarland08:09:45

I need insertion order

mbjarland08:09:58

unless I'm mistaken?

sundarj08:09:16

no, i think you're right.

mbjarland08:09:16

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

sundarj08:09:41

if you're cool with a dep

mbjarland08:09:03

yeah, was hoping not to..

sundarj08:09:30

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

mbjarland08:09:32

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

mbjarland08:09:14

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

mbjarland08:09:33

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

sundarj09:09:08

sorry 😛

mbjarland09:09:54

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

fmind10:09:46

his implementation is a bit more complex, and involves protocols

mbjarland10:09:53

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

gklijs10:09:36

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

martinklepsch11:09:33

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

martinklepsch11:09:18

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

martinklepsch11:09:13

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)

martinklepsch12:09:36

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 😄

cpmcdaniel12:09:10

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

cpmcdaniel12:09:31

I realize the material difference if coll contains booleans

cpmcdaniel12:09:19

actually, maybe (remove nil? coll) is better

martinklepsch14:09:21

This kind of makes me thing I would need to add my src dir manually, but it seems it’s implicit https://github.com/clojure/brew-install/blob/master/docs/guide-clj.adoc#including-your-source-directory-into-the-classpath

ghadi14:09:02

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

martinklepsch15:09:10

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

ghadi16:09:05

oh, neat.

martinklepsch09:09:26

@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.

martinklepsch13:09:24

@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

martinklepsch13:09:31

$ cat ~/.clojure/deps.edn
cat: /Users/martin/.clojure/deps.edn: No such file or directory
$ clj -S
/Users/martin/.m2/repository/org/clojure/clojure/1.8.0/clojure-1.8.0.jar:src
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

martinklepsch14:09:38

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

vincentdm14:09:00

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?

ghadi15:09:11

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.

ghadi15:09:06

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)

noisesmith17:09:06

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

reefersleep20:09:32

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)

reefersleep20:09:57

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

reefersleep20:09:17

once I :required it, no problem in either build

tjtolton15:09:12

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)

mpenet16:09:39

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.

mpenet16:09:25

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

mpenet16:09:13

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

tjtolton18:09:36

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

vincentdm15:09:13

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...

vincentdm15:09:21

That's a good insight. Thanks!

tjtolton15:09:50

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

tjtolton15:09:36

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

ghadi15:09:40

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

vincentdm15:09:37

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.

mbjarland15:09:07

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

tjtolton15:09:16

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.

ben.mumford16:09:15

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

lvh16:09:26

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

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

ghadi17:09:22

enumeration-seq

ben.mumford16:09:40

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

ben.mumford16:09:03

(ns model.document (:gen-class :name model.document :implements java.io.Serializable :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))

ben.mumford16:09:21

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

hiredman16:09:28

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

hiredman16:09:52

you also have to aot compile to use gen-class

ben.mumford16:09:15

how do i do that in leinigin?

noisesmith18:09:50

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

noisesmith18:09:42

excerpt:

;; 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]

ben.mumford19:09:48

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