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

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

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

👍 1
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 🙂

2
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

👍 1
practicalli_john22:01:26

I think I am okay with a falsey nil 🙂

👍 1
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

👍 1
Bret Horne23:01:09

oh! duh 😛

dpsutton23:01:53

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

👍 2
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

👍 1
Bret Horne23:01:33

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