beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
2021-01-21T00:44:23.416Z

I would recommend giving the function a name, and not using those symbols

Jorge Tovar 2021-01-21T00:44:57.416600Z

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?

2021-01-21T00:45:58.416700Z

people have enough trouble with prefix notation using < and >, then the way >= works for multiple arguments leads you into a philosophical crisis

2021-01-21T00:49:47.000400Z

re-use in what way?

2021-01-21T00:49:55.000700Z

you are already doing it

2021-01-21T00:50:29.001400Z

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

dpsutton 2021-01-21T01:06:22.002900Z

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

Jorge Tovar 2021-01-21T01:07:32.003100Z

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

Jorge Tovar 2021-01-21T01:08:36.003300Z

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

seancorfield 2021-01-21T01:11:06.005Z

(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 Tovar 2021-01-21T01:12:20.005700Z

Thank you so much. Thats exactly what im looking for

seancorfield 2021-01-21T01:13:10.005900Z

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

Jorge Tovar 2021-01-21T01:13:46.006100Z

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

seancorfield 2021-01-21T01:14:26.006300Z

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 Tovar 2021-01-21T01:15:29.006600Z

thats a good point

dpsutton 2021-01-21T01:18:09.007300Z

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

Jorge Tovar 2021-01-21T01:18:36.007500Z

Its an example

dpsutton 2021-01-21T01:54:08.008100Z

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

2021-01-21T02:01:34.008300Z

Yes, make everything visible

Jorge Tovar 2021-01-21T02:01:55.008500Z

OK no problem

Hlodowig 2021-01-21T06:30:53.010Z

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?

Hlodowig 2021-01-21T06:34:00.010800Z

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

Ovidiu Stoica 2021-01-21T07:02:23.011500Z

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

Ovidiu Stoica 2021-01-21T07:02:55.012300Z

If I use the actual db for both commands this works

Ovidiu Stoica 2021-01-21T07:03:26.012600Z

But if I use conn I get

Ovidiu Stoica 2021-01-21T07:03:30.012700Z

Ovidiu Stoica 2021-01-21T07:04:47.013700Z

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

Ovidiu Stoica 2021-01-21T07:13:26.014400Z

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

roelof 2021-01-21T11:07:37.015200Z

I have made this layout in hiccup

roelof 2021-01-21T11:08:09.016100Z

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

roelof 2021-01-21T11:08:29.016500Z

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

Piotr Brzeziński 2021-01-21T11:20:08.017Z

Global window you mean like a modal?

roelof 2021-01-21T11:20:47.017300Z

yep, a overlay with a modal screen

roelof 2021-01-21T11:21:13.017800Z

but first I have to make this in react or reframe

Piotr Brzeziński 2021-01-21T11:27:01.020700Z

Maybe this is gonna be helpful https://codepen.io/kotomoika/pen/RVOKxR

Piotr Brzeziński 2021-01-21T11:27:09.021200Z

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

roelof 2021-01-21T11:32:35.021500Z

Thanks, I will look at it

roelof 2021-01-21T11:33:03.022Z

just following a course on re-frame

borkdude 2021-01-21T11:41:46.022300Z

@roelof are you using bootstrap or something like this?

roelof 2021-01-21T11:42:16.022900Z

fotr that one no bootstrap is used

roelof 2021-01-21T11:42:22.023200Z

only css and html

roelof 2021-01-21T11:43:15.024100Z

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

borkdude 2021-01-21T11:44:52.025400Z

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

borkdude 2021-01-21T11:45:09.025800Z

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

roelof 2021-01-21T11:46:00.026200Z

oke, but I mean a step before the modal

roelof 2021-01-21T11:46:51.027100Z

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

roelof 2021-01-21T11:47:23.028100Z

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

borkdude 2021-01-21T11:48:43.029600Z

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...)

roelof 2021-01-21T11:49:03.030Z

I know

borkdude 2021-01-21T11:49:26.030500Z

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

roelof 2021-01-21T11:50:32.031700Z

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

borkdude 2021-01-21T11:51:24.032200Z

yeah, you can do that

roelof 2021-01-21T11:53:37.032800Z

I think first finisched the free course on re-frame course https://www.jacekschae.com/courses/learn-re-frame-free/78034-navigation/222916-09-nav-subs

roelof 2021-01-21T14:23:09.033300Z

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

roelof 2021-01-21T14:23:27.033800Z

the last image is totally at a wrong place

roelof 2021-01-21T14:24:01.034200Z

Can anyone help me figure this out

2021-01-21T14:29:35.035100Z

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.

2021-01-21T14:51:55.035300Z

Is it a hiccup problem or a CSS problem?

2021-01-21T14:52:32.035500Z

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

roelof 2021-01-21T14:53:02.035700Z

the display it totally wrong

roelof 2021-01-21T14:58:58.036400Z

2021-01-21T15:00:41.037Z

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

roelof 2021-01-21T15:01:34.037200Z

right now for the repl with (start-server)

1👍
2021-01-21T15:12:13.037600Z

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

2021-01-21T15:14:27.037800Z

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

roelof 2021-01-21T15:15:43.038Z

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

omendozar 2021-01-21T15:56:50.039900Z

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

roelof 2021-01-21T16:07:19.040200Z

did you send me a PR ?

2021-01-21T16:07:39.040400Z

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.

2021-01-21T16:09:18.040600Z

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.

2021-01-21T16:10:07.040800Z

What do your tests look like?

roelof 2021-01-21T16:10:53.041Z

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

2021-01-21T16:11:44.041200Z

:thumbsup:

2021-01-21T16:21:47.041800Z

Here’s a quick test as an example:

(require '[clojure.test :refer [deftest testing is run-tests]])
=&gt; 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 (&gt; 2 1))))
=&gt; #'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.
=&gt; {:test 1, :pass 2, :fail 1, :error 0, :type :summary}

1👍
2021-01-21T16:23:31.042Z

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

2021-01-21T16:43:12.042500Z

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

2021-01-21T16:44:34.042700Z

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

roelof 2021-01-21T16:59:51.043200Z

but sometimes I get lost in the parentheses and so on

roelof 2021-01-21T17:00:01.043400Z

many thanks for the PR

roelof 2021-01-21T17:06:08.043600Z

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

phronmophobic 2021-01-21T17:40:53.043800Z

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

roelof 2021-01-21T17:50:16.044400Z

hmm, how can I add this with hiccup : `

&lt;script src="<https://kit.fontawesome.com/6f1527f147.js>" crossorigin="anonymous"&gt;&lt;/script&gt;

roelof 2021-01-21T17:54:27.045Z

my app method looks like this : |

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

roelof 2021-01-21T17:55:32.045600Z

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)])

pavlosmelissinos 2021-01-21T18:00:53.045800Z

what have you tried so far?

sova-soars-the-sora 2021-01-21T18:03:43.047400Z

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 😛

pavlosmelissinos 2021-01-21T18:06:22.047500Z

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

pavlosmelissinos 2021-01-21T18:08:26.048Z

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

roelof 2021-01-21T18:10:59.048300Z

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

pavlosmelissinos 2021-01-21T18:11:33.048500Z

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

pavlosmelissinos 2021-01-21T18:12:08.048700Z

But you can build it yourself using only clojure data structures

roelof 2021-01-21T18:12:17.048900Z

??

roelof 2021-01-21T18:14:33.049100Z

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

pavlosmelissinos 2021-01-21T18:14:42.049300Z

yup, exactly that

pavlosmelissinos 2021-01-21T18:15:09.049500Z

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

"&lt;span class=\"foo\"&gt;bar&lt;/span&gt;"
Modify the contents of the vector to get the output you want

pavlosmelissinos 2021-01-21T18:16:34.049900Z

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

roelof 2021-01-21T18:23:11.050100Z

yep

roelof 2021-01-21T18:23:31.050300Z

do not know if this is working `

&lt;link crossorigin="anymous" href="<https://kit.fontawesome.com/6f1527f147.js>" rel="js"&gt;

1
roelof 2021-01-21T18:26:54.051100Z

hmm, this looks better

&lt;script crossorigin="anymous" href="<https://kit.fontawesome.com/6f1527f147.js>"&gt;&lt;/script&gt;

roelof 2021-01-21T18:27:05.051300Z

but still no icon to see

pavlosmelissinos 2021-01-21T18:28:21.051500Z

Better than before but still different from your target:

&lt;script src="<https://kit.fontawesome.com/6f1527f147.js>" crossorigin="anonymous"&gt;&lt;/script&gt;

roelof 2021-01-21T18:28:35.051700Z

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

pavlosmelissinos 2021-01-21T18:28:48.051900Z

You need to pay more attention to detail,

1👀
pavlosmelissinos 2021-01-21T18:29:16.052100Z

you have a script element

pavlosmelissinos 2021-01-21T18:29:31.052300Z

and you want to load a js file

pavlosmelissinos 2021-01-21T18:29:41.052500Z

what attribute do you use to do that?

roelof 2021-01-21T18:29:59.052700Z

a src instead of a href

roelof 2021-01-21T18:30:08.052900Z

and I see a very small icon 🙂

1👌
pavlosmelissinos 2021-01-21T18:31:56.053200Z

you also have a typo

pavlosmelissinos 2021-01-21T18:32:04.053400Z

anymous -> anonymous

roelof 2021-01-21T18:51:21.053600Z

thanks, look well

roelof 2021-01-21T18:51:28.053800Z

1🎉
roelof 2021-01-21T18:53:00.054200Z

tomorrw and the dasys after it work on the next page

roelof 2021-01-21T21:22:18.056Z

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))

sova-soars-the-sora 2021-01-22T23:21:21.167600Z

Might be worth looking at the source of "flatten" https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core.clj#L7136

alexmiller 2021-01-21T21:34:34.056400Z

why not just use flatten?

roelof 2021-01-22T08:40:41.094Z

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

practicalli-john 2021-01-21T21:46:42.060Z

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"}]) 

phronmophobic 2021-01-21T21:49:53.060400Z

you could use some rather than map

1👍
dpsutton 2021-01-21T21:51:57.061100Z

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

practicalli-john 2021-01-21T21:52:36.062Z

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

dpsutton 2021-01-21T21:53:32.062200Z

then you're never lost 🙂

2
phronmophobic 2021-01-21T21:57:12.063300Z

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

1👍
practicalli-john 2021-01-21T22:05:26.066600Z

I think I am okay with a falsey nil 🙂

1👍
Bret Horne 2021-01-21T23:13:47.071100Z

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?

2021-01-21T23:15:17.071400Z

You may be interested in https://clojure.org/guides/destructuring

2021-01-21T23:15:43.072100Z

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

Bret Horne 2021-01-21T23:15:50.072300Z

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

2021-01-21T23:16:23.073100Z

I don’t think an empty vector does much

Bret Horne 2021-01-21T23:17:43.074800Z

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"}

phronmophobic 2021-01-21T23:17:52.075100Z

an empty vector for :keys doesn't do anything

Bret Horne 2021-01-21T23:18:08.075400Z

ah is it merely needed for using :as blah?

phronmophobic 2021-01-21T23:18:30.076100Z

you can just have :as blah without the :keys

2021-01-21T23:18:42.076500Z

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

1👍
Bret Horne 2021-01-21T23:19:09.076900Z

oh! duh 😛

dpsutton 2021-01-21T23:19:53.077900Z

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

2👍
Bret Horne 2021-01-21T23:20:00.078100Z

(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 Horne 2021-01-21T23:20:24.079Z

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

2021-01-21T23:23:00.081100Z

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 Horne 2021-01-21T23:23:20.081700Z

(thanks for the help!)

phronmophobic 2021-01-21T23:23:35.082100Z

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

1👍
Bret Horne 2021-01-21T23:24:33.082800Z

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

2021-01-21T23:52:37.083300Z

Ya, peek is given a stack, returns the last inserted element. So its LIFO. first is given an ordered collection, return the first one from its order

2021-01-21T23:53:25.083500Z

And it seems that peek can also be: given a queue, return the first inserted element FIFO

2021-01-21T23:54:09.083700Z

Basically, it does something different based on the type of coll, so just make sure you are aware what type of coll you use it with

2021-01-21T23:54:52.083900Z

So my trick is: You peek from a stack or queue. And Lists and Vectors are also Stacks