Fork me on GitHub
#beginners
<
2021-09-01
>
popeye07:09:16

TIll when future will holds its result?

Ed09:09:43

A future will stay in memory until it can be garbage collected. So if you no longer have a reference to it, the gc process will free the memory for you.

👍 2
popeye07:09:43

I have seen below code in compojure

(defn- wrap-response [handler]
  (fn
    ([request]
     (response/render (handler request) request))
    ([request respond raise]
     (response/send (handler request) request respond raise))))
In above code, we are not passing any values to the request, only handler is a single argument, What values go inside request here and called function is
(-> handler
      (wrap-response))

Ed09:09:51

Yes, the wrap-response takes a handler, and returns a modified handler. A handler is a function that takes a request and returns a response. When you start the web server (with something like run-jetty) you pass it the handler that will be called for each request that comes in over the network. The request is a map that will conform to the ring spec (which is here: https://github.com/ring-clojure/ring/blob/master/SPEC) so if you want to test the handler, you can create a request pretty easily with a literal data structure. Maybe something like:

(is (= 200 (:status (handler {:uri "/ping"}))))
Make sense?

popeye11:09:29

I wanted to know how it calls line (response/render (handler request) request)) , we are not passing any value to request right? and passing single argument handler

popeye11:09:17

in the below code

(defn- wrap-response [handler]
  (fn
    ([request]
     (println (handler request) request))
    ([request respond raise]
     (println (handler request) request respond raise))))


(defn calling-extend-protocol [x]
  (wrap-response "abc"))


(calling-extend-protocol "df")
how it calls the println method ?

popeye11:09:49

how clojure calling these functions

Ed11:09:01

when you run (-> handler (wrap-response)) you get a function back. You pass that function to jetty and it is responsible for running it. If you want to run it yourself, you need to get that function and call it

(let [h (-> handler wrap-response)]
  (h request))
in your code, this
(defn- wrap-response [handler]
  (fn
    ([request]
     (println (handler request) request))
    ([request respond raise]
     (println (handler request) request respond raise))))
will not produce a valid handler, since it will return nil
(defn- wrap-response [handler]
  (fn
    ([request]
     (prn 'request-> request)
     (handler request))
    ([request respond raise]
     (prn 'request-> request 'respond-> respond 'raise-> raise)
     (handler request respond raise))))
that will print some debug and then delegate to handler and you can use it like this:
(let [h (wrap-response (constantly {:status 200 :body "success"}))]
  (h {:uri "/request"}))
that will return {:status 200, :body "success"} and print request-> {:uri "/request"} at the repl/terminal/wherever your setup is printing stdout.

popeye12:09:52

That helped! thanks @U0P0TMEFJ

👍 2
noisesmith16:09:07

it might help to see what it looks like to do this to normal code:

(ins)user=> (defn wrap-double-n [f] (fn [n] (f (* 2 n))))
#'user/wrap-double-n
(ins)user=> (def foo (wrap-double-n inc))
#'user/foo
(ins)user=> (foo 1)
3
a middleware modifies some function by making a new version which either pre-process or post-processes values (or does some side effect in there...)

popeye18:09:31

@U051SS2EU Thanks , I understood it now!

Benjamin13:09:49

https://i.imgur.com/TRGy17u.png tyring to invoke clojure from a java (kotlin) app and String/format throws. What am I doing wrong?

Benjamin16:09:45

it was fixed by doing

var req = Clojure.`var`("clojure.core","require")
        req.invoke(Clojure.read("com.github.benjaminasdf.idlelib.compsearch"))
        val fn = Clojure.`var`("com.github.benjaminasdf.idlelib.compsearch", "get-re-str")

Benjamin16:09:02

instead of invoke-static . I was not aware that there is a difference. Can somebody point me towards it?

alexmiller13:09:39

just as a general comment: write the code you ran (in text, use a snippet or triple-back-quote) and the result you got. that will make it easier for people to help

countb16:09:05

I'm a bit naive on this topic, but for ease of Clojure development, is WSL 2 fine? Or is it worth it to set up dual boot on my Windows machine?

stianalmaas16:09:38

I am a beginner myself so I haven't used it in anger but I think WSL2 is really good for Clojure. Especially combined with Vscode and Calva.

porkostomus16:09:35

Yes WSL2 will likely give you the smoothest experience because you'll be able to use the same shell commands as Linux/MacOS. There is also a #clj-on-windows channel if you need more help.

👍 2
Nikhil Warke16:09:59

I tried setting up clojure on my backup windows machine, found no problems so far.

seancorfield17:09:30

My Windows setup is: VS Code on Windows (11) with the Remote-WSL2 extension; WSL2 with Ubuntu running all of my Clojure CLI, REPLs, other stuff. It's a very slick setup. VS Code + Calva is great!

seancorfield17:09:32

I also have Docker for Windows installed (with the WSL2 integration) and run MySQL, Elastic Search, and Redis via Docker on WSL2 (`docker-compose up`) so I only have my Clojure source code and REPL running directly on WSL2.

countb00:09:07

Thank you all for the input and resources. I'm going to work on setting up my dev environment. VS Code + Calva seems neat. When I was originally toying around with Clojure I was trying to learn emacs at the same time which was a bit much for me.

seancorfield00:09:29

@U02CV35U6AH Yeah, I usually recommend folks don't try to learn both Clojure and Emacs at the same time 🙂 In general, if the editor you already know best has a decent Clojure integration, that's the best thing to use.

😄 4
popeye18:09:22

Can any one suggest me link to understand clojure.walk please, I was going through the clojure documentation and I am not able to understand it properly

hiredman18:09:26

have you looked at the source?

quoll18:09:51

I feel like this is a lot to ask of someone in the #beginners channel.

hiredman18:09:42

1. Asking someone if they have read the source, and even suggesting that they read the source is not insisting that they must read the source

hiredman18:09:45

2. Reading source is a fantasticly useful thing to do at any skill level

kpav18:09:27

Hey all, not sure if this is the right place to ask, but can anyone recommend some good Clojure talks for someone newish to clojure? Ive seen a few but wondering if there are specific ones I should watch as a newbie

hiredman18:09:25

I have a list I was building, but haven't updated in a while, not all clojure specific, but a good helping of rhickey talk's peppered in:

#+TITLE: Software, a Deconstruction
1. 
2.  Simple Made Easy
3. 
4. 
5. 
6.  (Engineering(,) A Path to Science: "I don't want to die in a language I can't understand")
7. 
8.  (Jonathan Blow on Software Quality)
9.  (Reasoning about performance)
10.  (The Coming War on General Computation)
11. 
12.  (What, Where And When Is Risk In System Design)
13. 
14.  (Creativity In Management)
15.  (Hammock Driven Developement)
16. 
17. 
18. 
19. 
20. 
21.  (Are We There Yet - Rich Hickey)

👀 2
kpav18:09:54

Thanks for the responses everyone! Got some good stuff to watch over memorial weekend now 🙂

Carnun18:09:35

Actually, I was thinking of https://youtu.be/gIoadGfm5T8 !

👀 2
kpav18:09:46

Y’all are awesome, thanks again

👍 2
practicalli-john03:09:38

Clojure 10 big ideas video is excellent https://practical.li/clojure/concepts/

👀 2
hiredman18:09:35

https://github.com/clojure/clojure/blob/master/src/clj/clojure/walk.clj 130 or so lines, walk is the main function, everything else builds on it

Harshit Joshi19:09:36

hey everyone, i am a beginner

(defn func
	([] sentence)
	([name] (str sentence name)))

(func "harshit")
is this code wrong ?

Russell Mull19:09:52

Can you describe what sentence is doing?

Harshit Joshi19:09:16

(def sentence "And the Golden Grouse And the Pobble who")

Harshit Joshi19:09:29

sentence is just a string

Russell Mull19:09:50

In that case, the code looks reasonable to me. Why are you asking?

Harshit Joshi19:09:12

hmm i tried running it with vscode clojure extension

Harshit Joshi19:09:19

but it gives me an error

Harshit Joshi19:09:58

specifically a syntax error compiling

Russell Mull19:09:21

what's the error?

Harshit Joshi19:09:33

(ns markov-elear.generator)

(def sentence "And the Golden Grouse And the Pobble who")

(defn func
	([] sentence)
	([name] (str sentence name)))

(func "harshit")

(defn -main
  "I don't do a whole lot."
  [x]
  (println ( str x "Hello, World!")))



;; keyword? functions
(odd? 2)  ; false
(even? 2) ; true

;; long short signed unsigned int
;; double float

Harshit Joshi19:09:44

is there something wrong in the entire file ?

dpsutton19:09:58

what's the error?

Harshit Joshi19:09:19

it says syntax error compiling at (c:\.........\generator.clj:2:1)

Harshit Joshi19:09:37

I have replaced the path with .......

dpsutton19:09:49

can you paste the rest of the error?

Harshit Joshi19:09:06

(ns markov-elear.generator)

(def sentence "And the Golden Grouse And the Pobble who")

(defn func
	([] sentence)
	([name] (str sentence name)))

(func "harshit")

(defn -main
  "I don't do a whole lot."
  [x]
  (println ( str x "Hello, World!")))



;; keyword? functions
(odd? 2)  ; false
(even? 2) ; true

;; long short signed unsigned int
;; double float

dpsutton19:09:47

ok i think i know what is going on. you need to evaluate the namespace

dpsutton19:09:56

the function func has not been defined yet

dpsutton19:09:05

or evaluated

Harshit Joshi19:09:32

thanks man :thumbsup:

Harshit Joshi19:09:39

can i also ask something else

dpsutton19:09:55

of course. its the point of the channel. just ask your questions 🙂

Harshit Joshi19:09:11

whenever i do lein run in my project directory.

Harshit Joshi19:09:22

No :main namespace specified in project.clj.

Harshit Joshi19:09:21

this is my project.clj

Harshit Joshi19:09:30

(defproject markov-elear "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url ""}
  :dependencies [[org.clojure/clojure "1.10.1"]]
  :repl-options {:init-ns markov-elear.generator})

Russell Mull19:09:45

It's just like it says, you need a :main entry in there

Russell Mull19:09:19

The sample project.clj has an example of this... kind of.

Russell Mull19:09:32

It has EVERYTHING, so it's kind of a lot

noisesmith19:09:58

you can also get that sample in a terminal with lein help sample - if you are comfortable with paging / searching in terminal

Russell Mull19:09:59

But the comment above it should explain things well

Harshit Joshi19:09:06

thanks i will try this out

Russell Mull19:09:22

@U051SS2EU omg really? I wish I knew that 5 years ago.

noisesmith19:09:53

lein help has a lot of great content, which is weird for a CLI utility

Harshit Joshi19:09:38

thanks everyone it works now :thumbsup:

Harshit Joshi19:09:57

wow this is a very helpful community 🍀

😊 2
ghadi19:09:27

@harsjoshi1614 the number one thing to internalize early about clojure is that running it "batch mode" -- i.e. type some stuff, hit save, then lein run -- is hampering your abilities by 99%

🙌 2
ghadi19:09:04

using your editor of choice (vs code, emacs, vim, whatever), connect to a live REPL

💯 6
2
ghadi19:09:44

for example, in emacs, I hit a shortcut key, and it evaluates a small part of my program, the form surrounding my cursor

Harshit Joshi19:09:41

i was wondering why all the tutorials were so hell bent on using a live REPL shortcut key for evaluation

ghadi19:09:21

your feedback cycle with the editor-connected REPL will be faster than in any other language

Harshit Joshi19:09:41

yeah i never noticed this thing working with other languages lol

Harshit Joshi19:09:15

but it is actually a lot of time switching to you terminal and running the command to see the output with every little change

seancorfield19:09:08

@harsjoshi1614 You might find a couple of my videos interesting from that point of view https://www.youtube.com/watch?v=gIoadGfm5T8 (long, shows how I can build an app from scratch while it is running in the REPL) and https://www.youtube.com/c/SeanCorfield_A some shorter videos showing a similar workflow for different things. Stu Halloway also has a great video called REPL-Driven Development https://vimeo.com/223309989

🙌 6
seancorfield20:09:01

The #practicalli channel and associated videos might also be very interesting to you in terms of effectively leveraging the REPL.

❤️ 2
2
practicalli-john20:09:28

This overview of repl driven development is a good place to start. The workflow is applicable to any Clojure aware editor https://youtu.be/NDrpclY54E0

💜 2
Harshit Joshi20:09:02

thanks i will check them all out !

pez20:09:18

Check my vid out too! 😃 https://www.youtube.com/watch?v=d0K1oaFGvuQ It is not fancy in anyway, just me trying to use a familiar and simple problem to demonstrate an Interactive Programming workflow.

adityaathalye05:09:59

@U0ETXRFEW That's a lovely demo! Exactly the workflow I try to convey when teaching newcomers. Oddly, I find that structural editing blows peoples' minds first. The power of REPL-orientation takes a little longer to digest (requires drills and practice). As an aside, I also like to use tiny examples because they make it easy to illustrate Clojure's power to model the same domain in vastly different ways. And to also show how they can fall out of REPL experiments, like you've shown so nicely. I did https://gist.github.com/adityaathalye/cd3f275086285f15e384ad3707d9cc16#file-crackle_poppers-clj-L150: • cond (usual imperative style), • or (short-circuit, but still imperative), • juxt (closer to choice / alts-like concept), • lookup table (arithmetic domain), and • a functional encoding of the arithmetic domain (infinite sequence). After seeing your video I realized I missed multimethods / polymorphic dispatch / message-passing!

❤️ 2
pez05:09:03

Thanks! I get very curious about those five ways, butt the link seems to take me to something unrelated?

adityaathalye05:09:55

@U0ETXRFEW Sorry, "CracklePop" = "FizzBuzz". It was in response to a question someone had posed where they called it CracklePop instead of FizzBuzz. (And then I couldn't resist putting a very obtuse and very pointless reference to https://plato.stanford.edu/entries/popper/ in there 😅 ).

pez05:09:13

Hahahaha! I'll have a look when I have a computer.

lambda-prompt 2
practicalli-john20:09:28

This overview of repl driven development is a good place to start. The workflow is applicable to any Clojure aware editor https://youtu.be/NDrpclY54E0

💜 2