This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-01-29
Channels
- # announcements (17)
- # aws (12)
- # babashka (27)
- # beginners (85)
- # bristol-clojurians (1)
- # calva (16)
- # cider (3)
- # clara (7)
- # clojure (80)
- # clojure-europe (13)
- # clojure-italy (19)
- # clojure-nl (2)
- # clojure-norway (6)
- # clojure-poland (1)
- # clojure-spec (31)
- # clojure-uk (61)
- # clojurescript (29)
- # core-async (10)
- # cursive (7)
- # data-science (1)
- # datomic (29)
- # docker (3)
- # fulcro (120)
- # graphql (16)
- # hugsql (2)
- # leiningen (17)
- # luminus (2)
- # off-topic (36)
- # other-languages (3)
- # pathom (13)
- # re-frame (12)
- # ring (2)
- # rum (1)
- # shadow-cljs (126)
- # tools-deps (56)
- # vscode (5)
Curious, is there a bult-in predicate that returns true for list-like things (vectors, lists) but not strings/maps?
sequential?
I can never remember what sequential?
returns true vs. false for, but I would check that.
it doesn't work for eg. arrays or java.util.List, but it's the right thing for ordered clojure immutable collections
nb. also it returns false for sets
Its doc string is probably 100% accurate, but tells you very little if you do not know the internal implementation details of data structures.
user=> (pprint (group-by sequential? ["hi" 3 [] clojure.lang.PersistentQueue/EMPTY {} #{} () nil]))
{false ["hi" 3 {} #{} nil], true [[] <-()-< ()]}
nil
The http://Clojuredocs.org page for it is pretty good: https://clojuredocs.org/clojure.core/sequential_q
when did we get that nice pr for queues? I like it
That is probably some REPL plugin thingy you have. Not part of default Clojure REPL
There are also return values of seq
that at least some (maybe all except nil?) that sequential returns true
for, e.g. (sequential? (seq {:a 1}))
is true
But I may be getting too far off into the weeds there in my wondering. Part of the reason I am confused by it is that I have actually looked at half a dozen different implementations of seq
under the hood in Clojure/Java.
hmm, I don't have any plugins - this is just vanilla clj with no deps.edn file
the fish version only shows up with pprint, not normal pr
Ah, yes, I see it, too. I wasn't using pprint before.
anyway, it's a pleasant surprise
Looks like it's been in there since Clojure 1.2.
1.1 doesn't have clojure.pprint
I found the commit https://github.com/clojure/clojure/commit/d1e39b1ec7fc65907b13458d7ec70b0839f3f85e May 12, 2010
$ clojure -A:1.2 -e "(println *clojure-version*)" -e "(require 'clojure.pprint)" -e "(clojure.pprint/pprint (conj clojure.lang.PersistentQueue/EMPTY :a))"
{:major 1, :minor 2, :incremental 1, :qualifier }
<-(:a)-<
(well, that's 1.2.1)
Why is defn /
valid but defn //
is not? Also, suggest me a name for a division operation which repeatedly divides x with y and returns the count, e.g. (? 125 5) => 3
and (? 250 5) => 3
.
"'/' has special meaning, it can be used once in the middle of a symbol to separate the namespace from the name, e.g. my-namespace/foo. '/' by itself names the division function."
@hindol.adhya You're looking for the opposite of power/exponent?
why not?
I went with times-divisible
last time, but that's too long. I will be using this frequently.
Some variant of the name log
seems appropriate for what it does, perhaps with a letter thrown in to mean "floor of log" or "log, truncated"
@U0CMVHBL2 Because the calculation method is different and also because log returns a double. My method returns a whole number.
e.g. tlog
/ is not going to work well in Clojure,nor anything that contains /
%%
is valid?
"Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, ', ?, <, > and = (other characters may be allowed eventually)."
Ya, I'd say its a little icky as well, since it won't work inside #()
cause it'll conflict with that form's %
Many things that are not on the list of officially supported characters are not explicitly disallowed -- they just aren't on the officially supported list.
/ is one of the few on the explicitly disallowed (or very special restricted use) list.
One hack is if you define a /
fn in some namespace, and then alias it to say m
you can do (m// 10 5)
Thanks everyone for pitching in. Instead of fighting against the system, I think I'll go with tlog
for now.
That would probably be my approach to this. Define a new /
in a namespace that can get a nice, short alias.
user=> (ns times.div (:refer-clojure :exclude [/]))
nil
times.div=> (defn / [a b] (count (take-while #(<= b %) (reductions clojure.core// a (repeat b)))))
#'times.div//
times.div=> (ns user)
nil
user=> (require '[times.div :as *])
nil
user=> (*// 250 5)
3
user=> (*// 125 5)
3
user=>
(updated to add :refer-clojure :exclude
so the warning about redefinition doesn't appear)Reviving and old thread, but I did a cool (to me) implementation of int-log
.
(defn int-log
"Returns how many times b can evenly divide x."
[x b]
(let [squares (reverse
(map vector
(iterate #(* 2 %) 1)
(take-while #(zero? (rem x %))
(iterate #(*' % %) b))))]
(loop [[[i m] [j n] & squares] squares]
(if (nil? n)
i
(if (zero? (rem x (* m n)))
(recur (cons [(+ i j) (* m n)] squares))
(recur (cons [i m] squares)))))))
This one calculates the exponent in logarithmic time O(log n)
.
Benchmarked against a naive O(n)
implementation.
(defn int-log-naive
[x b]
(count
(->> (iterate #(*' b %) b)
(take-while #(zero? (rem x %))))))
(defn -main
[& _]
(let [base 29
raised (math/expt base 1000)]
(dotimes [_ 1000]
(? raised base))))
(time
(-main))
int-log: "ElapsedĀ time:Ā 113.5178Ā msecs"
int-log-naive: "ElapsedĀ time:Ā 11103.9029Ā msecs"
For (int-log 1250 5)
,
1. collect enough squares [5 25 625]
2. start with the highest square 625
3. (* 625 25)
divides 1250? if not, drop 25. (* 625 5)
divides 1250? if not drop 5.
is there a channel in this Slack about this book https://dragan.rocks/articles/19/Judge-a-clojure-programming-book-by-its-cover ? š
https://dragan.rocks/articles/18/Ditching-Slack-Opening-Place-Personal-Interaction
Would anyone be able to point me to some information on why a ClojureScript application (re-frame template +handler) wouldnāt show updates on a custom domain when changes are pushed to master with GitHub integration, but will show them on the herokuapp? Iāve ran git add ., git push heroku master(:master), lein prod, lein with-profile prod uberjar, pulled my changes into master and committed that, Iāve ran git push --force, and still, the changes will not take to the new site? Iāve scoured through some S/O answers but most of them are for languages/platforms that arenāt Clojure/S.
Just a dumb question, but have you eliminated caching? Each browser has a different way of busting the cache -- in Chrome, shift-click on reload, for example.
Oh my dear god that was it. That was it! Thanks so much, Iāll never forget it.
@UHR1B0NJG For future reference: There is a small trick to always force the browser to use the most recent version of your script and ignore cache.
When loading your script you can add a version-identifier as any query parameter. For example: app.js?v=1.0
as soon as the browser sees another query string it won't load from cache
We sometimes use this to ensure that clients get the newest version and we don't get calls which are resolved by a simple cache reload
I know the angular bundler solves this by adding some random string to the end of its generated js files
so for v1.0.0 all of the built js will be bundle-45ASDB123.js and the index.html file will reference that, and when you bump to v1.0.1 that random string will change
Hey everyone, I'm having trouble managing response types on a API, like sending a 200, 422, or any other status, does anyone have this problem too? I'm using compojure to design it
how are you setting the response status?
there's a helper, but a hash-map {:body ... :status N}
should do the trick
I'm trying to figure out how to set up the responses types without applying that on the business layer
If I add the logic to send back a 422 for example, where would I put it? On the API layer or the business layer? If I add it in the business layer, it wouldn't really be a business layer anymore, right?
likely a middleware that takes your handler as an argument and returns a new handler that validates that the request can be processed, and falls back to 422
but I haven't isolated my layers in as disciplined way as you are describing, so I might be missing a better option
I'm also slightly confused by your usage of the term "response type" here because to me that's somehting like application/json
which clearly isn't what you mean
sorry, I mean sending back a 422 when someone sends a invalid entity, like a missing field or wrong information type
OK yeah I'd definitely write a wrap-validate
middleware and wrap the handler with it (inside the route if it's endpoint specific, around the router if it's generic)
hmm, ok I'll take a look on how to create middlewares then, maybe that's what I'm missing
the platonic form of a handler middleware:
(defn wrap-foo
[handler-function]
(fn new-handler-function [request]
(post-function (handler-function (pre-function request)))))
it takes a handler, and returns a new one that might do something with data before the handler is called, and might do something after the handler is calleda validator is probably more like this:
(defn wrap-validate
[handler-function]
(fn [request]
(let [validation-error (validate request)]
(if validation-error
{:status 422 :body validation-error}
(handler-function request)))))
where validiate
returns nil or false for a good input, and a message for a bad one
so instead of using layers per-se (or as well as), you can isolate concerns into middleware, and compose them on each other to get all your needed features (there are a lot of great middleware that come with ring, many are applied already by default)
that is a interesting approach, I'll have to think more on that and decide what to do
and another variation is that wrap-validate
can take another arg- the validator rules (or validator function) and individually get wrapped on different contexts
since in ring the router itself is wrappable (for things that apply to all routes) but so are individual endpoints (for things that are route or subtree specific)
right - they are flexible enough to be applied at either level
(or maybe better to say, ring's design is such that they can be applied at either level)