Fork me on GitHub
#beginners
<
2021-04-02
>
cschep00:04:33

I’m not sure how exactly to ask this question but the gist is that I have a program that is a loop, that is a chat bot, so it sits and loops and listens to input and responds, pretty starightforward. Doing REPL driven development on it has been challenging because when I run the listening loop it hangs the REPL

cschep00:04:42

is there a way to async that loop in the “background”

cschep00:04:51

and then i can poke and and update a function it calls

cschep00:04:01

to have a more interactive experience?

noisesmith18:04:24

one option to consider is that the bot and the repl can listen to different file descriptors, either because you have multiple connections to one repl process, or you use a dedicated pipe/fifo - a question of making an alternate binding for *in* connected to a different tty

noisesmith18:04:52

(a process with multiple repl connections already makes multiple *in* bindings for you)

dpsutton00:04:22

generally a loop will call some function with some kind of message and then recur. from the repl you would want to not start the loop but just call the function that handles the message

dpsutton00:04:12

kind of a "functional core, imperative shell" type thing. top level you have a loop which takes a message from a socket/web endpoint/queue/what have you and then does actions based on it, and then back to the top of the loop

cschep00:04:07

i’d love to be able to poke at it while it’s running the loop and getting stuff from the server

cschep00:04:14

in that case i’d be passing my own messages in yeah?

cschep00:04:07

i think it’s waiting on a channel for input

cschep00:04:08

(a/<!! event-ch)

cschep00:04:23

in go i would call this as a go routine

cschep00:04:27

and it wouldn’t block anything while it did its thing

cschep00:04:02

core async has go routines? woah

zackteo07:04:28

Hi is there an iterate that allows for side effects?

zackteo07:04:29

am I looking for doseq?

zackteo08:04:53

^ this runs but the result used for rand-nth is fixed

zackteo08:04:54

Is there a function or a better way to do what I want.? Am trying to create a simulation of when a factory will refill its stock. So (def days-store1 [2 3 3 4 4 5 5 6]) is saying after there is a 1/8 chance that after 2 days, there is a need to restock. And then it undergoes a renewal process. So it any of those number of days in days-store1 are equally likely again

zackteo08:04:07
replied to a thread:

Is there a way I can do (take-while #(>= 1280 %) (reductions + (repeatedly #(rand-nth [1 2 3 4 5])))) but still have rand-nth do its side effect with each iteration? Like is there another something else I can use instead of take-while

Dimitar Uzunov12:04:08

making a jar file for deployment

💯 2
oxalorg12:04:04

Thanks yes I could talk quickly about it showing an example. I was trying to stay away from deployment as I wanted to create a separate video on it 🙂

Dimitar Uzunov12:04:09

it is more of a build phase anyway 🙂

👍 1
✔️ 1
Umar Daraz12:04:03

backend and frontend routing may be

💯 1
oxalorg12:04:40

Thanks. Yes routing is something I want to cover but I'm confused to what depth? I do plan to add a simple (case (:uri request).. router in my jetty handler to explain the flow of how routers work at their core. I mostly tend to use reitit as it's quite nice on both backend and frontend, do you think I should cover those to setup separately? Or are you thinking more along the lines of a single shared router between both frontend and backend?

Umar Daraz13:04:40

I m thinking of separate routing setup for backend and frontend. I don't know that shared routing setup is even possible. If it is possible, would love to see this setup. Im thinking in the lines that, e.g. in nodejs/react app, we mostly add experess and react router at the start. because even a smallish app tend to have few pages on front end and few crud operations on backend.

oxalorg13:04:52

> I don't know that shared routing setup is even possible. > If it is possible, would love to see this setup. It's definitely possible with reitit, I don't have much experience with it but it would be a great way to learn and document. I would love to do this separately!! Thanks

oxalorg13:04:35

> Im thinking in the lines that, e.g. in nodejs/react app, we mostly add experess and react router at the start. > because even a smallish app tend to have few pages on front end and few crud operations on backend. Yes that is indeed true, but with adding routers do you think I need to talk a bit about content negotiation/encoding, middlewares or maybe interceptors too? :thinking_face: Edit: Maybe that would be a bit too much haha. I'll think about the most minimal example I can whip up with reitit

Umar Daraz13:04:30

Yeh indeed, negotiation/encoding and middleware come into play. And it may drag the direction to library usage, instead of minimal setup. May be you can have another screen cast, that is more involve with routing after the setup one

🚀 1
💯 1
oxalorg13:04:06

> May be you can have another screen cast, that is more involve with routing after the setup one Yes looks like that would be best! Thanks for your suggestions and feedback, super appreciate it 😄 ^_^

💯 1
🙏 1
Umar Daraz13:04:50

Here how i setup a minimal reitit example. May be it can be further simplify.

(def app
  (ring/ring-handler
   (ring/router
    ["/api"
     ["/scramble" {:post  scramble-handler}]]
    {:data {:muuntaja m/instance
            :middleware [muuntaja/format-middleware]}})
   (ring/routes
    (ring/create-resource-handler {:path "/"})
    (ring/create-default-handler))))

🙌 1
Umar Daraz13:04:46

thanks @U013MQC5YKD looking forward to the screen cast. cljs

bananadance 1
oxalorg13:04:48

Lovely! I was thinking something very similar, but I also understand that this can get really daunting for someone quite new 🙈

oxalorg13:04:26

> looking forward to the screen cast. Thanks haha, I'll be sure to ping you once I have it up 🙂nyantocat

Umar Daraz13:04:06

Yes pls do. I m fairly new in clojure land, And when I was try to setup my project, I was not able to find such screen cast. So I m sure, it will help a lot of new comers

awesome 1
💯 1
🙌 1
oxalorg13:04:44

I had the exact same experience! Trying my best to fill in this assumed gap, I already have a few beginner friendly clojure videos up here: https://www.youtube.com/channel/UCd588hDu4bszrSHlLXC8eZA If you're interested in checking it out 🙂

Umar Daraz13:04:48

🙌 wow Im sure I will go through each of these videos one by one. thanks of making and sharing, 🎯

bananadance 1
🥳 1
oxalorg13:04:39

Thanks a lot this means a lot! sharkdance 🙏

Endre Bakken Stovner14:04:41

In the below code the let only differs for the outer loop.

(for [[rulename jobs] (group-by :in-rule jobinfo)
        job jobs
        :let [{:keys [params shell script]} (rules rulename)]]
Still, is the let computed anew for each inner iteration?

NoahTheDuke15:04:10

to find out, you could embed a (do (println "hey") (rules rulename))

NoahTheDuke15:04:23

but you can also move the :let above the job jobs line

💯 2
NoahTheDuke15:04:15

user=> (for [a (range 1 5) :let [b (inc a)] c (range b)] c)
(0 1 0 1 2 0 1 2 3 0 1 2 3 4)

Endre Bakken Stovner05:04:36

Had no idea I could move the let. Thanks!

zackteo15:04:15

Hi Everyone, how would yall approach a problem of generating a list of numbers that looks like [0 0 2 0 2 0 0 0 2 .... ] where the interval between the 2 are randomly chosen. Perhaps using something like rand-nth [2 2 3 4 5]

hindol16:04:39

(take 100
      (flatten
       (interpose 2 (map #(repeat % 0) (repeatedly #(rand-nth [2 2 3 4 5]))))))
Something like this?

zackteo00:04:48

@UJRDALZA5 Wow yes, this is what I wanted! 😄 Actually my previous question was me trying to take a immediate step to get this

dpsutton15:04:43

i'd probably make a lazy seq that would compute the random interval, put that many 0's and a 2, and then cons onto the "recursive" call

Elliot Stern15:04:50

mapcat seems useful

dpsutton16:04:44

TIL replicate

hindol16:04:21

Me too. But repeat is favored over replecate.

alexmiller16:04:50

replicate is deprecated

oxalorg16:04:51

Every element in the sequence basically has a random chance of being either a 0 or a 2 (assuming 2's can be adjacent to each other). So something like this should also be enough:

(repeatedly 10 #({0 0 1 2} (rand-int 2)))
;; => (0 2 0 2 0 2 0 0 0 0)

pavlosmelissinos17:04:53

or maybe even (repeatedly 10 #(rand-nth [0 2])) ? this way you have some control over the distribution of the numbers, e.g. you can say (repeatedly 10 #(rand-nth [0 0 2])) if you want to have "twice" as many 0s as 2s (twice doesn't really make sense here, i'm using it figuratively)

alexmiller17:04:07

you might also look at test.check generators to make these kinds of things

💡 2
alexmiller17:04:33

helpfully, spec makes them for you if you can describe the sequence

alexmiller17:04:43

% clj -Sdeps '{:deps {org.clojure/test.check {:mvn/version "0.9.0"}}}'
Clojure 1.10.1
user=> (require '[clojure.spec.alpha :as s] '[clojure.spec.gen.alpha :as gen])
nil
user=> (gen/sample (s/gen (s/+ (s/cat :a (s/* #{0}) :b #{2}))))
((2) (2 2) (2) (0 2 0 0 2 0 0 2 0 2) (0 2 0 2 0 0 0 0 2 0 0 0 0 2) (0 0 0 0 2 0 0 0 0 2 0 0 0 2 0 0 2) (0 0 0 0 0 2 0 2) (0 2 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 2) (0 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2) (0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 2))

🤯 2
pavlosmelissinos17:04:53

https://clojure.org/guides/spec#_sequences > * - 0 or more of a predicate/pattern (check out the examples in the docs, they're really helpful)

sova-soars-the-sora17:04:14

Ah, like FSM notation. So + is "one or more" ? thank you i will check out the link

manutter5119:04:44

s/* => “Zero or more instances of” (like in regex)

yiorgos19:04:18

Is there an easy way to parse a date string like this Fri, 02 Apr 2021 18:00:17 GMT into a date instance? I’ve tried with java.time.Instant/parse but didn’t work

henryw37419:04:52

DateTimeFormatter, in java time, will do that if you give it a pattern

👍 1
dpsutton19:04:15

do you know how you ended up with that string?

yiorgos19:04:45

Last-Modified header has a value like that, for example curl -I -X GET

dpsutton19:04:07

oh bummer

((juxt #(get % "Date") #(get % "Last-Modified"))
                              (:headers (http/get "")))
["Fri, 02 Apr 2021 19:49:39 GMT" "Fri, 02 Apr 2021 18:00:17 GMT"]
was hoping those wouldn't be strings like that programatically.

yiorgos20:04:20

in case someone else finds it useful, the way I achieved that:

(def t (.parse java.time.format.DateTimeFormatter/RFC_1123_DATE_TIME "Fri, 02 Apr 2021 18:00:17 GMT"))
(java.time.Instant/from t)

jumar04:04:35

There's also an utility function in ring for parsing HTTP-header style dates:

(ring.util.time/parse-date "Fri, 02 Apr 2021 18:00:17 GMT")
;;=> #inst "2021-04-02T18:00:17.000-00:00"

🙏 1
Joseph Rollins20:04:18

maybe more of a spacemacs question... I have both the clojure layer and lsp layer enabled and when I open a new file I get the namespace autogenerated twice e.g.:

(ns my-ns)

(ns my-ns)
anyone know how I can disable 1 of these two from filling in the namespace?

ericdallo20:04:15

Check *Conflict with clj-refactor when creating new files* https://emacs-lsp.github.io/lsp-mode/tutorials/clojure-guide/

🙌 1
Joseph Rollins20:04:59

awesome, thanks!

👍 1
zach22:04:01

would anyone know how to include XML CDATA in a hiccup map? I am writing a babashka script that takes an edn file with markdown and turns it into an RSS feed. e.g.

[:item
  [:title "new post"]
  [:link ""]
   [:description (markdown->html "**some stuff**")]]

borkdude22:04:27

@webmaster601 according to the readme of clojure.data.xml:

[:-cdata "not parsed <stuff"]

zach22:04:31

oh, my gosh, thank you!

zach22:04:50

Also, thank you for babashka. It is incredible!

❤️ 1