I would recommend giving the function a name, and not using those symbols
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?people have enough trouble with prefix notation using < and >, then the way >= works for multiple arguments leads you into a philosophical crisis
re-use in what way?
you are already doing it
the values are bound to names, the names are in scope and usable from both arity bodies
What does that mean? They are def’ed in the namespace. They are visible in all scopes below their definition
yes, but what If I wanna make this values private.
like (let [cost 0])
yes I like the current solution, but def values are visible in (:require [my.namespaces]). no?
(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`Thank you so much. Thats exactly what im looking for
Having default positional arguments is what is causing you problems here really. You might want to rethink that...
yes. The test failed. Im gonna leave as def values, I think is good enough by the moment, thank you
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".
thats a good point
Only forms inside of that let block can see cost. Seems pretty private to me
Its an example
What’s an example where you’d actually like them private then?
Yes, make everything visible
OK no problem
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?
I know my way around AWS reasonably well but I just noticed Heroku supports Clojure.
Hei guys, so I have this next.jdbc
function:
If I use the actual db
for both commands this works
But if I use conn
I get
None of the tables have a id
column, and I would love to use the same db connection for both queries
nevermind, I found the issue. conn
is not passed the kebab-snake-case options of the db . Fixed 👍
I have made this layout in hiccup
Can someone help me to make this in react so I can show a global window to show data of a painting
I mean more think in what are the components to make this work ?
Global window you mean like a modal?
yep, a overlay with a modal screen
but first I have to make this in react or reframe
Maybe this is gonna be helpful https://codepen.io/kotomoika/pen/RVOKxR
It uses classes but shouldn’t be hard to move it to hooks
Thanks, I will look at it
just following a course on re-frame
@roelof are you using bootstrap or something like this?
fotr that one no bootstrap is used
only css and html
see here for the html and css I used : https://codepen.io/rwobben/pen/xxVepxG
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
the same is true for reagent-only but you can do it with atoms
oke, but I mean a step before the modal
can I make the layout with re-frame and if so , are all the paintings a componennt and all the shelves
or can it be one big component. That is really my question
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...)I know
you can split up things in as many components as you would like, just as when writing functions
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
yeah, you can do that
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
pff, now hiccup is messing with my layout code so far : https://github.com/RoelofWobben/paintings
the last image is totally at a wrong place
Can anyone help me figure this out
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.
Is it a hiccup problem or a CSS problem?
I mean is the underlying HTML wrong, or is the display rendering incorrectly in the browser?
the display it totally wrong
Ok, let me look at it for a bit. How do you launch the server?
right now for the repl with (start-server)
Darn, got a hot issue just came up at work, it’ll be a while…
i ended up running invidual test using :only ... i wish test would pinpoint the failing test upfront and show the error reason
no problem. This is a toy project to learn and practice with clojure web development
Hi, is there a lib in Clojure for the SMPP protocol? To enable SMS messaging between applications and mobiles?
did you send me a PR ?
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.
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.
What do your tests look like?
yep, I use VS code with Calva and it seems to have rainbow parens
:thumbsup:
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}
it's a test suite from exercism: https://pastebin.pl/view/5c8c3751
strange... lein test works as expected now...
i guess in the previous one, an error blocked the tests from even running...
but sometimes I get lost in the parentheses and so on
many thanks for the PR
now I can further experiment with something for the prev and next links
I haven't tried using SMPP, but I have used Twilio's clojure library to successfully send text messages
hmm, how can I add this with hiccup : `
<script src="<https://kit.fontawesome.com/6f1527f147.js>" crossorigin="anonymous"></script>
my app method looks like this : |
(defroutes app
(GET "/" [] (-> (memo-display-data)
(display/generate-html)))
(route/resources "/")
(GET "/favicon.ico" [] ""))
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)])
what have you tried so far?
'(include-js {:src https://kit.fontawesome.com/6f1527f147.jshttps://kit.fontawesome.com/6f1527f147.js})`
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 😛
Have you seen the API docs for hiccup? > Include a list of external javascript files. include-js expects a list, not a map
https://weavejester.github.io/hiccup/hiccup.page.html#var-include-js
If you click on view source
you should be able to understand what the "list of files" should look like.
oke, but how can I then add thaty cross-origin part
include-js doesn't seem to allow you to do that
But you can build it yourself using only clojure data structures
??
or do you mean I could do something like this : [:link {:href "/css/styles.css", :rel "stylesheet"}]]
yup, exactly that
(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 wantMake sure to try it out on a REPL as you modify it to get instant feedback 😉
yep
do not know if this is working
`
<link crossorigin="anymous" href="<https://kit.fontawesome.com/6f1527f147.js>" rel="js">
hmm, this looks better
<script crossorigin="anymous" href="<https://kit.fontawesome.com/6f1527f147.js>"></script>
but still no icon to see
Better than before but still different from your target:
<script src="<https://kit.fontawesome.com/6f1527f147.js>" crossorigin="anonymous"></script>
hmm, now I have to find out why the script is not loaded
You need to pay more attention to detail,
you have a script element
and you want to load a js file
what attribute do you use to do that?
a src instead of a href
and I see a very small icon 🙂
you also have a typo
anymous -> anonymous
thanks, look well
tomorrw and the dasys after it work on the next page
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))
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
why not just use flatten
?
that is forbidden. I think we have to re-implement flatten
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"}])
you could use some
rather than map
(some (comp #{value} :key) coll)
sometimes my brain only lets me type map - I think I am map obsessed :)
then you're never lost 🙂
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
I think I am okay with a falsey nil 🙂
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
?You may be interested in https://clojure.org/guides/destructuring
That said, I don’t understand what you are asking, could you elaborate ^^
I’ve scoured it…Am I mistakenly missing it? I dont see an instance of having an empty vector.
I don’t think an empty vector does much
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"}
an empty vector for :keys
doesn't do anything
ah is it merely needed for using :as blah?
you can just have :as blah without the :keys
no, not even that (defn hello [{:as rest}] …)
or (defn hello [rest] …)
would also both work
oh! duh 😛
"from the map, please bind no keys and call the whole thing rest"
(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.
(defn main [args {:keys [] :as opts}] ...)
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 🙂
(thanks for the help!)
or if it's a template, it could be a placeholder for adding the options you expect
yeah fair (Even mentions in the article that the following could be used as a template. I misunderstood.)