Fork me on GitHub
#beginners
<
2021-11-18
>
Little Mose11:11:19

(defn production-rate
  [speed]
  (let [v (* speed
             (cond (= speed 0) 0
                   (<= speed 4) 1
                   (<= speed 8) 0.9
                   (= speed 9) 0.8
                   (= speed 10) 0.77))]
    (float (* v 221.0))))

(= (str 1392.3) (str (production-rate 7)))
(= 1392.3 (production-rate 7))

randomm char23:11:39

(= (float 1392.3) (production-rate 7)) = true check out https://clojure.atlassian.net/browse/CLJ-1036 also if your not aware (= 0.06 (+ 0.01 0.05)) = false because (+ 0.01 0.05) = 0.060000000000000005

Little Mose11:11:58

the first equality return true, the second one return false

Little Mose11:11:10

how to fix my function production-rate to let it return true ?

flowthing11:11:08

One option might be to use ratios instead of floats to avoid rounding issues.

flowthing11:11:40

user=> (defn production-rate
  [speed]
  (let [v (* speed (cond
                     (= speed 0) 0
                     (<= speed 4) 1N
                     (<= speed 8) 9/10
                     (= speed 9) 8/10
                     (= speed 10) 77/100))]
    (* v 221N)))
#'user/production-rate
user=> (= 13923/10 (production-rate 7))
true
user=> (= 1392.3M (bigdec (production-rate 7)))
true
For instance, depending on what you need exactly, of course.

Little Mose13:11:21

remove the float conversion will be ok:

Little Mose13:11:42

(defn production-rate
  [speed]
  (let [v (* speed
             (cond (= speed 0) 0.0
                   (<= speed 4) 1.0
                   (<= speed 8) 0.9
                   (= speed 9) 0.8
                   (= speed 10) 0.77))]
    (* v 221)))

Little Mose13:11:52

(= 1.23 (float 1.23))  will return false

Ryan14:11:06

Is there a way to print the code for a generated anonymous fn? It's an assembly of a few smaller pieces and I want to make sure they are composed correctly, but printing it predictably yields #object[Function] Edit: in CLJS btw, in case that matters :)

Ryan14:11:53

I mean, I could just try calling str on it.. annnnnnnd it seems to get me partly there

Benjamin14:11:25

dunno but say your functions are at 1 point collected in a list, you could have 1 form evaluate to the list and test it in isolation

Benjamin14:11:19

[+ inc]
=> [#function[clojure.core/+] #function[clojure.core/inc]]

Ryan14:11:59

oo not a bad idea!

Andy Carlile16:11:02

It's likely that im just a freaking idiot, but I cannot find any material on how to implement a cljs app and a clj webserver together without a template. I'm specifically trying to implement sente+reagent. I can create each component separately but all attempts to conjoin the two have failed. What can I read to figure this out? Nearing the end of my wits

Andy Carlile16:11:10

thanks for the response! This is cool, I'll try to study it. Is there a write-up for how to arrive at code like this repo?

Michael W16:11:56

No, you'll probably have to look at several different projects that are doing what you want to see how they do things. I'm not aware of any in-depth analysis on this setup.

Andy Carlile16:11:03

this build appears to fail anyway...

Andy Carlile16:11:22

is there no resource on how to add a reagent frontend to an existing clj webserver? I have the two built already

Benjamin16:11:59

let's say I create a "new" vector [1 2 3] a thousand times in a loop. Is there immutability or jvm magic that makes it so I don't allocate 1000 times?

ghadi16:11:28

if the vector is constant (it's filled with literals) it will be created once

Benjamin16:11:47

ok that's sick. #"regex" is not a literal or is it?

ghadi16:11:05

a regex is a literal

clojure-spin 1
alexmiller16:11:15

it creates a java Pattern when compiled

ghadi16:11:35

(fn [x] [1 2 x]) the vector there is not a constant ^

👍 2
Andy Carlile16:11:45

how does one add a reagent frontend app to an http-kit webserver?

FiVo16:11:09

For development and getting started, I suggest setting up http-kit with shadow-cljs. Just add one route that serves some static index.html which links to the generated app.js file or whatever yours is called.

Andy Carlile16:11:08

thanks for the response! How do I ensure that the app.js linked is my clojurescript?

FiVo16:11:10

wait a second, I have a template somewhere

FiVo17:11:02

https://github.com/FiV0/spa-ws-template it's an example project setting up a websocket with http-kit, but also serving a cljs frontend with re-frame

FiVo17:11:21

you can ignore the websocket stuff

FiVo17:11:41

ignore the README for now

FiVo17:11:55

Here are the routes https://github.com/FiV0/spa-ws-template/blob/master/src/io/dbme/router.clj#L11-L14. So you serve the https://github.com/FiV0/spa-ws-template/blob/master/resources/index.html and shadow-cljs compiles the js which gets served as a resource. Let me know if that makes sense.

FiVo17:11:32

Ah saw that somebody actually already replied in another thread with a template.

Andy Carlile17:11:36

in what thread? if it's https://github.com/DavidVujic/sente-with-reagent-and-re-frame then that's similar to the solution you supplied above, it's a complete project without a wrrite-up w/r/t replication or a template

FiVo17:11:24

I am updating the Readme now, but what exactly are you struggling with?

FiVo17:11:45

I know it can sometimes be a bit daunting to put everything together.

Andy Carlile17:11:44

thanks for asking, I feel like an absolute dunce.

Andy Carlile17:11:04

I feel worse than that, but I'm trying to keep it clean on the boards

FiVo17:11:54

Have you got shadow running? Have you got your server running?

Andy Carlile17:11:33

i can create an webserver with compojure and http-kit and I can create a reagent app. I can do these things with appropriate templates such as lein new app or lein new reagent but I have also found guides to build them from scratch, which is my preference. however, I have not found any guides for adding clojurescript to an existing clojure webserver; only for starting a new full-stack template or for creating one or the other. I don't know how to join them

Andy Carlile17:11:17

I'm trying to build something to help with job applications, as I got laid off two months ago and am starting to lose my sh*t

Andy Carlile17:11:27

for that reason I am trying to find a method that does not rely on templates. the reagent template uses reitit, for example, which I could not get to play nicely with sente. I asked about it earlier and got "try not using a template"

FiVo17:11:44

yes totally get it. I think looking at templates just helps you see how other people might do it and then you can copy bits and pieces.

FiVo17:11:03

the sort of "dumbest" thing you can do if you have both a server and frontend project running is to just hardcode the app.js path of the reagent app in your index.html and go from there

Andy Carlile18:11:33

just what I rebuilt today, at least

Andy Carlile18:11:47

it runs with lein run on port 3000

Andy Carlile18:11:08

it has some clojurescript but none that's loading

Andy Carlile18:11:33

im updating the deps

Andy Carlile18:11:36

i just pushed an update that expands the project.clj file to include more deps as well as :profiles, :cljsbuild, and :figwheel

Andy Carlile18:11:33

lein run still behaves (by building to 3000 and displaying random pokemon)

Andy Carlile18:11:42

wow it looks like catjam is a magic totem that makes my code work

Andy Carlile18:11:48

that and all the screaming from this morning

Andy Carlile18:11:04

now if only i could add sente to the frontend section of my app

Andy Carlile18:11:20

now I still need to navigate the last hurdle: I keep getting 403s (bad request) from the chsk handshake in the browser console.

Andy Carlile18:11:28

oh flip duh i need middleware

Andy Carlile18:11:21

tried adding middleware and keep getting classNotFound exceptions... 💢

Andy Carlile22:11:20

this looks lovely! I'd love to use this, but I'd need to be able to re-build it myself esp. if I ever used it in a production setting or to use for job applications. What's your process for building this? did you start from scratch or with a template?

Andy Carlile18:11:22

I've finally gotten an http-kit server running with reagent on the frontend and am trying to implement sente to facilitate client-server interaction. here's https://github.com/jollyblondgiant/clj-pokedex so far (it's a little messy because I'm still figuring out the details). everything works (in its dinky way) except for sente, which keeps causing a 403 (bad request) error in my browser console. what piece am i missing? feeling crazy

hiredman19:11:37

have you looked at the body of the 403 response

hiredman19:11:34

looking at the sente code, it appears have two places 403's are given, and in returns an informative message in the body for both

hiredman19:11:33

either a bad csrf token or a bad websocket origin

Andy Carlile19:11:25

GET  400 (Bad Request)
goog.net.XhrIo.send @ xhrio.js:198
taoensso$encore$ajax_lite @ encore.cljc?rel=1637263374814:3424
taoensso$sente$poll_fn @ sente.cljc?rel=1637263375810:1431
(anonymous) @ sente.cljc?rel=1637263375810:1425

Andy Carlile19:11:08

looks like something related to wrap-params and wrap-keyword-params middlewares. injected them back into my code and I'm still getting 400. this time the browser error is much larger

Andy Carlile19:11:01

how can i determine the fault between the csrf token and a bad websocket origin? i've updated the https://github.com/jollyblondgiant/clj-pokedex

Andy Carlile19:11:50

being a 400 and not a 403 (I saw 403s once but now, having done I-kn-wnot-what. I only see 400s now), it looks like the websocket's never acutally connecting.

hiredman21:11:20

are you testing locally or running through heroku?

Andy Carlile21:11:25

It looks like requests to localhost:3449/chsk aren’t valid routes, but I did set up the CHSK routes in the server and I don’t know how to point them at any other endpoint from the client.

hiredman21:11:42

how are you starting the server?

hiredman21:11:26

sounds like you are trying to do sente stuff against the server figwheel runs, and not your server with your routes

Andy Carlile21:11:11

In one terminal I run “lein figwheel”. In another terminal I run “lein do clean, run. ” the lein run terminal seems to hang though, even after i rebooted my machine

Andy Carlile21:11:48

Is this something I need to update in the project.clj file or in my server code?

hiredman21:11:51

what do you mean hang?

hiredman21:11:05

what output do you expect other than just sitting there running the server?

Andy Carlile21:11:16

Some stdout that says what port it’s running on maybe

hiredman21:11:43

did you write code that prints that out?

Andy Carlile21:11:20

I just don’t want to assume I’m not an idiot

Andy Carlile21:11:05

If anything isn’t going right I just assume it’s because I broke it. So it’s good to hear that lein run is still behaving normally

Andy Carlile21:11:33

I’ve been beating my brain against this wall since Monday. It’s starting to take its toll on me.

hiredman21:11:59

I suspect you need to point the sent stuff at the sente stuff at the server start via lein run, and not whatever figwheel is doing

hiredman21:11:28

if you look at the README file in the project it actually mentions that websockets may not work correctly with the figwheel server

Andy Carlile21:11:56

How can I fix the issue you alluded to above, that sente may be pointed at the wrong server?

Andy Carlile21:11:23

Cool, I’ll remove figwheel and try again. Does this preclude using figwheel for development then?

hiredman21:11:42

no idea, I've never used figwheel or sente

Andy Carlile21:11:34

i'm testing locally with lein figwheel

metehan21:11:34

hello I am trying to use a react component in my clojurescript app I am trying to convert a html to hiccup is it correct way: original react code <Pie dataKey="value" data={data} cx={200} cy={200}fill="#ff7300" label /> my hiccup [Pie {:dataKey "value" :data data :cx 200 :cy 200 :fill "#ff7733" :label true} ]

Andy Carlile21:11:05

@m373h4n this looks good. react may act funny and require you to prepend your custom data keys with :data- eg :data-cx

🙏 1
metehan21:11:37

in javascript what does curly brackets means why they wrote {200} instead of 200 I never used react outside clojurescript it confuses me 🙂

Lennart Buit22:11:39

Thats not actually plain JS, its JSX. Its a way to splice in a value in a prop of a React component.

Lennart Buit22:11:23

so <MyComponent propName={3} /> means ’create a react component, MyComponent, with as props, propName with value 3

Lennart Buit22:11:46

In JSX you need to enclose all values that aren’t strings (and booleans, other story) with {}

metehan22:11:04

ah I see. it was hard to search such information on docs. now it's clear thank you

metehan22:11:39

(ns tools.charting
  (:require
   [reagent.core :as r]
   ["recharts" :refer [PieChart Pie Sector Legend Tooltip Cell]]))

(defn chart-render []
  [:div.boxed
   [:h1 "Hello!"]
   (let [data  [{:name 'Ada' :value 800}
                {:name 'Turing' :value 500}
                {:name 'Alice' :value 700}
                {:name 'Bob' :value 900}]]
     [PieChart {:width 800 :height 400}
      [Pie {:dataKey "value" :data data :cx 200 :cy 200 :fill "#ff7733" :label true}]])])
When I use PieChart or Pie results an error TypeError("Cannot call a class as a function") I am trying to draw a chart just like this: https://github.com/recharts/recharts/blob/master/test/specs/chart/PieChartSpec.js the way original javascript version calls must be identical to my clojurescript. according to my understanding it should just work but apparently I am missing something.

dpsutton22:11:52

https://github.com/reagent-project/reagent/blob/master/doc/InteropWithReact.md#creating-reagent-components-from-react-components should give some documentation about how to call react components directly from reagent forms

1
metehan23:11:25

thank you it helped 👍