This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-12
Channels
- # adventofcode (80)
- # announcements (11)
- # babashka-sci-dev (6)
- # beginners (52)
- # calva (144)
- # clj-kondo (28)
- # cljdoc (2)
- # cljs-dev (7)
- # clojure (120)
- # clojure-dev (28)
- # clojure-europe (36)
- # clojure-nl (1)
- # clojure-norway (16)
- # clojure-uk (3)
- # clojurescript (1)
- # cursive (4)
- # datomic (14)
- # figwheel-main (5)
- # fulcro (23)
- # hoplon (2)
- # hyperfiddle (46)
- # improve-getting-started (10)
- # jobs (1)
- # joyride (1)
- # leiningen (10)
- # malli (5)
- # nbb (5)
- # off-topic (21)
- # polylith (7)
- # portal (15)
- # practicalli (1)
- # rdf (18)
- # re-frame (3)
- # releases (2)
- # remote-jobs (5)
- # shadow-cljs (25)
- # spacemacs (4)
- # tools-deps (16)
- # tree-sitter (1)
For people totally new to functional programing and Clojure, I found the following book to be quite helpful. • Functional Thinking: Paradigm Over Syntax, by Neal Ford
When I tried to write the file in csv
(csv/write-csv writer ["Edward Cullen 12" "Edward Cullen 10" "Jacob Black 3" "Carlisle Cullen 6"])))
Why does it writing in below way
E,d,w,a,r,d, ,C,u,l,l,e,n, ,1,2
E,d,w,a,r,d, ,C,u,l,l,e,n, ,1,0
J,a,c,o,b, ,B,l,a,c,k, ,3
C,a,r,l,i,s,l,e, ,C,u,l,l,e,n, ,6
the default CSV writer assumes each element of the vector you're trying to write is a sequence of cell values. Because strings can be treated sequentially, it writes each character to its own cell. If you want to write a single-column CSV, you should put each row in its own vector like:
[["Edward Cullen 12"] ["Edward Cullen 10"] ["Jacob Black 3"] ["Carlisle Cullen 6"]]
but I'm guessing you want two columns, which would be more like:
[["Edward Cullen" "12"] ["Edward Cullen" "10"] ["Jacob Black" "3"] ["Carlisle Cullen" "6"]]
It is printing with double quote
"Edward Cullen,12"
"Edward Cullen,10"
"Jacob Black,3"
"Carlisle Cullen,6"
If you put the comma inside the string it will print as you've described because a comma inside the string puts the comma inside the cell
(csv/write-csv writer
[["Edward Cullen, 12"]
["Edward Cullen, 10"]
["Jacob Black, 3"]
["Carlisle Cullen, 6"]])
Instead, you can put a comma between strings (each new string is a cell)
(csv/write-csv writer
[["Edward Cullen", "12"]
["Edward Cullen", "10"]
["Jacob Black", "3"]
["Carlisle Cullen", "6"]])
but the commas are optional and treated as whitespace by the clojure interpreter so this also works
(csv/write-csv writer
[["Edward Cullen" "12"]
["Edward Cullen" "10"]
["Jacob Black" "3"]
["Carlisle Cullen" "6"]])
The most clojuresque way of doing it would also leave the numbers without quotes since the csv writer will serialize numbers automatically
(csv/write-csv writer
[["Edward Cullen" 12]
["Edward Cullen" 10]
["Jacob Black" 3]
["Carlisle Cullen" 6]])
I'm trying to track down why some html isn't updating on page reload, but it's a little complicated for me so would appreciate some help. I
have a Compojure route "/" that calls index
, which calls a hiccup (page/html5 ...)
-- in that is a div,
[:div#result
(if (get-in request [:session :identity])
(str (get-in request [:session :identity]))
"Not authorized")]
I have a login and a logout route which assoc's and dissoc's the :identity key into the :session map. That all seems to work, since I have a println at the top of index
that shows me the :session map. The logout
handler is
(defn logout [request]
(-> (redirect "/")
(assoc :session (dissoc (request :session) :identity))))
When that's called it does remove :identity from the :session map, but the result
div on the index page doesn't update to "Not authorized" until I manually refresh the page. The redirect
does trigger a new GET request which I see in the browser dev tools. Is this a hiccup thing where the html is compiled or something until I manually refresh?the funny thing is I see that :identity is gone from the :session map when the redirect runs, but the result div stays the same.
seems like it might be a caching issue. Try using a minimal set of middleware to reproduce the problem
thanks, yeah there is some stale cache. I don't have much middleware atm,
(def app
(-> #'app-routes
(wrap-authentication backend)
(wrap-defaults site-defaults)))
the really odd thing is that if I view source on the page, the div element has the correct string in it
and if I look in the dev tools response, it shows the correct string as rendered html...just not in the FIrefox page
How do I check if a value is in a collection, including if that value is false
or nil
?
(let [m [:foo nil :bar]
x nil]
(pred m x))
; => true
(let [m [:foo :bar]
x nil]
(pred m x))
; => false
Pretty annoying though to still need to check each of nil
and false
separately from the rest of all possible values.
something with :not-found
as sentinel?
e.g. (if-not (= :not-found (get m k :not-found)) ...)
I'm building very generic test generators. nil
and false
are possible values (probable values due to the way generators "grow") that I should expect to deal with, whether or not the consuming test chooses to take advantage of that.
(let [m [:foo nil :bar]
x some-value-which-could-be-nil-false
(pred m x))
it's more tricky with (some #{nil} [0 nil 1])
, because that gives you nil
either way 🙂
the collection is completely generic sequence?
#{nil}
is totally fine
I'm making generators to generate functions. User gives a generator to make input value(s), a generator to make output value(s), (depending one or the other of those might depend on the generated results of the other), and in this variation a generator for how many input-output pairs to match each other. Super generic. I'm trying to use the input value(s) as a key to the corresponding return value for the generated function.
if it doesn't have to be super-efficient, you can just walk the sequence linearly until you find the element that is =
to the one you're looking for?
(let [m [:foo nil :bar false]
x nil]
(some #(= x %) m))
I think this should work but I'm pretty sure it's O(n)
edit: Wrap it with boolean
to get a boolean resultYeah. I think that's what I was looking for. The collection will be a seq or vector, which will be zipmapped as keys to another seq/vec of values. Then the search is done on the original key seq. So I don't think I'm likely to outperform that without going SuperNerd on the problem.
with sets, you can use contains?
to avoid the falsy problem
(contains? #{nil} nil)
will be true and be faster than linear iteration (i.e some
) for large sets
I have a weird issue here I haven't seen before -- what does it mean if the html rendered on a web browser page has a dynamically rendered string, call it "foo", but if you view source, and view the response part of the GET in browser dev tools, both of those show "bar". In this case "bar" is correct.
I don't know. But the cache-control header (served by your web server) can declare circumstances under which the browser can use an outdated, cached version of the page (which says "foo") while it is busy fetching a new version of the page (which says "bar"). Perhaps that's what you are seeing?
yeah I was wondering the same thing...in this case though cache-control of the request is Cache-Control: no-cache