Fork me on GitHub
#clojure
<
2017-12-19
>
practicalli-johnny00:12:56

@qqq Clojurescript projects and tools like https://github.com/anmonteiro/lumo will connect (transpile) your Clojurescript code to a node.js host. You can also call node.js libraries.

ihabunek09:12:25

hm, i enabled reflection warnings on my project and i'm getting one i don't know how to interpret:

Reflection warning, /tmp/form-init4283907053113722510.clj:1:903 - call to static method invokeStaticMethod on clojure.lang.Reflector can't be resolved (argument types: unknown, java.lang.String, unknown).
any ideas? can i get a full stack for that warning?

qqq09:12:08

do reflection warnings happen at compile time or run time?

bronsa09:12:52

compile time

qqq10:12:32

@bronsa that's what I thought too, the 'full stack for that warning' confused me

qqq10:12:29

(defn rnd-f-arr [n]
  (let [random (java.util.Random.)
        x      (float-array n)]
    (doseq [i (range n)]
      (aset x i (.nextFloat random)))
    x))

(seq  (rnd-f-arr 2))


^-- help shortening the above: I think it can be made more idiomatic

qqq10:12:33

hmm, I get:

(defn rnd-f-arr [n] 
  (let [random (java.util.Random.)]
    (float-array n (repeatedly n #(.nextFloat random)))))
is this still as efficient, or does 'repeatedly' cause performance problems

qqq11:12:10

(aset (float-array [0 0 0 0]) 2 2) <-- what am I doing wrong in this code? I'm passing it an array, an index, and a number

bronsa11:12:28

2 is not a float

bronsa11:12:38

(or a double)

qqq11:12:30

yeah, changing it to 2.0 made it work

qqq11:12:04

is there a way to "unquote" format strings? for macros, we can do

`( .... ~(...))
now, for format strings, we can do (format "v: %s, i: %s" v i) ... but is there a way to "unquote" where the v/i are placed right at the %s ?

qqq11:12:32

(ns a.behemoth
  (:import [jcuda.jcublas.JCublas]
           [jcuda])
  (:require [a.atlantis :as a]))

(jcuda.jcublas.JCublas/cublasInit) 
is there a way I can shorter the jcuda.jcublas.JCublas/cublasInit

danm11:12:59

(ns a.behemoth
  (:import (jcuda.jcublas JCublas)))

(JCulbas/cublasInit)
?

qqq11:12:14

ah, that works; thanks!

qqq11:12:21

so 'classes' can be imported as 'namespace' ?

danm11:12:17

That's how we've always done our Java interop imports. The ( vs [ is just a weird Clojure convention IIRC, imports normally use ( but requires are [. It's the space instead of dot that makes it work. I couldn't tell you why it works though

zignd13:12:00

in clojure.spec, do you guys have any idea of how i could deref a value before checking it?

(s/fdef foo!
  :args (s/cat :arg1 (s/coll-of (fn [v] (instance? Bar v)))))
arg1 in foo! is a reference value containing a collection, i'd like to deref it so that i can check it with coll-of

Alex Miller (Clojure team)13:12:07

You could use s/and an s/conformer that calls deref

Alex Miller (Clojure team)13:12:10

Although i find it’s usually best to stop passing atoms around as much as possible and instead write your function on the data inside

Alex Miller (Clojure team)13:12:09

To get to@the point where all the logic that is worth testing operates on data, not state. Then you don’t need to spec the stateful functions

zignd13:12:23

oh nice, i will try that! thanks alex! by the way, it's an exercise in which i'm simulating database collections as references, so whenever i need to query the data i use this reference + core.logic. i thought it was a good idea at the time

Alex Miller (Clojure team)13:12:57

Which is a little dicey

plins13:12:09

hello everyone I’m trying to generate a fat jar (leiningen) of a project which will run some jobs and die, and I have some module levels declarations with def that accesses the database, something like (def sms-body (get-msg-from-db "smsInviteText")) but when I run lein uberjar it tries run those commands upon compilation, is there a way to avoid that?

Alex Miller (Clojure team)13:12:40

Yeah, don’t do that :)

plins13:12:35

how? id like to have this value as a constant visible to all functions of the module

Alex Miller (Clojure team)13:12:07

Make that a function, invoke it at runtime, and pass it around

Alex Miller (Clojure team)13:12:43

I know that sounds like a pain in the ass, but you will probably be thankful a year from now

Alex Miller (Clojure team)13:12:20

The short term fix would be to wrap it in delay and then deref it where it’s used

danm13:12:31

I mean, you could have a function you invoke at startup that puts the result in a def'd atom, then just access the atom whenever you need the value, but that's a very un-Clojureish way to do it

plins13:12:06

I ll just pass it around i guess 🙂 thx

djebbz14:12:00

Hello, question about pedestal : is it possible to have :constraints and :path-params for static routes, i.e. routes that don't have any placeholder like :id etc. ?

donaldball15:12:03

FWIW there is a #pedestal room which be a better place to ask

djebbz15:12:09

oh thanks, didn't know

djebbz14:12:02

My use case is that I'd like to pre-generate all routes of my app (several millions, I think 2~5) since I know them in advance, but still be able to differentiate them in the interceptors. Some are product pages, others are listing pages. Several of them will also have query-params attached to them (like ?productid=X)

djebbz14:12:18

Or maybe I need to do something else once I generate the routes ? I can't find enough help in pedestal docs/tests/source as to what to pass to expand-routes (to be more precise, to some function that will call expand-routes).

markbastian15:12:53

Hey folks, I'm trying to get gen-class working and I can't seem to get the syntax right for passing an array of Strings. I've tried the following (and then some). I'm sure the actual answer is pretty simple, but I haven't found it yet. Can someone help me out? Thanks!

;Nope
(:gen-class
  :methods [#^{:static true} [foo [[Ljava.lang.String;] int]])

;https://groups.google.com/forum/#!topic/clojure/TFLUw8GSAbY
(:gen-class
  :methods [#^{:static true} [foo [#^"[Ljava.lang.String;"] int]])

;Another guess
(:gen-class
  :methods [#^{:static true} [foo [(Class/forName "[Ljava.lang.String;")] int]])

josh_tackett15:12:15

Anyone know a good python slack team? or anyone here know python?

bwstearns15:12:36

I can help with Python

luskwater17:12:54

@markbastian Corrected:

;https://groups.google.com/forum/#!topic/clojure/TFLUw8GSAbY corrected
(:gen-class
 ;; Remove spurious hash-and-caret (#^) in front of type definition
  :methods [#^{:static true} [foo ["[Ljava.lang.String;"] int]])
Now, incidentally, I haven’t been able to get a static definition to work…

noisesmith18:12:47

you can replace #^{:static true} with ^:static iirc

luskwater18:12:00

True, but let’s try one more time (@markbastian)

(ns sandbox.HelpOut
  (:gen-class
   :name sandbox.HelpOutClass4
   :main false
   :methods [
             ^:static [foo ["[Ljava.lang.String;"] int]
             [bar ["[Ljava.lang.String;"] int]
             ]))

(defn -foo
  "This is declared `static`"
  [some-strings]
  (let [how-many (count some-strings)]
    (doseq [[i x] (map-indexed #(vector %1 %2) some-strings)]
      (println (format "(%d): %s" i x)))
    how-many))

(defn -bar
  "This is an instance method"
  [_ some-strings]
  (let [how-many (count some-strings)]
    (doseq [[i x] (map-indexed #(vector %1 %2) some-strings)]
      (println (format "(%d): %s" i x)))
    how-many))
The ^:static notation goes before the whole vector that describes the method

bronsa18:12:16

also #^ is old deprecated syntax, just ^ will do

dominicm18:12:47

Why was it deprecated? I quite like how special things use #

bronsa18:12:00

because ^ is shorter

bronsa18:12:08

originally #^ was what ^ is now

bronsa18:12:20

and ^ was reader syntax for (meta

bronsa18:12:59

it was later changed so that ^ is syntax for with-meta like #^, with ^ being the preferred syntax

dominicm19:12:40

That seems different to other of clojure's design choices, especially the recent ##NaN

bronsa19:12:30

they're completely different scenarios

dominicm19:12:05

Using # for all special syntax seems consistent. Having ^ be an exception seems inconsistent.

bronsa19:12:14

I'm sure you wouldn't like to write #@ either

bronsa19:12:30

there's a number of reader macros that are not routed through the # dispatch

bronsa19:12:59

the reason why we went for ##NaN and not NaN is that Rich didn't want to pollute the namespace of special symbols

bronsa19:12:13

but in this case ^ was already reserved as a reader macro

bronsa19:12:38

we use # as a dispatch macro mostly just because (good) top level symbols were running out

dominicm19:12:16

That's true, I hadn't considered @. I'd be happy to move to #@ though. I think I've grown fond of consistent syntax.

bronsa19:12:47

what about :, ` ' ; ~ etc

bronsa19:12:55

there's tonnes of top level reader macros

bronsa19:12:06

and there's absolutely no reason for them to be prefixed

dominicm19:12:38

There's much less sanctity of # than I realised when making the comment. I think I've seen comments valuing that clojure has concise syntax for many things, it makes sense in that context.

dominicm19:12:14

I still struggle to picture myself wanting #^ to be shorter.

bronsa19:12:38

you've never used ^foo or ^:foo then I assume? ;)

bronsa19:12:18

here's the thing, #^ didn't become ^ because it was shorter, rather, we realized that ^ as a reader macro for (meta had virtually no use, while with-meta had quite a bit of use

bronsa19:12:47

so it was decided to repurpose ^ to be the more useful with-meta, and #^ was kept for backwards compatibility

dominicm19:12:25

I see, that's a more interesting growth. I can't actually see a particular purpose for the old ^ symbol. I have never used those much, no. They don't seem to come up in normal use of clojure, for me. Not enough that I'd want to shave a character off. Generally speaking, outside of functions and arguments, I don't see any use of metadata in clojure code. Arguments cited usually involve losing metadata under certain cases, and being concealed.

markbastian18:12:18

Awesome, thanks for the help!

luskwater18:12:27

I was glad for the (re-?)education, thanks for the opportunity

luskwater18:12:58

(And, as for “old deprecated syntax”: some learn from older books, like those in former Colonies learning English from books from the 1880s; some of us are old enough [in Clojure] that that syntax was once our native tongue, and we have to learn your newfangled slang.)

bronsa18:12:13

I believe ^ has been the preferred way since 1.2, so for quite a long time :)

markbastian19:12:01

This has been my fallback link every time I forget how to do this https://stackoverflow.com/questions/2181774/calling-clojure-from-java and it has "Ye Olde English" style. Perhaps an updated example on https://clojure.org/reference/java_interop would set things aright.

seancorfield20:12:22

@markbastian An alternative to generating classes in Clojure is to use the Java API to require and invoke code (it's the approach we've used at World Singles) -- see Alex Miller's answer to that SO question https://stackoverflow.com/questions/2181774/calling-clojure-from-java/23555959#23555959

sggdfgf20:12:04

could someone please advice? I want to load namespace every time I run "lein repl", but not on other tasks...

Drew Verlee20:12:46

Are there any clojure SQL libs that just make every table lazily available? Something like (take 2 (:customers tables)) => [{:name "drew"} {:name "sally"}]

arrdem20:12:54

@faxa one traditional answer to this is to add :profiles {:dev {:source-paths ["dev/"]}}, and then put the code you want to load in dev/user.clj where Clojure will load it automatically for you.

sggdfgf20:12:50

@aardem , but won't dev/user.clj load on all other tasks as well?

sggdfgf20:12:27

I wish there were special profile for repl...

seancorfield20:12:26

@drewverlee I can think of all sorts of ways that would be problematic for resource usage (connections etc). Also, what would it mean to lazily process a table while updates were made to that table in other threads or processes?

seancorfield20:12:27

About the closest you can probably get is building some sort of abstraction on top of reducible-query (from clojure.java.jdbc) because you could use sequence and/or eduction to produce sequences of rows from tables... (off the top of my head).

Drew Verlee20:12:15

Good input @seancofield thanks. hmmm

sggdfgf21:12:12

that's mess for a beginner... I am trying to extend the :repl profile ` :dev-repl {:repl {:source-paths ["src" "env/dev"] :main "env.figwheel-api"}}` but fails with Warning: no nREPL dependency detected.

qqq21:12:58

;; (wrap cublasSasum 3) -> #(JCublas/cublasSasum %1 %2 %3)
;; (wrap cublasSaxpy 4) -> #(JCublas/cublasSaxpy %1 %2 %3 %4)

(defmacro wrap [fname n]
  (let [args (vec (take n gensym))
        jname ...]
    `(fn ~args
       (~jname ~@args))))
what do I put in the ... ? JCublas = not a clojure namespace, but a Java class

bronsa21:12:18

(symbol "package.to.Jcublas" (name fname))

sggdfgf21:12:06

Is it possible to launch REPL with different Leiningen profile? Is it only my setup? :face_with_rolling_eyes:

noisesmith21:12:18

two options: lein with-profile +foo repl - that merges the foo profile in; lein with-profile foo run -m clojure.main ... that starts a vanilla repl with only the foo profile (no nrepl btw)

sggdfgf21:12:54

thank you very much!

sggdfgf22:12:40

Can I run lein repl with different user.clj specified? ideally cpecified via Leiningen's profile.clj...

noisesmith22:12:00

wouldn’t putting a different directory on the front of the classpath (source-paths) so that the user.clj there is found first do the trick?