Fork me on GitHub
#beginners
<
2020-11-29
>
David Reno01:11:33

REPL driven development question… When I want to test a line in a function that depends on an argument without calling the function, what do you do to provide a value for the argument? Do you just throw in a ‘def’ or is there a better way?

st3fan02:11:40

I would do a let - doesn’t def create global scope?

st3fan02:11:54

What I often do is pull the line out of the function, and execute it in the repl, wrapped in a let

st3fan02:11:04

Doesn’t always work of course

seancorfield02:11:04

@UBMFBEWKY Put a (comment ,,,) form immediately after the function. Then put (def addend 4) inside that comment and evaluate the def, then you can eval any form inside the function.

👍 1
seancorfield02:11:45

That way you're not modifying the "production code", but your scratch code stays visible and available in your code for future use.

seancorfield02:11:25

And maybe attend one of my online talks in December/January https://corfield.org/blog/2020/11/24/talks-clojures-superpower/ 🙂

David Reno02:11:35

Thanks! I know it’s a bit of a silly question but I figured there was a common way.

David Reno02:11:00

I like the (comment …) version.

seancorfield02:11:08

"Rich Comment Form"

seancorfield02:11:27

(because Rich Hickey does this too)

seancorfield02:11:18

(and definitely not a "silly question" -- getting to a really efficient RDD workflow is something that a lot of people struggle with, because it's not like how you work with other languages)

👍 1
noisesmith19:11:04

also, with code that exists already, it's often useful to insert a tap> call into the defn, then use an add-tap function that captures values for experimentation

noisesmith19:11:24

this is my "stethascope" for examining internals of a running system with legacy code

David Reno20:11:16

I haven’t ventured into debugging running/deployed code. Tap is new to me, I’ll read about it. Maybe @U04V70XH6 will cover such wizardry in his upcoming talks? Thanks for the suggestion.

seancorfield21:11:53

@UBMFBEWKY I will make sure I cover that (since I use Reveal alongside my editor to show all tap>'d values).

👏 1
yubrshen05:11:37

How can fix the error:

clojure -M:uberjar
Compiling yubrshen.unique-errors ...

Compilation of yubrshen.unique-errors failed!

Could not locate yubrshen/unique_errors__init.class, yubrshen/unique_errors.clj or yubrshen/unique_errors.cljc on classpath. Please check that namespaces with dashes use underscores in the Clojure file name.
I'm use an app template from: https://github.com/seancorfield/clj-new Here is the related alias for :uberjar:
:uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.1.128"}}
            :main-opts ["-m" "hf.depstar.uberjar" "unique_errors.jar"
                        "-C" "-m" "yubrshen.unique-errors"]}
It seems that I may need somehow to install unique_errors.jar? Thanks!

Brian Chevalier06:11:06

What are the file/folder names used? It looks like a classpath error. Your namespace yubrshen.unique-errors includes a dash which is okay, but your filenames should uses underscores, replacing the dashes.

yubrshen17:11:59

@U017BNGT998 thanks for helping! yubrshen.unique-errors namespace is not of my own, it's prescribed by the alias of :uberjar, which is generated by clj-new template for app. My own namespace is just yubrshen.friendwall The error happened when I created the jar file for friendwall.jar (I don't even know the purpose of yurbrshen.unique-errors) Should I implement somehow <project-parant-folder>/friendwall/yubrshen/unique_errors.clj ?

yubrshen18:11:33

Sorry, the error is caused by my mistake! unique_error was an old project of mine, that I forgot to change it to the new project frendwall, when I copy and paste the deps.edn

🎉 1
😄 1
Lukas16:11:26

Hello everyone, is there a way to get the query result without the brackets?

(d/q '[:find (pull ?e [:commit/rev])
         :in $ $date
         :where
         [?e :commit/date ?date]
         [(> ?date $date)]]
       @conn date)

;; => ([#:commit{:rev "25ed6cb"}]
;;     [#:commit{:rev "02a760a"}]
;;     [#:commit{:rev "61df934"}]
;;     [#:commit{:rev "d6524ab"}])
Like this but without the extra step.
(->> result
       (reduce (fn [acc e] (conj acc (first e))) '()))

;; => (#:commit{:rev "4e2cce7"}
       #:commit{:rev "13c57d1"}
       #:commit{:rev "8a3fd52"})

oxalorg16:11:30

@lukas.block you can try doing this if it helps:

(map first result)
;; or
(->> result
     (map first))

Lukas16:11:30

hey thank you. The reduce was just an, I have to admit overly complicated example, for an extra step

dpsutton16:11:11

I believe there’s a way to do it in the query without extra work afterwards

st3fan18:11:11

What is a good tool to ‘make 150 http requests (random order) with N workers’ ? Should I reach out to java.util.concurrent?

st3fan18:11:35

Going to do now with what I am familiar with .. java.util.concurrent.ExecutorService but I am interested in learning more about clojure.async which also seems to be able to do this

Ben Sless20:11:44

Client libraries like httpkit allow you to pass an optional thread pool (number of workers). You can then use core.async's async-pipeline to control the number of inflight requests

st3fan20:11:07

I need to investigate that. Right now I have a simpler approach:

(defn ping-endpoints [endpoints]
  (let [pool (Executors/newFixedThreadPool 8)
        tasks (for [url endpoints] #(ping-endpoint url))
        futures (.invokeAll pool tasks)]
    (for [f futures] (.get f))))

Ben Sless04:11:30

Beware that for is lazy. Laziness and side effects don't mix well together. I recommend either coercing it via doall or using mapv. Also take a look at https://github.com/TheClimateCorporation/claypoole

st3fan14:11:42

That looks very nice. I'll take a peek.

st3fan21:11:46

I wish I could just keep all data in memory and not bother with databases

andy.fingerhut21:11:41

You don't care if your data disappears when the power goes off?

andy.fingerhut21:11:19

I believe quite a few databases are good at keeping recently used data cached in RAM.

st3fan00:11:06

Sorry - what I mean is that I wish I could just Clojure data structures and not bother with SQL at all

st3fan00:11:37

If they would somehow magically persist to disk

uosl21:11:50

Isn't this basically datomic?

zackteo21:11:58

Apparently the 4clojure server is down hmmm

zackteo21:11:02

Anyway, am doing qn 39 have a solution to make my own version of interleave but is there a way I can remove vector ? Like by changing concat to something else?

(defn leave [a b]
  (when (and (not-empty a) (not-empty b))
    (concat (vector (first a)) (vector (first b))
            (leave (rest a) (rest b)))))

dpsutton21:11:13

I think you can use list* there

zackteo21:11:51

yeap list works too but was wondering if there's a concat that doesn't require elements to be in some form of collection

dpsutton21:11:18

List* is that

dpsutton21:11:01

Distinct from list. Often you’ll see lazy-seq cons cons leave

zackteo21:11:12

oooo! yeap it works

zackteo21:11:39

Also does anyone understand why mapcat vector works as a replacement for interleave ?

zackteo21:11:50

Okay nvm I got it, but I never knew that map applies the f to the first item of each coll then the second items and so on

dpsutton22:11:39

yeah its a neat feature of map. other languages need a bespke map2, map3, etc