Fork me on GitHub
#beginners
<
2021-01-21
>
Jorge Tovar00:01:57

Hello, another question:

(def dark-roast-coffee-desc "Dark Roast Coffee")
(def dark-roast-coffee-cost 0.90)

(defn make-dark-roast-coffee
      ([] (beverage dark-roast-coffee-cost dark-roast-coffee-desc))
      ([size] (beverage dark-roast-coffee-cost dark-roast-coffee-desc size))
      )
is there a way to re-use the values of dark-roast-coffee-desc and dark-roast-coffee-cost inner the function with multy-arity?

andy.fingerhut00:01:47

re-use in what way?

Jorge Tovar01:01:25

be inner the scope of the function :thinking_face:

dpsutton01:01:22

What does that mean? They are def’ed in the namespace. They are visible in all scopes below their definition

Jorge Tovar01:01:32

yes, but what If I wanna make this values private. like (let [cost 0])

dpsutton01:01:09

Only forms inside of that let block can see cost. Seems pretty private to me

Jorge Tovar01:01:36

Its an example

dpsutton01:01:08

What’s an example where you’d actually like them private then?

hiredman00:01:55

you are already doing it

hiredman00:01:29

the values are bound to names, the names are in scope and usable from both arity bodies

Jorge Tovar01:01:36

yes I like the current solution, but def values are visible in (:require [my.namespaces]). no?

hiredman02:01:34

Yes, make everything visible

Jorge Tovar02:01:55

OK no problem

Jorge Tovar01:01:25
replied to a thread:re-use in what way?

be inner the scope of the function :thinking_face:

seancorfield01:01:06

(let [dark-roast-coffee-desc "Dark Roast Coffee"
      dark-roast-coffee-cost 0.90]
  (defn make-dark-roast-coffee
    ([] (beverage dark-roast-coffee-cost dark-roast-coffee-desc))
    ([size] (beverage dark-roast-coffee-cost dark-roast-coffee-desc size))))
@jorgetovar621 That will make those names local to the function, but it's somewhat unusual to have let-over-`defn`

Jorge Tovar01:01:20

Thank you so much. Thats exactly what im looking for

seancorfield01:01:10

Having default positional arguments is what is causing you problems here really. You might want to rethink that...

Jorge Tovar01:01:46

yes. The test failed. Im gonna leave as def values, I think is good enough by the moment, thank you

seancorfield01:01:26

Default positional arguments do not compose very well -- as you can see, having beverage take an optional positional argument forces make-dark-roast-coffee to also take an optional positional argument. I'd consider it a "code smell".

Jorge Tovar01:01:29

thats a good point

Hlodowig06:01:53

Hi guys. I want to learn clojure + redis and I need to choose a cloud provider. Would you go for Heroku or AWS, and why?

Hlodowig06:01:00

I know my way around AWS reasonably well but I just noticed Heroku supports Clojure.

Ovidiu Stoica07:01:23

Hei guys, so I have this next.jdbc function:

Ovidiu Stoica07:01:55

If I use the actual db for both commands this works

Ovidiu Stoica07:01:26

But if I use conn I get

Ovidiu Stoica07:01:47

None of the tables have a id column, and I would love to use the same db connection for both queries

Ovidiu Stoica07:01:26

nevermind, I found the issue. conn is not passed the kebab-snake-case options of the db . Fixed 👍

roelof11:01:37

I have made this layout in hiccup

roelof11:01:09

Can someone help me to make this in react so I can show a global window to show data of a painting

roelof11:01:29

I mean more think in what are the components to make this work ?

Piotr Brzeziński11:01:08

Global window you mean like a modal?

roelof11:01:47

yep, a overlay with a modal screen

roelof11:01:13

but first I have to make this in react or reframe

Piotr Brzeziński11:01:09

It uses classes but shouldn’t be hard to move it to hooks

roelof11:01:35

Thanks, I will look at it

roelof11:01:03

just following a course on re-frame

borkdude11:01:46

@roelof are you using bootstrap or something like this?

roelof11:01:16

fotr that one no bootstrap is used

roelof11:01:22

only css and html

roelof11:01:15

see here for the html and css I used : https://codepen.io/rwobben/pen/xxVepxG

borkdude11:01:52

how you could do this in reagent / re-frame: have one component which paints the modal, but only when some state is set, e.g. (subscribe [::show-modal]) and set the re-frame db :show-modal value to true, if a user has clicked on one of these paintings

borkdude11:01:09

the same is true for reagent-only but you can do it with atoms

roelof11:01:00

oke, but I mean a step before the modal

roelof11:01:51

can I make the layout with re-frame and if so , are all the paintings a componennt and all the shelves

roelof11:01:23

or can it be one big component. That is really my question

borkdude11:01:43

you can make one component for the modal:

(defn modal []
  (let [show? (subscribe [::show?])
     painting-id (subscribe [::painting-id])]
  (fn [] 
(if @show?  
[:div [:img src="..."]])
....
(sorry for the layout, typing code in Slack is...)

borkdude11:01:26

you can split up things in as many components as you would like, just as when writing functions

roelof11:01:32

oke, I can image that because of the subscribing I can better can make every painting a component. So a different text can be displayed

borkdude11:01:24

yeah, you can do that

roelof14:01:09

pff, now hiccup is messing with my layout code so far : https://github.com/RoelofWobben/paintings

roelof14:01:27

the last image is totally at a wrong place

roelof14:01:01

Can anyone help me figure this out

manutter5114:01:55

Is it a hiccup problem or a CSS problem?

manutter5114:01:32

I mean is the underlying HTML wrong, or is the display rendering incorrectly in the browser?

roelof14:01:02

the display it totally wrong

manutter5115:01:41

Ok, let me look at it for a bit. How do you launch the server?

roelof15:01:34

right now for the repl with (start-server)

👍 3
manutter5115:01:13

Darn, got a hot issue just came up at work, it’ll be a while…

roelof15:01:43

no problem. This is a toy project to learn and practice with clojure web development

roelof16:01:19

did you send me a PR ?

manutter5116:01:39

Ok, I had a chance to look at it — there was one typo and some issues with nesting. (Also the /images/prev.png route is returning a nil response.) I sent you a pull request because that was the easiest way I could think of to communicate the changes.

manutter5116:01:18

Do you have rainbow parens or some kind of paren highlighting in Calva? I’m using IntelliJ and the rainbow parens are really helpful in diagnosing problems like this.

roelof16:01:53

yep, I use VS code with Calva and it seems to have rainbow parens

roelof16:01:51

but sometimes I get lost in the parentheses and so on

roelof17:01:01

many thanks for the PR

roelof17:01:08

now I can further experiment with something for the prev and next links

Paul14:01:35

hi, anyone knows how to set lein test to pinpoint which test failed and show the corresponding error? the following output is not helpful:

reverse_string$reverse_string.invokeStatic (reverse_string.clj:7)
    reverse_string$reverse_string.invoke (reverse_string.clj:3)
    reverse_string$reverse_string.invokeStatic (reverse_string.clj:7)

Ran 8 tests containing 8 assertions.
0 failures, 1 errors.
Tests failed.

Paul15:01:27

i ended up running invidual test using :only ... i wish test would pinpoint the failing test upfront and show the error reason

manutter5116:01:07

What do your tests look like?

manutter5116:01:47

Here’s a quick test as an example:

(require '[clojure.test :refer [deftest testing is run-tests]])
=> nil
(deftest test-error-reporting
  (testing "a test that should succeed"
    (is (= 2 (+ 1 1))))
  (testing "a test that should fail"
    (is (= 2 1)))
  (testing "another test that should succeed"
    (is (> 2 1))))
=> #'user/test-error-reporting
(run-tests 'user)

Testing user

FAIL in (test-error-reporting) (markn.clj:5)
a test that should fail
expected: (= 2 1)
  actual: (not (= 2 1))

Ran 1 tests containing 3 assertions.
1 failures, 0 errors.
=> {:test 1, :pass 2, :fail 1, :error 0, :type :summary}

👍 3
Paul16:01:31

it's a test suite from exercism: https://pastebin.pl/view/5c8c3751

Paul16:01:12

strange... lein test works as expected now...

Paul16:01:34

i guess in the previous one, an error blocked the tests from even running...

omendozar15:01:50

Hi, is there a lib in Clojure for the SMPP protocol? To enable SMS messaging between applications and mobiles?

phronmophobic17:01:53

I haven't tried using SMPP, but I have used Twilio's clojure library to successfully send text messages

roelof17:01:16

hmm, how can I add this with hiccup : `

<script src="" crossorigin="anonymous"></script>

pavlosmelissinos18:01:53

what have you tried so far?

pavlosmelissinos18:01:22

Have you seen the API docs for hiccup? > Include a list of external javascript files. include-js expects a list, not a map

pavlosmelissinos18:01:26

If you click on view source you should be able to understand what the "list of files" should look like.

roelof18:01:59

oke, but how can I then add thaty cross-origin part

pavlosmelissinos18:01:33

include-js doesn't seem to allow you to do that

pavlosmelissinos18:01:08

But you can build it yourself using only clojure data structures

roelof18:01:33

or do you mean I could do something like this : [:link {:href "/css/styles.css", :rel "stylesheet"}]]

pavlosmelissinos18:01:42

yup, exactly that

pavlosmelissinos18:01:09

(hiccup.core/html [:span {:class "foo"} "bar"]) outputs the following html string:

"<span class=\"foo\">bar</span>"
Modify the contents of the vector to get the output you want

pavlosmelissinos18:01:34

Make sure to try it out on a REPL as you modify it to get instant feedback 😉

roelof18:01:31

do not know if this is working `

<link crossorigin="anymous" href="" rel="js">

3
roelof18:01:54

hmm, this looks better

<script crossorigin="anymous" href=""></script>

roelof18:01:05

but still no icon to see

pavlosmelissinos18:01:21

Better than before but still different from your target:

<script src="" crossorigin="anonymous"></script>

roelof18:01:35

hmm, now I have to find out why the script is not loaded

pavlosmelissinos18:01:48

You need to pay more attention to detail,

👀 3
pavlosmelissinos18:01:16

you have a script element

pavlosmelissinos18:01:31

and you want to load a js file

pavlosmelissinos18:01:41

what attribute do you use to do that?

roelof18:01:59

a src instead of a href

roelof18:01:08

and I see a very small icon 🙂

👌 3
pavlosmelissinos18:01:56

you also have a typo

pavlosmelissinos18:01:04

anymous -> anonymous

roelof18:01:21

thanks, look well

roelof18:01:00

tomorrw and the dasys after it work on the next page

roelof17:01:27

my app method looks like this : |

(defroutes app
  (GET "/" [] (-> (memo-display-data)
                  (display/generate-html)))
  (route/resources "/")
  (GET "/favicon.ico" [] ""))

roelof17:01:32

and this is the beginning of my templte

(defn convert-to-hiccup [data]
  [:html
   [:head
    [:meta {:charset "utf-8"}]
    [:title " Most popular paintings from the Rijksmuseum "]
    [:link {:href "/css/styles.css", :rel "stylesheet"}]]
   (display-body data)])

sova-soars-the-sora18:01:43

Re: "Clean Code" -- if you can come back to it in 6 months and [swiftly] reason about what the code does and the intended results, that's clean. If not, time travel back six months and tell yourself to add more documentation and stop using Java 😛

roelof21:01:18

sorry to ask so many but can I make this work without the fn

(defn flatten-a-sequence [s]
  (reduce (fn myflatten [collection element]
            (if (sequential? element)
              (reduce myflatten collection element)
              (conj collection element))) [] s))

alexmiller21:01:34

why not just use flatten?

roelof08:01:41

that is forbidden. I think we have to re-implement flatten

practicalli-john21:01:42

Given a sequence of hash-maps, is there a predicate function I can use to determine if any of the hash-maps contain a specific key with specific value? So far I have a function mapped over the vector of hash maps which returns a sequence of true false. I'd like a boolean true or false answer returned, rather than a sequence of bolean values. The key is always a top level key. I'd rather not use a library other than clojure.core

(map #(= (:key %) "fish") [{:key "fish"} {:key "chips"} {:key "peas"}]) 

phronmophobic21:01:53

you could use some rather than map

👍 3
dpsutton21:01:57

(some (comp #{value} :key) coll)

practicalli-john21:01:36

sometimes my brain only lets me type map - I think I am map obsessed :)

dpsutton21:01:32

then you're never lost 🙂

6
phronmophobic21:01:12

you asked for something that returns true or false and technically, the some version will return true or nil. that probably doesn't matter, but wanted to point it out, just in case

👍 3
practicalli-john22:01:26

I think I am okay with a falsey nil 🙂

👍 3
Bret Horne23:01:47

I saw a similar example of this kind of destructuring on a blog post but im not finding this in my google searches.

(defn hello [{:keys [] :as rest}] 
  (rest-of-fn))
Is it that the :keys [] bit merely takes the entire map you give it vs say more selective destructuring with {:keys [a b]} a-map?

Lennart Buit23:01:43

That said, I don’t understand what you are asking, could you elaborate ^^

Bret Horne23:01:50

I’ve scoured it…Am I mistakenly missing it? I dont see an instance of having an empty vector.

Lennart Buit23:01:23

I don’t think an empty vector does much

Bret Horne23:01:43

For instance if I were to use that style with

(defn hello [{:keys [] :as rest}]
  (println (str rest)))

(hello {:hello "hello"})
{:world "world"}

(hello {:hello "hello" :world "world"})
{:hello "hello", :world "world"}

phronmophobic23:01:52

an empty vector for :keys doesn't do anything

Bret Horne23:01:08

ah is it merely needed for using :as blah?

phronmophobic23:01:30

you can just have :as blah without the :keys

Lennart Buit23:01:42

no, not even that (defn hello [{:as rest}] …) or (defn hello [rest] …) would also both work

👍 3
Bret Horne23:01:09

oh! duh 😛

dpsutton23:01:53

"from the map, please bind no keys and call the whole thing rest"

👍 6
Bret Horne23:01:00

(this was the first place I saw it: https://lambdaisland.com/blog/2020-07-28-well-behaved-command-line-tools) so it struck me as curious.

Bret Horne23:01:24

(defn main [args {:keys [] :as opts}] ...)

Lennart Buit23:01:00

Yeah, maybe to visually show that opts is a map, maybe its the authors preference to always define :keys […] for future extension, or maybe its just an oversight 🙂

Bret Horne23:01:20

(thanks for the help!)

phronmophobic23:01:35

or if it's a template, it could be a placeholder for adding the options you expect

👍 3
Bret Horne23:01:33

yeah fair (Even mentions in the article that the following could be used as a template. I misunderstood.)