Fork me on GitHub
#clojure
<
2017-05-22
>
qqq01:05:57

is it possigble to do the same thing with tomcat, I want to "fire off a tomcat from inside cloljure, passing it a ring handler"

tianshu01:05:34

Is there any good library for async jdbc in core.async. now I use thread for jdbc operate. I'm using MySQL.

noisesmith01:05:05

there's no libs for async jdbc period because jdbc can't actually do async

noisesmith01:05:19

using thread inside core.async is a great idea, best option IMHO

noisesmith01:05:57

it would be awesome if the db driver actually supported async directly

tianshu01:05:07

I saw there's some library like postgres-async. I'm not sure how it works

noisesmith01:05:57

if it's jdbc it's just using threads, it can't do anything better

noisesmith01:05:06

if it's not jdbc, then it might be cool

tianshu01:05:19

using thread directly may cause problem IMO, maybe a thread pool is better?

noisesmith02:05:41

thread uses a pool

tianshu02:05:14

😂I'll take a look

noisesmith02:05:15

(defonce ^:private ^Executor thread-macro-executor
  (Executors/newCachedThreadPool (conc/counted-thread-factory "async-thread-macro-%d" true)))
@doglooksgood

noisesmith02:05:50

that's what thread uses (via thread-call)

sophiago02:05:28

i'm confused as to why (def (gensym) ...) is telling me gensym isn't returning a symbol when (symbol? (gensym)) => true?

noisesmith02:05:16

because def is a macro, it doesn't evaluate that argument

noisesmith02:05:27

(gensym) when unevaluated, is a list containing a symbol

sophiago02:05:31

ah, ok. thanks justin

noisesmith02:05:35

you need to write a macro to def from gensym

sophiago02:05:17

well, considering the use case i'm thinking there may be a better option than def

mfikes02:05:27

Maybe (intern *ns* (gensym) :value)

noisesmith02:05:41

also, in general, if you aren't accessing the value from code written in a file, it's much more convenient to just use a hash-map instead of using a namespace via def

noisesmith02:05:54

it is a much nicer api to work with

sophiago02:05:19

i have a library that currently requires the user to define a global atomic map, pass it to a function, and then pretty print it. i figured i should automate that with a macro so they can just call one function that does everything plus returns the map

noisesmith02:05:51

OK - why not let the user decide where to put the map?

sophiago02:05:49

i'm toying with making it a little more dsl-ish

noisesmith02:05:51

anyway, just an observation, it's a lot easier not to do automated things with namespaces, and there's few benefits to doing them with namespaces in my experience

noisesmith02:05:13

but that's just my opinion of course

sophiago02:05:42

i'm just saying, were i to do this, it seems calling def inside a macro doesn't make much sense, right?

sophiago02:05:21

i'd rather have the map bound locally and then not need gensym. then i can return a copy and have the user decide what they want to do with it

sophiago02:05:08

anyway, to solve the macro issue i'd just need to unquote splice it right? like (let [store @~(gensym)] (def store (atom {})))?

sophiago02:05:27

err wait...without the deref i think...

sophiago02:05:58

ah! sorry, i got it. i don't use macros hardly at all 😛

sophiago02:05:22

obviously need to put a let on the outside of my quasiquote for something like that

noisesmith02:05:17

(defmacro random-var [value] `(def ~(gensym) ~value))

noisesmith02:05:32

or with a let block

(defmacro random-var [value] `(let [sym# ~(gensym)] (def sym# ~value)))

noisesmith02:05:17

the # suffix is needed because ` doesn't like unqualified symbols, even in binding positions

sophiago02:05:36

i got it to work by just putting the let outside the quasiquote (everything else is in a do block)

sophiago02:05:12

but thanks, that's good to know for the future

lincpa03:05:43

it isn't a good style that def isn't on the top, You might be able to find a better solution.

lincpa03:05:46

according to the scene

raheel06:05:01

hello! I am trying to use Immutant.scheduling to run something periodically. However this

(let [t (clj-time/today-at 11 00)]
  (schedule foo
    (-> (at t) (every 1 :hour))))
gives me an error: No implementation of method: :as-time of protocol: #'immutant.coercions/AsTime found for class: org.joda.time.DateTime any thoughts? I am figuring t cannot be passed to at here, but am not sure why! Thanks

rauh06:05:05

@raheel Are you requiring immutant.scheduling.joda?

raheel06:05:54

oops. I was not! thank you, that does fix it.

Björn Ebbinghaus07:05:30

Hey, I have Clojure and Java code side-by-side in my project. Is there a leiningen plugin for static code analysis like kibit or eastwood for java code?

georgberky08:05:04

Do you need something for Java only or Java and Clojure at the same time? If it's Java only, you could use Findbugs, or Sonar if you want to use a full-fledged tool.

Björn Ebbinghaus08:05:11

georgberky: Both would be great. I didn't find a Findbugs plugin, which would be great. It doesn't have to be a full blown tool. just a small report in the terminal. Do you know a practically way to integrate findbugs into leiningen?

georgberky14:05:08

Hmm, findbugs seems to be limited to Java code, so not sure if you can find a tool that can do both Java and Clojure. But maybe Sonar is an option for you. They seem to have a Clojure plugin.

georgberky14:05:35

At least you'd have both Java and Clojure in the same UI, even if they're not handled by the same tool.

blane09:05:15

hey guys, can someone point me to a Dockerfile that installs clojure. I don't want to use FROM:Clojure

mccraigmccraig09:05:00

@blane if you build an uberjar you don't need your Dockerfile to install clojure - a jdk will do just fine, e.g. https://hub.docker.com/_/openjdk/ or https://hub.docker.com/r/frolvlad/alpine-oraclejdk8/

pesterhazy09:05:49

@blane, people often build the uberjar and build the docker image around that

pesterhazy09:05:12

then there's no need to install clojure, all you need is a jdk

pesterhazy09:05:04

disclaimer: I'm not a fan of docker so I may be all wrong

lepistane09:05:13

this is not clj question but i cant seem to find solution and you are pro developers someone surely must know the answer guys i've got sql question hope someone can help create table forums( id int auto_increment primary key, name varchar(50) not null, description varchar(250), ownerUsername varchar(30) not null, -- owner creationDate datetime not null, -- creation date locked boolean not null, type varchar(30) not null, parentForumId int, -- nadforum, deleted boolean not null, FOREIGN KEY (ownerUsername) REFERENCES users(username), FOREIGN KEY (parentForumId) REFERENCES forums(id) ); this is my table i have tree like structure forum1 has (forum 2, forum 3( has forum 4, forum 5)) i wanna make statement or function that would delete all children if parent is 'deleted' (deleted set to 1 ) i am using mysql and i kinda saw something i could use http://stackoverflow.com/questions/39325801/mysql-change-status-of-all-child-records-if-status-of-parent-is-updated i am just not sure how to make use of it

urbank09:05:54

Is there a cond-as-> threading macro?

jumar09:05:50

@lepistane if you just want to perform delete, the approach with foreign keys might work (I didn't try that). However, for more efficient approach usable for querying too see "Nested Set Model" (e.g. http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) - relational databases usually suck at modeling tree-like structures but I guess that's the best model you can use with MySQL.

urbank10:05:36

jumar: Yeah, doesn't seem to exist. I suppose cond-> with #() is a fine replacement

lepistane10:05:18

@jumar i will read this i mean worst case scenario i will make recursive function in java DAO class that sets deleted =1 to all children until there are no children it will be unefficient as.... but u know

lepistane10:05:22

@jumar tnx for the read it's way over my head (i am newbie) i will get back to it in a month when i finish this and now to the recursive functions!

armed10:05:51

Hey, folks In example below if I replace #'home-routes to home-routes nothing changes, program still works. What is the reason to use var reader macro in such places?

(def app-routes
  (routes
    (-> #'home-routes
        (wrap-routes middleware/wrap-csrf)
        (wrap-routes middleware/wrap-formats))
    (route/not-found
      (:body
        (error-page {:status 404
                     :title "page not found"})))))

jumar11:05:33

@armed it still works, but you will loose the ability to reload the routes definition without restart, which is quite handy during the development

armed11:05:38

@jumar thanks, now I see why sometimes I had to restart repl.

jumar11:05:56

you still need to use wrap-reload middleware, though - I'm not sure what's your setup, but Luminus will generate the code automatically for you in dev-middleware namespace

cpmcdaniel11:05:12

namespace on a keyword is a different thing than namespace on a var, correct? For a keyword it is just a string, there may or may not be a physical namespace object by that name.

cpmcdaniel11:05:36

ie, you can’t get a list of all keywords for a given namespace

cpmcdaniel11:05:12

because they aren’t registered with the namespace like vars are. Do I have this right?

vargaradu11:05:33

Aanother question related to reloading, have any of you any experience in hot code reloading in non-web server code? I tried implementing the same code as in wrap-reload, but it does nothing.

mccraigmccraig11:05:22

@vargaradu not sure about wrap-reload but you can certainly use tools.namespace/refresh directly for hot code reloading - https://github.com/clojure/tools.namespace#reloading-code-usage

vargaradu12:05:05

@mccraigmccraig I just tried that, but i keep getting java.lang.IllegalStateException: Can't change/establish root binding of: *ns* with set Note: I am using mount to start stop my components, and am calling this refresh function right before I receive an async message (via rabbitmq/langohr)

mccraigmccraig12:05:15

i don't have enough context to understand @vargaradu - what's the stacktrace of the exception you are seeing - and are you hooking mount's stop/`start` around tn/refresh like this https://github.com/tolitius/mount#the-importance-of-being-reloadable

vargaradu12:05:43

Thanks @mccraigmccraig , I was looking for something like that, dunno how I missed it 🙂

mattyulrich15:05:25

Doesn't look like a lot of activity in the CLR channel - anyone here using clojure/CLR or have an idea where that crew is hanging out?

lincpa00:05:03

mattyulrich: I've written a 25K lines clojure-clr project. https://github.com/linpengcheng/fa

lincpa00:05:41

Now migrate to Clojure Luminus

josh.freckleton16:05:23

I just missed a hidden ArityException that made it to production, how do I eliminate this from happening? it seems that those should be able to be caught at compile time, no?

xiongtx16:05:04

@alexmiller Is it not recommended to mix ‘n match reducers and transducers like:

(r/reducer [1 2 3] (map f))
instead of
(r/map f [1 2 3])
Due to differences between rfn and transducers? See: https://groups.google.com/d/msg/clojure/xU5vwYutp3s/zTaGYfOiBQAJ

dpsutton16:05:19

with threading macros and apply I can think of scenarios where the compiler couldn't enforce it

josh.freckleton16:05:41

@dpsutton ah, it was in ->> now that you mention it

josh.freckleton16:05:14

a test would expand macros, right... I suppose I could just write a test that it's always called with the right arity...

dpsutton16:05:01

write a test to make sure what ever is producing the values always returns the right data?

josh.freckleton16:05:41

that too, I struggle sometimes with writing tests with heavily effectful code, but that's a good point

dpsutton16:05:58

yeah. but gives a reason to try to separate the two

lepistane17:05:38

how is clj clr? is it getting better/more viable ?

mattyulrich17:05:03

I dunno - was looking to try it out but I can't get it to run as of right now. Hoping someone here may be able to provide some guidance.

tbaldridge17:05:42

if you're wanting to get into Clojure CLR, I recommend downloading this: https://github.com/nasser/nostrand

tbaldridge17:05:52

it's basically lein for Clojure CLR.

tbaldridge17:05:09

using that I was able to get it up and running in a few minutes.

lepistane17:05:19

my friend made windows app which i wanted to make feature in clj then i found out about clj clr

mattyulrich17:05:20

cool.. I'll take a look at that..

mattyulrich17:05:59

I spent the last 10 years working at a Java shop and therefore used clojure for all my personal tooling/testing/poc stuff.

nasser17:05:01

still needs work to better integrate with the .NET ecosystem, but I use it daily! feedback and PRs welcome!

tbaldridge17:05:02

lol and the man himself appears

mattyulrich17:05:04

Just switched to a .net place

mattyulrich17:05:16

Hoping to try and get a bit of that back.

nasser17:05:19

@tbaldridge s u m m o n e d

lepistane17:05:34

i will take a look, tnx for link

mattyulrich17:05:50

so, @nasser, to use this with microsoft clr may need some work?

nasser17:05:49

nostrand itself is designed to work with mono, and i test on osx

nasser17:05:17

microsoft’s clr env is set up differently, and depends on visual studio a lot more as i understand it

nasser17:05:15

but nostrand exists because clojure is primarily a library and not a command line tool

nasser17:05:03

so it depends on what you want to do with it. if you want to integrate it into a microsoft clr project as a library, Clojure is on NuGet already. if you want to use nostrand’s dep management and command line tools, its going to take some tweaking.

mattyulrich17:05:11

right.. I should investigate what it does with Mono first and then see what the parallel in microsoft might be... I pulled the NuGet module, but can't run the repl (dep issue)...

nasser17:05:49

the Clojure.dll i use in nostrand is patched, too. its taken from https://github.com/arcadia-unity/Arcadia

nasser17:05:16

nostrand also implements its own repl, which i am on the fence about keeping

nasser17:05:22

which clojure repl did you try and use?

mattyulrich17:05:45

cool - maybe I'll try that.. I'm getting a "System.IO.FileNotFoundException: Could not locate clojure.core.server.cljc.dll or clojure/core/server.cljc on load path"

mattyulrich17:05:24

The 1.8 from NuGet - just pulled the extracted dlls and executables to a separate place and tried to run Clojure.Main.exe - not sure if that's the appropriate way to get it to find all requisite assemblies or not.

nasser17:05:46

hm, clojure.core.server should be on the load path. i havent pulled from NuGet in a minute, that might be a bug

mattyulrich17:05:53

Well - I'm "new" in returning to microsoft-land... So, it's possible that I'm not getting the right assemblies together. Is everything supposed to be in that Clojure.dll ?

mike_ananev19:05:42

hi! can't compile project with [metrics-statsd "0.1.8"] and [org.clojure/clojure "1.9.0-alpha16"]. if I put [org.clojure/clojure "1.8.0"] everything works fine. i suspect that ["-Dclojure.compiler.direct-linking=true"] in metrics-statsd can cause such error. any suggestions?

mike_ananev19:05:49

here is the stacktrace

mike_ananev19:05:11

that stacktrace is produced by (require '[metrics-statsd.core :as statsd])

ghadi20:05:46

@mike1452 I just downloaded that metrics-statsd jar, and it's a rascal. It contains AOT compiled classes, which is what's causing your headache. I'd file an issue with them to fix and rerelease their jar.

ghadi20:05:52

To get you unblocked, you're going to want to delete the jar from your local .m2 directory, clone their project and do lein install after removing their AOT directives (the direct linking is not the culprit here).

mike_ananev20:05:28

@ghadi thanx for advice

ghadi20:05:05

clojure jars that contain AOT code make lots of headaches

mike_ananev20:05:47

first time met this problem.

sova-soars-the-sora20:05:03

Hi, I'm trying to manipulate an application-state-atom for use with om... and I'm trying to reason about how I can insert-nest comments into the atom... since if the datastructure is right, om-next can just do the recursive query like a champ...

wiseman20:05:21

why exactly does AOT cause headaches? i’ve read that several times but i don’t know the mechanism

sova-soars-the-sora20:05:27

Ahead-of-time Compilation

noisesmith20:05:41

@wiseman when two different clojure versions compile code, they aren’t going to aot compile into 100% compatible results

noisesmith20:05:15

(not to mention problems with generating a new Interface everytime you run defprotocol, and a new class every time defrecord is run etc. leading to weird errors if things are compiled separately or later get recompiled but maybe not everything got recompiled ….)

wiseman20:05:40

ah, that makes sense. thanks!

ghadi20:05:59

It can work fine when AOTing your own project locally. Just never distribute AOT'ed jars unless you are really really careful

shader20:05:49

figwheel seems to be serving the public/index.html file from one of the libraries I use. How do I prevent the resource directories from being merged like that? Or does anyone know of another figwheel setting that can prevent it from using that html file?

mike_ananev20:05:58

@ghadi your advice works! thanks. but additional steps were required: :aot [metrics-statsd.reporter] and point out the same version of clojure that i use [org.clojure/clojure "1.9.0-alpha16"]

ghadi20:05:28

just remove aot entirely

mike_ananev20:05:04

without aot it produces another error:

mike_ananev20:05:23

in ns metrics-statsd.reporter, there some code which requires aot

noisesmith20:05:09

if they do their requires properly they can generate that class without needing aot

lxsameer20:05:00

How can I get the current runtime profile ? I mean i want to know whether I'm running on dev or production ?

jsa-aerial20:05:09

An AOT uberjar seems safe enough to distribute

shader20:05:55

How do I isolate my resource files from those of the libraries I use? Apparently the public/index.html file from one of them is being picked up and served by figwheel since I'm not using one.

shader20:05:25

Do I have to change the name of the directory I use from public to avoid the conflict, or is there a better way to handle that?

noisesmith20:05:39

on the other hand, you can run an app (even an app that needs to be launched from inside another java app) without using aot, thanks to clojure.main already being byte compiled

mike_ananev21:05:47

@lxsameer I use [environ "1.1.0"] to work with different profiles

benny21:05:55

I’m at a loss, I am trying to use (in-ns 'myapp) in a lein repl and nothing can be resolved and there are no errors before the unresolved symbol error when i attempt to use my namespace

benny21:05:04

any common solutions?

benny21:05:59

interesting, why use one versus the other then? (`ns` vs in-ns)

noisesmith21:05:07

in-ns is a low level function that switches to an ns and creates it if it doesn’t exist

tanzoniteblack21:05:14

I don't think I've ever had a legitimate reason to use in-ns, I'm sure they exist...I've just never needed it

noisesmith21:05:39

the specific thing that causes most of the problem is that it doesn’t call clojure.core/refer-clojure - which you can call btw

benny21:05:17

very good to know, thanks guys

noisesmith21:05:34

but usually what you want is to get your namespace loaded from source, which means using require, potentially with the optional :reload arg

kzeidler21:05:39

what's the most idiomatic way to define a private function? letfn? let?

kzeidler21:05:54

I know letfn exists, I scarcely see it used

noisesmith21:05:20

letfn is mostly useful when you want mutually recursive functions that are not at namespace scope

noisesmith21:05:40

you can’t do that in a straightforward way with let

kzeidler21:05:48

Gotcha. I had a hunch it was "special" in some way considering how infrequently I see it 🙂

noisesmith21:05:13

right, let is more flexible, and you can just use fn inside let, that’s usually enough

kzeidler21:05:41

Got it. Thanks once again @noisesmith

tanzoniteblack22:05:38

@kzeidler if you're just defining an ns private function, use https://clojuredocs.org/clojure.core/defn-

jeffmad22:05:13

Hi I am trying to update metrics-clojure which is a wrapper around dropwizard metrics, to v 3.2.2. However, someone added another method name time in Timer. Now there is one method that takes Callable and another that takes Runnable. https://github.com/dropwizard/metrics/blob/3.2-development/metrics-core/src/main/java/com/codahale/metrics/Timer.java#L98 When I run the tests, I get this Reflection warning: Reflection warning, metrics/timers.clj:120:3 - call to method time on com.codahale.metrics.Timer can't be resolved (argument types: unknown). I tried to add a type hint to make it go away, but it didn't work. Anyone have an idea for how to resolve it? `(defn time-fn! [^Timer t ^Callable f] (.time t (cast Callable f)))`

tanzoniteblack22:05:16

if it's a "private" function, as in you want a function only for a particular scope, then let, like @noisesmith recommended

noisesmith22:05:55

@jeffmad I don’t know the right way to do this, but if you wanted to force it to pick a specific method, (reify Callable (call [this] (f)))

zylox22:05:30

also note private is determined by metadata so you can use metadata to mark most things private. though i believe you can still get at the var if you call it directly

noisesmith22:05:35

something defined with defn / fn is both callable and runnable of course

jeffmad22:05:06

ok that is cool. Thank you. @noisesmith that makes it clear since function implements both. I'll try it.

kzeidler22:05:22

@tanzoniteblack Interesting. The function is just a named selector with no meaning outside the context of some accessor function. I was taught in Scheme that any indexical accessor functions should be abstracted to have a name, like (e.g., in Scheme): (define foo cadr).

kzeidler22:05:46

Would defn- or let be more appropriate in that case?

noisesmith22:05:06

I’d use let if it is never meaningful outside the let block

zylox22:05:45

the real answer is to just do everything in one giant let block

noisesmith22:05:31

@zylox clearly the let block should return a single dwim function

zylox22:05:10

i mean preferably avoid return values, ideally the memory artifact after you call some native code to violently kill the jvm will contain the answer you were looking for and can be read up by a simultaneously running program

zylox22:05:23

(ok this monday might have gone a bit too late already...)

kzeidler22:05:38

zylox: so here is the organizational challenge. It's not a huge deal, but it does conflict a little bit with the best practices I picked up in a (Java-based) data structures classes. If make-foo and update-foo are both functions that select a :foo value, then foo can be defined in a let block that declares the selectors for each of those functions. But then of course if the interface changes, then the selectors for every function have to change, making that special ns-only defn- form sound quite appealing

kzeidler22:05:29

I'm just wondering what the conventional way of operating on some nested state-machine-like object is in Clojure, since normally in object-oriented languages these would be scoped to private vars of an object with methods for getting/updating the data 🙂

noisesmith22:05:26

If all make-foo and update-foo do is access :foo on some data structure, then the typical idiomatic thing is to just use a hash map and not define any special functions. If you need something specific that doesn’t match the behavior of a hash-map, then define some deftype that implements the interfaces / protocols that best describe the data structure - use the good parts of OO for what they are good at

noisesmith22:05:08

using let to define a bunch of secret accessors and updaters as a pseudo-object is never really the right thing in my experience

noisesmith22:05:20

if you pick the right interfaces, people can use the existing data functions in clojure seamlessly with your data type

noisesmith22:05:37

this namespace is a great example of implementing a data type for clojure: https://github.com/amalloy/ring-buffer/blob/master/src/amalloy/ring_buffer.clj

kzeidler22:05:15

That makes sense to me. In Scheme it's conventional to use sequences as interfaces, so it quickly gets pretty unreadable if you're not defining a mini-dsl for your abstraction barrier. e.g. "define color as the fourth element of foo". But I guess the idea of deftype is to cast those accessors as static, fixed metaproperties of the data rather than, not another datum within the data. Am I understanding correctly?

noisesmith22:05:58

the accessors are defined on an interface, which is an abstraction that allows polymorphism

noisesmith22:05:30

code will be written to use those interfaces, and thus will simply work with your new data type

noisesmith22:05:52

(as long as your data type is well behaved for the expectations of those interfaces, of course)

noisesmith22:05:07

your type is known to implement certain interfaces, and that indicates that the methods making up the interface (including accessors etc.) are available

noisesmith22:05:39

:frown: defonce still doesn’t work with a doc string

kzeidler23:05:45

Remind me how to mutate foo? I seem to remember Clojurescript uses destructuring to apply the value returned by inc-b

(def foo {:a {:b 2}})
(defn inc-b []
  (update-in foo [:a :b] inc)