Fork me on GitHub
#beginners
<
2018-03-08
>
seancorfield00:03:59

Since :id will either be present or not, just testing (:id item) is all you need.

seancorfield00:03:49

But for group-by you need to detect presence so (group-by #(contains? % :id) seq-of-items) should be sufficient.

seancorfield00:03:23

That will produce a map with two keys (`true` and false) and the value of the true key entry will be a sequence of all the maps that contain an ID and need to be updated, and the value of the false key entry will be a sequence of all the maps without an ID that need to be bulk-inserted.

mfikes00:03:44

I suppose, if the sequence is gigantic, and you want to be lazy, and the values under :id are truthy, then (filter :id seq-of-items) and (remove :id seq-of-items) would be another way to produce the two lists

Will00:03:00

Thanks guys, now how would I filter all the ones where id = 0 or something like that?

Will00:03:48

I figured it out with group-by (group-by #(true? (= (:d %) 0) seq-of-items) but how would I do that with filter or remove?

noisesmith00:03:24

@josmith2016 that call to true? can be removed

noisesmith00:03:38

and any (= x 0) can (and should) be replaced with (zero? x)

noisesmith00:03:17

@josmith2016 to do that with filter or remove, use the same function you provided to group-by

noisesmith00:03:33

(remove f seq-of-items)

Will00:03:06

Thanks @noisesmith I must have typed something wrong the first time I tried it, it worked this time

noisesmith00:03:54

so with both my suggested changes your function does the same thing but is clearer #(zero? (:d %))

Will00:03:44

That is a lot clearer thanks alot!

Wilson Velez01:03:59

Hi, is there a uri predicate for clojurescript?

mathpunk02:03:35

I tried this, but while I get a REPL, I can't seem to exit it with :repl/quit

mathpunk02:03:30

(also the repl captures my input, it's not like my usual CIDER repl.... but, one step at a time)

mathpunk02:03:13

If there's more examples of using sub-repls, I'd love to read them

mathpunk02:03:24

but this talk doesn't quiiiiite have enough information for me

nakiya05:03:12

Perhaps I don’t understand secretary well. I have an anchor like this:

[:a.btn.btn-primary
                      {:on-click #(do
                                    (println i)
                                    (secretary/dispatch! "/device"))}
                      "Details"])))
My route before I click the anchor is . After I click, it’s still the same. But the device component gets displayed. Because of this, The home component (corresponding to gets hidden. Does this make sense? How to actually go to when anchor is clicked?

nakiya05:03:40

If I do instead:

[:a.btn.btn-primary
                      {:href "/device"} "Details"]
It’s fine

pooboy06:03:43

Can clojure be used to write simple scripts

seancorfield06:03:17

@suryapjr Yes, but the JVM has a bit of a startup overhead. If you use clj, that is minimized.

justinlee06:03:18

@duminda what you are attempting to do looks correct to me. Is that the code you are actually using? I only ask because you have a (println i) in the click handler but there’s no i declared anywhere

nakiya06:03:13

@lee.justin.m: Yes, part of the actual code. The println is just for debug puropses.

justinlee06:03:38

yea sorry i can’t see why that wouldn’t work. i actually use accountant and secretary together, so I call accountant/navigate, but they function similarly, and if the href works the dispatch! should work

nakiya06:03:34

@lee.justin.m: Ok, I will have a look again.

pooboy06:03:46

@seancorfield cool !! Thank you !!

pooboy06:03:12

I prefer .clj than Lein most of the time

pooboy09:03:42

` (def guess (.Random.)(nextInt 100))

pooboy09:03:18

The above code is giving me too many arguments to def

orestis09:03:16

You are passing two forms to it.

orestis09:03:18

Correct form is

(def guess (.nextInt (java.util.Random.) 100))

orestis09:03:08

(java.util.Random.) creates the random instance, then (.nextInt <instance> 100) calls nextInt on it with n=100

pooboy09:03:44

Ohhh.. Thanks orestis !

pooboy09:03:57

Sorry for the late reply..saw Ur text now

pooboy10:03:07

(ns magic)
 (defn -main[]
(println "Lets generate a random number")
 (import java.util.Random)
 (def magic (.nextInt (java.util.Random.)100) )
 println(magic)
 ) 

pooboy10:03:31

The above code gives me : ClassCastException java.lang.Integer cannot be cast to clojure.lang.IFn magic/-main (core.clj:7)

firthh10:03:03

So that error is saying your trying to call something as a function but it’s not a function

firthh10:03:21

Specifically you’re using an integer somewhere that it’s expecting a function

pooboy10:03:06

I fixed the parentheses in the println magic line

pooboy10:03:20

But then too its showing the same error

pooboy10:03:32

I want to print the magic variable

sundarj10:03:17

$ clj
Clojure 1.9.0
user=> (def magic (.nextInt (java.util.Random.) 100))
#'user/magic
user=> (println magic)
30
nil

orestis10:03:56

@pooelfbin Note that def will make a “global” variable. If you want to define variables inside a function, you should use let:

(let [magic (.nextInt (java.util.Random.) 100)]
  (println magic))

pooboy12:03:43

My code :

(defn clojurify [def a]
(import java.util.Random)
(.nextInt (java.util.Random.) a))

pooboy12:03:04

I call this function with : (clojurify 100)

pooboy12:03:39

But I get : wrong number of args (1) passed into to user/clojurify

pooboy12:03:07

I have already defined the arity as a ,right ?

tbaldridge12:03:44

you have 'def a' in the arg list

tbaldridge12:03:46

why the def?

pooboy12:03:17

Variable a

tbaldridge12:03:32

it's just a, no def needed in Clojure

antonio12:03:40

the clojurify function has 2 args: def and a

pooboy12:03:42

Okay got it ..

pooboy12:03:50

I'm going crazy :D

pooboy12:03:36

Guys im building the above code into a library

pooboy12:03:01

And i wanted to know in the project.clj - :url , Do i have to create a github repo and paste the link there aur lein will automatically publish it with after pasting the link with lein deploy

antonio13:03:05

maybe you could just use rand-int, take a look: https://clojuredocs.org/clojure.core/rand-int

tbaldridge13:03:40

@suryapjr no, the :url is just metadata it doesn't even have to be there

pooboy13:03:52

@tbaldridge cool!! Thanks a ton!

dazld13:03:48

this might not be a total beginner question, but googling isn’t helping me much - so.. 🙂

dazld13:03:56

i’m trying to stub out a nested require in a test

dazld13:03:13

with node projects, i’d use something like proxyquire

dazld13:03:20

but i’m not sure what the equivalent is in clojure

dazld13:03:23

any tips?

dazld13:03:04

for example, i have a function which performs a db action, and i’d like to be able to override that particular require with some mock

dazld13:03:23

so I can test just the logic in the surrounding function

ghadi13:03:12

There are ways of mocking and stubbing calls to vars, but it's not a generally recommended approach. Look at with-redefs in core

dazld13:03:20

thank you

val_waeselynck14:03:39

@dazld this is probably a sign that you should hide your db behind a protocol.

dazld14:03:00

I’ve not used protocols yet

dazld14:03:11

could you point at an example..?

Russ Olsen14:03:45

The idea of a protocol is that you define a bundle of operations and give it a name. Then you can implement that bundle for different data types. Sort of like interfaces in Java, if you know Java.

dazld14:03:24

aah, ok - so, would be able to pass it a mock connection, for example, and leave the rest alone

Russ Olsen14:03:52

If I'm following you correctly, yes.

dazld14:03:53

(I don’t know java)

timo14:03:58

Hi, I would like to use this JS-lib in my re-frame-app (https://github.com/graphhopper/directions-api-js-client/blob/master/src/GraphHopperOptimization.js). How do I resolve the js-deps in my re-frame project? Would you use npm for that? I guess I would have to disable advanced compilation then, right? Would you rather implement it yourself? (I am quite new and it would take me a while)

Russ Olsen14:03:59

(good for you)

dazld14:03:41

that sounds like something to try next - but with-redefs as @ghadi mentioned is working great

Russ Olsen14:03:45

Yeah with-redefs is fine for tests, I think if you were looking for a more general solution then protocols or multi methods are the way to go.

val_waeselynck14:03:12

Major rework of my draft of a Clojure REPL guide for http://clojure.org. Feedback from beginners is still very welcome 🙂 https://clojureverse.org/t/feedback-wanted-draft-of-a-clojure-repl-guide-soon-to-be-submitted-on-clojure-org/1703

joelv14:03:39

Hey guys, I have compojure-api project which I'm using the lein ring plug in for developement. However, because of they way our paas is set up, I can't use lein ring uberjar but only lein uberjar. So I had to include a main.clj in my source for lein uberjar. I'm having trouble having with the setup for the uberjar file to include the main.clj in the resources/production directory.

(defproject ss-prescribers-spi-api "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.9.0"]
                 [metosin/compojure-api "2.0.0-alpha18"]
                 [com.auth0/jwks-rsa "0.3.0"]
                 [com.auth0/java-jwt "3.3.0"] 
                 [clj-time "0.14.2"]
                 [org.clojure/tools.logging "0.4.0"]
                 [clj-adapters "0.1.20-SNAPSHOT"]
                 [clj-fhir-lib "0.1.08-TEST-SNAPSHOT"]
                 [org.apache.logging.log4j/log4j-slf4j-impl "2.10.0"]
                 [org.apache.logging.log4j/log4j-api "2.10.0"]
                 [org.apache.logging.log4j/log4j-core "2.10.0"]
                 [jsonevent-layout "2.1-SNAPSHOT"]
                 [io.sentry/sentry-log4j2 "1.7.0"]
                 [slingshot "0.12.2"]]
  :ring {:handler api.handler/app
         :auto-reload? true}
  :uberjar-name "server.jar"
  :profiles {:dev {:dependencies [[javax.servlet/javax.servlet-api "3.1.0"]
                                  [cheshire "5.8.0"]
                                  [ring/ring-mock "0.3.2"]]}
             :uberjar {:aot :all
                       :source-paths ["resources/production"]
                       :main main
                       :dependencies [[ring/ring-jetty-adapter "1.6.3"]]}}
  :plugins [[lein-ring "0.12.0"]
            [s3-wagon-private "1.1.2"]]
  :repositories [])

joelv14:03:41

when lein uberjar runs it doesn't include the main.clj under resource/production

joelv18:03:04

I figured out my own issue I moved the main.clj file to another directory...it didn't like the resources directory

pooboy14:03:17

random.nextInt(max - min + 1) + min 

pooboy14:03:32

How should i implement this in clojure

mfikes14:03:58

The interop would be pretty easy, but I'd suggest taking a look at the core rand-int fn

pooboy14:03:12

@mfikes coool..ill check it out

mfikes14:03:50

The interop, if interested is (+ (.nextInt random (+ max (- min) 1)))

mfikes14:03:58

But, rand-int is much easier 🙂

mfikes14:03:49

It should be (+ min (rand-int (- max min)))

mfikes14:03:59

Maybe off by one 🙂

pooboy14:03:33

great stuff @mfikes..ill stick with interop..want to make a simple clojure wrapper around the java.util.Random for the beloved community

ghadi15:03:50

@dazld what @russ767 suggested is a much better approach. Hide your interactions behind polymorphism, then pass around the implementations explicitly

dazld16:03:04

but, it’s still useful to know the why

dazld16:03:16

and that with-redefs exists

rgm01:03:40

Let’s assume I don’t entirely understand yet, but isn’t the race condition in the 2nd last paragraph that with-redefs has gone out of scope by the time the future looks up the var, and that the root is reset to 42? The text says reset to 0.

dazld16:03:56

that’s super helpful!

Russ Olsen16:03:31

Yeah, while I was in the middle of writing Getting Clojure I realized that I didn't actually understand how Vars worked and spent a couple of weeks figuring it out. I think that is a common experience even with folks who have been doing Clojure for a while. Of course @tbaldridge published that article about 5 minutes after I figured it out on my own. Thanks Tim!

nakiya17:03:12

Ok, added [cljsjs/chartjs "2.7.0-0"]] dependency in project, required [cljsjs.chart]. Then ran lein cljsbuild once and I get this error inside the stacktrace:

Caused by: clojure.lang.ExceptionInfo: No such namespace: cljsjs.chart, could not locate cljsjs/chart.cljs, cljsjs/chart.cljc, or JavaScript source providing "cljsjs.chart" in file src/cljs/ppserver/views/device.cljs {:tag :cljs/analysis-error}

nakiya17:03:59

Any pointers to what I should look at?

athomasoriginal18:03:59

@russ767 do you cover vars in detail in your book? I spent a whole day just asking questions in clojurians and got a ton of helpful answers, but never feel like I got to the core of it 😞

schmee18:03:15

The Joy of Clojure has a great chapter which covers all the reference types (atoms, agents, refs and vars), that chapter cleared up the confusion for me once and for all

Russ Olsen19:03:41

@tkjone Chapters 8 and 9! 8 Covers vars, 9 covers namespaces.

Shaitan20:03:10

how much slurp can slurp? is there a limitation on size ? :thinking_face:

seancorfield20:03:43

@tacesofoca available memory and/or maximum length of a Java String.

seancorfield20:03:20

(but if you slurp in a very large file you may not have enough memory left to actually process it)

mathpunk23:03:45

My current project is grabbing some data out of a web service I've used, and cleaning/validating/classifying it. Later I intend to load it into Datomic to start doing experiments but for now I'm just saving it to flat files so it's hosted locally. I'm trying to decide how to handle some characters that, on the service, are rendered as "question mark in a diamond." I know this has to do with string encodings; it is messing up my pipeline somehow, but only for a dozenish records. Can someone point me at where I can learn enough to decide if it's worth caring vs just dropping a bit of data?

mathpunk23:03:02

Bytes and encodings have caused me many problems before, but that was Java. Maybe there's a nicer solution