This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-05-03
Channels
- # aleph (6)
- # announcements (4)
- # babashka (73)
- # beginners (117)
- # calva (25)
- # chlorine-clover (59)
- # cider (21)
- # clara (3)
- # cljdoc (8)
- # cljs-dev (54)
- # cljsrn (15)
- # clojure (65)
- # clojure-france (5)
- # clojure-spec (3)
- # clojure-uk (13)
- # clojurescript (79)
- # conf-proposals (1)
- # conjure (17)
- # core-logic (11)
- # datomic (21)
- # fulcro (82)
- # graalvm (11)
- # helix (7)
- # jobs-discuss (11)
- # joker (2)
- # juxt (3)
- # local-first-clojure (1)
- # luminus (5)
- # nrepl (61)
- # off-topic (12)
- # pathom (70)
- # re-frame (3)
- # reitit (3)
- # rum (1)
- # shadow-cljs (58)
- # sql (1)
- # tools-deps (26)
- # xtdb (3)
Is it possible to have a variable inside regex? Like when I use re-matches, can I have like a variable from outside? Something like:
(def char-to-find \C)
(re-seq #"{char-to-find}" s)
you can use re-pattern
to make a pattern from a string
and use format
or whatever to make the string
If you want to count occurrences of individual characters in a string, is it within the entire string, or only a part matched by the regex?
I thought of other methods but maybe it's cause im bad, but I thought that regex would be the best
If you can be more specific about what kind of string you want as input, and what you want to count, that might help. If you want to count all characters in a string, i.e. its length in characters, that is just (count my-string)
.
If you want to know how many times each different character occurs in a string, you can do (frequencies my-string)
If it is in the entire string, then you don't need a regex to do that.
If it is in a single part, or several parts, of a string that match a regex, then I would recommend thinking about first finding those matching parts, then doing a pass over those matched strings that count how many times each character occurs in them, using a second function that doesn't use regexes.
Those are all efficiency concerns, not correctness, really, but maybe you are also concerned about efficiency.
If you need N different regexes, then dynamically generating N different regexes at run time, and matching a string against all N of them, should work correctly.
How do you work with byte[] in Clojure? I'm using a library that gives me a byte[], offset, and length. I'm trying to get a string out of it and I'm not really sure what to do.
I tried using subvec to get the bytes I care about out, but it doesn't seem to work with a byte[]
to convert from byte-array to clojure vector. you can do (vec my-byte-array)
to get a byte array from a vector, you can do (byte-array my-vec)
Thanks for the help. I found an easier way to do what I wanted. (String. buffer offset length)
I guess using Java's String is easier as it can create one from a byte array with an offset and length.
I was exploring various Ring middlewares and then found enduro > enduro provides a reference type similar toĀ http://clojure.org/atoms, except that enduro's is durable - its contents can be persisted as Clojure data to some durable backing store. https://github.com/alandipert/enduro This is so nice. Much easier than manually persisting/restoring an atom. Has anyone used it before?
how do i know where the wrong message is,too much debug message shown,and i can't find it
java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to java.lang.Number
at (Numbers.java:229)
clojure.lang.Numbers.max (Numbers.java:4027)
euler_project.11_20$solution_11.invokeStatic (form-init7724603543419221011.clj:99)
euler_project.11_20$solution_11.invoke (form-init7724603543419221011.clj:6)
euler_project.11_20$eval1436.invokeStatic (form-init7724603543419221011.clj:210)
euler_project.11_20$eval1436.invoke (form-init7724603543419221011.clj:210)
clojure.lang.Compiler.eval (Compiler.java:6927)
clojure.lang.Compiler.eval (Compiler.java:6890)
clojure.core$eval.invokeStatic (core.clj:3105)
clojure.core$eval.invoke (core.clj:3101)
clojure.main$repl$read_eval_print__7408$fn__7411.invoke (main.clj:240)
clojure.main$repl$read_eval_print__7408.invoke (main.clj:240)
clojure.main$repl$fn__7417.invoke (main.clj:258)
clojure.main$repl.invokeStatic (main.clj:258)
clojure.main$repl.doInvoke (main.clj:174)
clojure.lang.RestFn.invoke (RestFn.java:1523)
clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__697.invoke (interruptible_eval.clj:87)
clojure.lang.AFn.applyToHelper (AFn.java:152)
clojure.lang.AFn.applyTo (AFn.java:144)
clojure.core$apply.invokeStatic (core.clj:646)
clojure.core$with_bindings_STAR_.invokeStatic (core.clj:1881)
clojure.core$with_bindings_STAR_.doInvoke (core.clj:1881)
clojure.lang.RestFn.invoke (RestFn.java:425)
clojure.tools.nrepl.middleware.interruptible_eval$evaluate.invokeStatic (interruptible_eval.clj:85)
clojure.tools.nrepl.middleware.interruptible_eval$evaluate.invoke (interruptible_eval.clj:55)
clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__742$fn__745.invoke (interruptible_eval.clj:222)
clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__737.invoke (interruptible_eval.clj:190)
clojure.lang.AFn.run (AFn.java:22)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
java.lang.Thread.run (Thread.java:748)
it tells you the exception:
java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to java.lang.Number
It's a bit tricky because that would require analyzing the stack. You can wrap it in try-catch which would return the exception
I want to know if there is better solution for https://projecteuler.net/problem=12, this is my solution,and it solves too slowly
(defn solution-12 []
(let [tran-nums (fn [num]
(cons (filterv #(zero? (rem num %)) (range 1 num))
num))]
(loop [n 1
c (count (tran-nums n))]
(if (= c 500)
n
(recur (inc n)
(count (tran-nums (inc n))))))))
Probably you need to use prime factorization to calculate the number of factors: https://mathschallenge.net/index.php?section=faq&ref=number/number_of_divisors
what the hell is this??
=> (Integer. "5537376230")
NumberFormatException For input string: "5537376230" java.lang.NumberFormatException.forInputString (NumberFormatException.java:65)
2147483647
is the maximum value for a java integer
hi all,
i have this reference of vector of maps:
(def l (ref [{:a 1 :c false} {:a 2 :c false} {:a 3 :c false}]))
and i apply a filter on it to fetch the desired element, example:
(filter #(= (:a %) 1) (deref a))
now what i want to do is to apply the filter and at the same time change the :c value in the referenced map.. so the above would then become:
#<Ref@5f8b3cd8: [{:a 1, :c true} {:a 2, :c false} {:a 3, :c false}]>
is there some way to do this without having to iterate the ref map again just to change the desired value?
(ns sudokusolver.logic
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :refer :all]
[clojure.core.logic.fd :as fd]))
For some reason I'm getting an ns
spec error, but I can't see what's wrong with my ns
decelerationmuch preferable in general to give an alias in my opinion. [clojure.core.logic :as l]
if I have a record Card and I use (map->Card {...})
, is there an opposite function (Card->map #Card{...})
?
trying to figure out the source of a bug using differ that leads to java.lang.UnsupportedOperationException: Can't create empty: game.core.card.Card
was wondering if it had to do with using a record instead of a map
empty on records wonāt work (records inherently have keys)
if you post a fuller stacktrace, I suspect you'll see a call to clojure.walk/walk within it
my apologies for the color codes, never figured out how to turn those off
right, so if I can turn the Card objects back to a map, or do some silliness like (Card->map (map->Card {...}))
to get all of the :key nil
, I can explore more
thanks either way
appreciate y'alls input
and then the fun game of do the Card defrecords match up on both sides of the websocket š
if I have a list of kev-value pair vectors (like you get from sequencing a map), is there an easy way to get these back into a map? this is what I have (reduce (fn [m [k v]] (assoc m k v)) {} kvs)
but Iām curious if there is something better
weird @ghadiā¦ I thought I tried that and it didnāt work. worked this time tho, so thanks!!
the gotcha is that into only works with vectors, your reduce above would work with two elements seqs as well
thanks @noisesmith, that makes sense
Hi! Iām trying to use selmer for templating in a small web app. Iām passing variables like current-user, errors, messages when I use selmer.parser/render but now I reach the point where Iām trying to work with ring sessions. I can pass the session map using redirect but how can I pass the other variables I was sending with render?
@orlandomr27 You could just assoc
or merge
the data you need into a single hash map.
(selmer.parser/render template (merge (:session req) {:other :data :goes :here})
-- off the top of my head
I often pass the entire req
hash map to Selmer, and assoc
in any extra data I need, either assoc-in
into :params
or as qualified top-level keys.
Thank you @seancorfield and how could I reference the keys inside req
from inside the template? The same way as other variables? Like for example I want to show the username from :session
{{ :session {{ username}} }} ??
Use dotted notation (per the Selmer docs): {{session.username}}
if you're passing the whole req
{{params.foo}}
to access a parameter
If you're passing just the session hash map, with other data merged in -- per my code above -- then {{username}}
would be the username from session, since the whole hash map is that session data.
It's "just" data -- a potentially nested hash map.
I had a problem using render-file. After login the app was rendering index.html but the url was showing the login route, so I insisted in using redirect and Iāve found a way to do it. session was destructured in function params
(let [next-session (assoc session :session (:user-id "1" :first-name "John"))]
(-> (redirect "/")
(merge next-session {:some-keys some-values)))
Then on ā/ā route I just render-file the index.html with :session param
(parser/render-file "templates/index.html" {:session (:session req) :books books})
It is working just fine and redirecting as it should.
Thanks @seancorfield for remembering me that Ā«itās just data - a potentially nested mapĀ».Nice! IĀ“m going to try that. So there is no need for response.redirect to assoc the session. Thanks!
Passing the whole req
is definitely the easiest way for the view templates to have access to everything they need.
hi everyone,
just out of curiosity
why does (dir clojure.string)
in the lein repl return the names of the functions in that namespace
but in the calva repl return an error Unable to resolve symbol: dir in this context
dir
is a macro defined in clojure.repl
. So if the macro is visible from your current namespace it will work. But in an arbitrary namespace it won't be there
(apply require clojure.main/repl-requires)
is what many repls run in the user namespace so these helpful bindings are present
and the source of clojure.main/repl-requires:
(def ^{:doc "A sequence of lib specs that are applied to `require`
by default when a new command-line REPL is started."} repl-requires
'[[clojure.repl :refer (source apropos dir pst doc find-doc)]
[clojure.java.javadoc :refer (javadoc)]
[clojure.pprint :refer (pp pprint)]])
that's true @dpsutton
I tried (clojure.repl/dir clojure.string)
in the calva repl and it worked
when I discovered clojure.main/repl-requires it also reminded me of the very useful javadoc
function, which I use a lot more now - you can pass it an arbitrary object and it will usually find the web page with that object's api
and if not the query bar is partially filled in for my own google search :D
oh, I haven't tried java12 so haven't run into that
for 13 it falls back to 8. it also doesn't handle outside of java.lang maybe? bit hazy on details now
https://clojure.atlassian.net/browse/CLJ-2534 and relatedly https://clojure.atlassian.net/browse/CLJ-2560
Yeah, I use the internals of javadoc
as part of my Atom/Chlorine/REBL integration so I can view HTML pages inside REBL and navigate between them. I also have an equivalent for ClojureDocs inside REBL (but that's not as sophisticated).
wow, that's cool
(map #(- 15 %) [20 10 35])
(map (partial - 15) [20 10 35])
they do the same thing. But witch one is the best option for use?it's a question of style but I tend to reserve partial for more complex higher order / point free situations, and use #() in a simple case like that one
and I'd switch from #() to fn (ideally with the optional name parameter included) for cases where there's any nested calls
I understand what partial is and it's results. But i still not understand in a practical way how can i solve something with it
partial will return me a function with a parameter already filled and then i have to call this returned function with something that is missed.
Anyway, i'll keep going in my studies
for example (def rotate-table (partial apply mapv vector))
- in this case I'm already working point free, and it's natural to add partial in, it "speaks the same language" as things like apply, comp, juxt etc. which are used in order to stack function calls instead of naming temprorary variables
(defn rotate-table [t] (apply mapv vector t))
and (def rotate-table #(apply mapv %))
would do the same thing, but stylistically partial fits better
there's also cases where partial avoids a call to apply - eg. (partial list :prefix)
as apposed to #(apply list :prefix %&)
since partial makes no assumptions about argument count of the thing it returns
I had a problem using render-file. After login the app was rendering index.html but the url was showing the login route, so I insisted in using redirect and Iāve found a way to do it. session was destructured in function params
(let [next-session (assoc session :session (:user-id "1" :first-name "John"))]
(-> (redirect "/")
(merge next-session {:some-keys some-values)))
Then on ā/ā route I just render-file the index.html with :session param
(parser/render-file "templates/index.html" {:session (:session req) :books books})
It is working just fine and redirecting as it should.
Thanks @seancorfield for remembering me that Ā«itās just data - a potentially nested mapĀ».