This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-06
Channels
- # announcements (12)
- # asami (3)
- # babashka (59)
- # beginners (20)
- # biff (1)
- # calva (87)
- # cherry (8)
- # clj-kondo (41)
- # clj-together (4)
- # cljdoc (5)
- # cljfx (4)
- # cljs-dev (2)
- # cljsrn (6)
- # clojure (63)
- # clojure-europe (22)
- # clojure-nl (1)
- # clojure-norway (35)
- # clojure-uk (4)
- # clojurescript (5)
- # conjure (2)
- # datalevin (4)
- # datascript (8)
- # datomic (16)
- # events (1)
- # figwheel-main (1)
- # fulcro (9)
- # hyperfiddle (4)
- # introduce-yourself (1)
- # jobs (3)
- # kaocha (10)
- # lambdaisland (2)
- # lumo (7)
- # nbb (1)
- # off-topic (29)
- # pathom (15)
- # re-frame (80)
- # releases (1)
- # remote-jobs (4)
- # shadow-cljs (13)
- # spacemacs (9)
- # sql (25)
- # squint (32)
- # tools-deps (6)
- # uncomplicate (6)
- # xtdb (15)
I think I've run into an interesting edge case with range + float step + metadata:
(range 0 5 1.0) ;=> (0 1.0 2.0 3.0 4.0)
(with-meta (range 0 5 1.0) {}) ;=> (5)
I would expect with-meta to preserve the initial value of the range.weird
user=> (def x (range 0 5 1.0))
#'user/x
user=> x
(0 1.0 2.0 3.0 4.0)
user=> (with-meta x {})
(5 1.0 2.0 3.0 4.0)
ah, it looks like they have the args swapped
public Obj withMeta(IPersistentMap meta){
if(meta == _meta)
return this;
return new Range(meta, end, start, step, boundsCheck, _chunk, _chunkNext);
}
private Range(IPersistentMap meta, Object start, Object end, Object step, BoundsCheck boundsCheck, IChunk chunk, ISeq chunkNext){
it is called with
meta, end, start, step, boundsCheck, _chunk, _chunkNext
but the args of the constructor are
meta, start, end, step, boundsCheck, _chunk, _chunkNext
https://ask.clojure.org/index.php/12148/strange-behavior-with-range-and-with-meta
Hello everyone? Does anyone know a good way to programmatically escape regex chars in a string? I would like to escape them because I am constructing the pattern at runtime from different parts. I have a starting point but it's not working because \
are special characters in Clojure strings. See thread for my code.
I'm not entirely sure this is exactly what I need because this returns a quoted pattern but I need something to quote the \
character in my replacement.
However, if I escape it with \\
it turns into a single \
which is then interpreted as an escape for the $
(clojure.string/replace "." #"[.*+?^${}]" #(str "\\" %1))
third argument for replace could be a functionfwiw, I've found https://github.com/lambdaisland/regal very helpful for composing regexes, the alternate syntax makes it almost self-documenting for future reading too.
@U028BUU1P3R I took a look at regal but it doesn't have non-capturing groups and it looks like I need those unless want to write my own state machine.
It does, you just use :cat
instead of :capture
. If you want to check what it produces just call regal/regex
and compare the generated regex with a hand-rolled version.
I've been using it and I don't want to go back. It’s convenient to have refer literals in a string like format but it’s super powerful to be able to compose and manipulate regexes like you manipulate arrays.
Hey y’all - I recall reading recently that Excel added support for Scheme and Clojure, but I’m having trouble finding an article or announcement. Does that sound familiar to anyone, or am I making that up?
Ah that must be it, thank you! For some reason I was thinking that Microsoft itself had done this
This is the one I saw here and cross-posted to our company slack 🙂 https://code-magazine.com/Article/2207071/The-Excellent-Schemer
cool - thanks!
Hello Masters of Clojure. I have a monorepo structure (here simplified), where
/deps.edn - defines :deps {is.mad/server-web3 {:local/root "./server/web3"}}
/server/web3/deps.edn - defines :test alias with {:extra-paths ["test"]}
/server/web3/src/<more files>
/server/web3/test/<more files> - these I would like to be included in the build & run
I'm trying to run commands via clj
from the top level with
clj -A:shadow:test watch test-node
But the test
alias isn't taken into consideration. My understanding and assumptions must be wrong how this works, so maybe you could help me figure out the following -
How to define aliases (that have extra-paths
) in dependency (sub-project) folder deps.edn
? Or is there a better way to achieve it?
> I'm using CLJS, but reading tools.deps.alpha
code this doesn't seem to be ClojureScript specific, so I thought to post it here. My apologies if it belongs to #clojurescript the aliases will only be used from the deps.edn in the directory you are in (dependency aliases are not used)
https://ask.clojure.org/index.php/7843/allow-specifying-aliases-coordinates-that-point-projects is a request for something like this if you want to upvote it
Thanks... that seems to leave me with 2 options for now:
1. define aliases in the top-level deps.edn
for each sub-project so that each alias has extra-deps
and extra-paths
2. Have each sub-project include the test folder in paths
in its deps.edn
Am I missing something, is there a better way?
I think those are both options that would work
a simpler version of 1 would define a single alias with all the nested extra-deps and extra-paths (not sure how interrelated all of those are)
Hey all, I am trying to write a macro to the declares a function and have gotten something pretty simple to work. The issue is that i want to iterate through a map and apply a function to the key to generate the function name and apply a function to the value to generate the args list and part of the function body. Below is a simplified version of what I am trying to do:
(def sample-map {:foo/bar {:properties
{:a "a"
:b "b"}}
:biz/baz {:properties
{:c "c"
:d "d"}}})
(defn key->fn-name [k]
(-> k name symbol))
(defn make-arg-array [obj]
(let [properties (keys (:properties obj))]
(->> properties
(map csk/->kebab-case-symbol)
vec)))
(defn create-function
[k v]
(let [fn-name (key->fn-name k)
arglist (make-arg-array v)]
`(defn ~fn-name
[{:keys ~arglist :as ~'resource}]
~'resource)))
(defmacro build-fuctions [map]
(let [fn-list (map (fn [[k v]] (create-function k v)) map)]
`(do
~@fn-list)))
most things work with manual calls such as
(create-function :biz/baz {:properties
{:c "c"
:d "d"}})
;; => (clojure.core/defn baz [{:as resource, :keys [c d]}] resource)
and
(map (fn [[k v]]
(create-function k v)) sample-map)
;; => ((clojure.core/defn bar [{:as resource, :keys [a b]}] resource) (clojure.core/defn baz [{:as resource, :keys [c d]}] resource))
but when i run (build-fuctions sample-map)
I get
1. Unhandled java.lang.IllegalArgumentException
Don't know how to create ISeq from: clojure.lang.Symbol
RT.java: 557 clojure.lang.RT/seqFrom
RT.java: 537 clojure.lang.RT/seq
LazySeq.java: 60 clojure.lang.LazySeq/seq
Cons.java: 39 clojure.lang.Cons/next
RT.java: 713 clojure.lang.RT/next
Compiler.java: 7181 clojure.lang.Compiler/eval
Compiler.java: 7149 clojure.lang.Compiler/eval
core.clj: 3215 clojure.core/eval
core.clj: 3211 clojure.core/eval
interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn/fn
AFn.java: 152 clojure.lang.AFn/applyToHelper
AFn.java: 144 clojure.lang.AFn/applyTo
core.clj: 667 clojure.core/apply
core.clj: 1990 clojure.core/with-bindings*
core.clj: 1990 clojure.core/with-bindings*
RestFn.java: 425 clojure.lang.RestFn/invoke
interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn
main.clj: 437 clojure.main/repl/read-eval-print/fn
main.clj: 437 clojure.main/repl/read-eval-print
main.clj: 458 clojure.main/repl/fn
main.clj: 458 clojure.main/repl
main.clj: 368 clojure.main/repl
RestFn.java: 1523 clojure.lang.RestFn/invoke
interruptible_eval.clj: 84 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 56 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 152 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
AFn.java: 22 clojure.lang.AFn/run
session.clj: 218 nrepl.middleware.session/session-exec/main-loop/fn
session.clj: 217 nrepl.middleware.session/session-exec/main-loop
AFn.java: 22 clojure.lang.AFn/run
Thread.java: 833 java.lang.Thread/run
can anyone help me out?once you fix that it still won't work because you are trying to use the value of evaluating a symbol in a macro which runs before evaluation happens
So does that mean I should do map outside of the macro and pass it in? It isn't quite clear to me how to address this.
Both parts tbh. I'm pretty new to macros. I dont really understand how I am shadowing map when I called it outside of the syntax quote.
you have a function/macro with an an argument named map
and then in the body you have (map ... map)
where both those map
names refer to the argument named map
that is passed in, and neither refers to clojure.core/map
and that is because you are doing something like (m x)
where m
is a macro, so m
is getting passed the symbol x
not the value the symbol x
evaluates to, and you are trying to use the value x
evaluates to in m
hmm so I tried moving the syntax quote up to the let. But it keeps throwing
(defmacro build-fuctions [data]
`(let [fn-list (map
(fn [[k v]]
(create-function k v)) data)]
(do
~@fn-list)))
Unable to resolve symbol: fn-list in this context
yeah, you are trying to generate code at compile time that depends on information available at runtime (which is after compile time)
for a limited class of things, you can actually do this, because clojure's notion of compile time vs, runtime is fine grained, and the runtime information of previous code is part of the compile time information for the next code
so the data I plan to iterate through will come from http. So could I make that call inside the body of the macro to make it available at compile-time?
you can do it, it is a fun party trick, but doing http requests everytime you load your code (and what do you do if those http requests fail, how does that effect your code loading?) is going to drive you nuts
One thought, if you absolutely must have this information in vars rather than just data at runtime, make a script that hits the endpoint and stuffs it into some edn files. Build vars from the edn map?
I have assumed something for a very long time, and now wonder when being asked if I’ve quite been making things up (I certainly could be!). My thought was given something like this:
(def foo
[reqs]
(let [xfm (comp set (partial map :resource-object-id) :response))]
(apply xfm (map do-post reqs))))
that when the defn is evaluated (when the file is loaded), xfm
will be identified as a constant function value and will not be recreated each time the function foo is called. To restate comp
will not rerun every time foo
is called. Instead an anonymous function in memory will exist as a value that xfm
is set to. Does any one know enough about the compiler to confirm this or set me straight?It will be evaluated each time. It's easy enough to check:
user=> (defn f [] (comp map set))
#'user/f
user=> (f)
#object[clojure.core$comp$fn__5876 0x69b2f8e5 "clojure.core$comp$fn__5876@69b2f8e5"]
user=> (f)
#object[clojure.core$comp$fn__5876 0x1a411233 "clojure.core$comp$fn__5876@1a411233"]
user=> (= (f) (f))
false
(defn foo []
(let [a {:a 1}]
a))
(System/identityHashCode (foo))
^ an exception where it can re-use.The way you could do this would be to extract your xfm to a def or a local variable, e.g.:
(let [xfm (comp set (partial map :resource-object-id) :response))]
(defn foo [reqs]
(apply xfm (map do-post reqs))))