Fork me on GitHub
#beginners
<
2020-08-14
>
ar.nazeh07:08:38

Good morning everyone, 👋 I am a new developer who just got his first job working with JS. Thanks to Roam Research I got intrigued by Clojure and intend to explore it further, I struggled to find much Clojurians on Twitter so here I am, and would still love to follow you If you are active there!

chokheli10:08:35

@ar.nazeh a warm welcome to you!

cattabanks17:08:42

Hey, (+ (.-x p) (.-y p)) what is the best way to remove the p repetition? (+ (->> p (.-x) (.-y))) is failing for me

smith.adriane17:08:20

> (clojure.walk/macroexpand-all '(+ (->> p (.-x) (.-y))))
(+ (. (. p -x) -y))

smith.adriane17:08:09

I'm not sure there's a good way to get rid of the p repetition

cattabanks17:08:38

interesting -

alexmiller17:08:01

I don't think any answer to that is going to be more readable than what you have

cattabanks17:08:19

Thanks! Would love to see an alternative, if you have time to burn

alexmiller17:08:36

something like juxt is the typical way to apply multiple functions to the same piece of data

alexmiller17:08:50

but that's bad in multiple ways here

alexmiller17:08:14

it's way more verbose and it forces boxed numbers which is way slower

alexmiller17:08:44

(apply + ((juxt #(.-x %) #(.-y %)) p)) something like that

alexmiller17:08:14

don't be afraid of repetition

delaguardo17:08:30

Could be a juxt-like macro

alexmiller17:08:43

then you have to understand the macro

alexmiller17:08:09

unless you have 10 attributes or something, this is losing in every dimension imo

hiredman17:08:51

repeated names are how you make a computational graph

alexmiller17:08:52

KISS > DRY every day :)

cattabanks17:08:36

Yeah, that juxt alternative above is not readable, unless we are being esoteric and obstructionist

dpsutton17:08:37

I wouldn’t hate some kind of simple destructuring macro for this. But for this simple example I agree it’s overkill

cattabanks17:08:34

Well, I can maybe roll my own, because DSL 🙂

johnwesonga21:08:47

I'm building a Clojurescript frontend app and Clojure backed, I can run the frontend separately and the backend but I'm wondering how during dev I can just fire up one instance so that I can test frontend -> backend calls without having to hard code /port:path in the frontend

smith.adriane21:08:31

the browser's Location object, https://developer.mozilla.org/en-US/docs/Web/API/Location , has enough info to construct a path back to the host the webpage was served from.

johnwesonga21:08:11

May be my question wasn't very clear, in my re-frame app I make calls to my backend /api/users if I start the repl for the frontend and make those calls they will be mapped to the frontend server+port so but my backend http server is running on I could hard code the paths in the frontend, just wondering if there's another way, does this make sense?

jason35822:08:25

What do you use as your frontend server?

smith.adriane22:08:29

you could pass the api end point to the frontend. there's a couple of different ways to do that. the most straight forward is to just dynamically add a script tag like:

<script>window.MY_API_ENDPOINT="";</script>

jason35822:08:51

@ if it's shadow-cljs, then you're probably looking for this: https://shadow-cljs.github.io/docs/UsersGuide.html#dev-http-proxy

tkjone00:08:14

What I do is have my backend, your localhost:8080 I believe, serve the static assets and thus, hits the correct endpoints by default. I’m using figwheel and still use that to serve up files updates. Apologies if this wasn’t what you were looking for, just thought I would through this out there.

clojurians-slack10023:08:59

How would you translate the following into a transducer-based solution, in a way that the map and filter is only executed once?

(->> [:foo :bar :baz]
     (map str/upper-case)
     (filter some?)
     first)
I came up with these, but both seem overly cumbersome. (The printlns are just to be able to count iterations.)
(transduce
  (comp (map str/upper-case)
        (filter some?))
  (completing (fn
                ([] nil)
                ([result input]
                 (println "⇒" result input)
                 (reduced input))))
  [:foo :bar :baz])
(first (eduction (comp
                   (map str/upper-case)
                   (filter some?)
                   (fn [rf]
                     (fn
                       ([] (rf))
                       ([result] (rf result))
                       ([result input]
                        (println "⇒" result input)
                        (reduced (rf result input))))))
                 [:foo :bar :baz]))