This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-18
Channels
- # announcements (26)
- # beginners (107)
- # calva (26)
- # cider (55)
- # clj-kondo (7)
- # clojure (115)
- # clojure-europe (6)
- # clojure-houston (3)
- # clojure-italy (4)
- # clojure-nl (16)
- # clojure-norway (1)
- # clojure-uk (42)
- # clojuredesign-podcast (3)
- # clojurescript (47)
- # clojutre (4)
- # cursive (7)
- # datomic (75)
- # fulcro (1)
- # graalvm (3)
- # graphql (16)
- # jobs (1)
- # jobs-discuss (13)
- # keechma (1)
- # leiningen (19)
- # luminus (5)
- # off-topic (33)
- # pathom (16)
- # re-frame (76)
- # reitit (4)
- # ring (5)
- # shadow-cljs (86)
- # spacemacs (52)
- # tools-deps (43)
- # vim (7)
- # yada (1)
This seems like a bug since (s/replace "1-1" "-" "\\-")
works fine.
I think I'm going a little crazy but (.toString ^CharSequence "\\")
throws an exception for me, whereas
(let [a "\\"]
(.toString ^CharSequence a))
is fine?!Why do you need the type hint in the first place? Passing string is just fine. If you have a little function wrapping this then you would add type hint on the argument. Otherwise you need type hint in the let block - type hints can't be applied everywhere.
This is code copied from clojure.string/replace
(sort of), I'm aware that it could be done other ways but that doesn't mean it's less of a bug
I was trying to work out why Nazral's example:
(s/replace "1-1" #"-" "\\-")
outputs 1-1
but
(s/replace "1-1" "-" "\\-")
doesn't
There are different cases in the implementation and explained in the doc string for s/replace, depending on whether the second arg is a string or a regex. They do not work the same.
If second arg is a string or a character, then the 3rd arg is a literal replacement string with no special interpretation, except the usual one for all Clojure strings in double quotes, where a single backslash is an escape for the next character.
If second arg is a regex, then the 3rd arg is not just a literal replacement, but can contain things like $1 or $5 that are replaced by the corresponding parenthesized groups in the regex match.
It should all be explained in the doc string for clojure.string/replace, but ask if it doesn't make sense after reading it.
👍 My bad, RTFM seems to apply! That said I'm still confused why (.toString ^CharSequence "\\")
wouldn't compile, is that by choice or just isn't ever needed so shouldn't matter
I am not certain, but the type of "\\" is java.lang.String, not CharSequence, so it seems a little odd to try to type hint "\\" as something it isn't.
(.toString ^String "\\")
fails as well fwiw
Do you get an error message of any kind?
Syntax error reading source at (REPL:1:24).
Metadata can only be applied to IMetas
Syntax error reading source at (REPL:1:25).
Unmatched delimiter: )
I see one "Metadata can only be applied to IMetas". In Clojure, metadata can be applied to collections and symbols, but very few, if any, other things. Strings are not on the list of things that metadata can be applied to.
Also on the list of things that metadata cannot be applied to: literal expressions of type string, number, char, boolean, keyword
If you have a name bound in a let or loop, or a parameter of a function, that can have metadata annotating it.
ahh of course, so in the let
example it's a symbol so can have meta attached, looks like I need to spend some more time reading
Thanks for your help 🙂
could someone help me understand what's the use of :state under gen-class ? i have the following class:
(ns test.core
(:gen-class
:name Testing
:extends javax.swing.JFrame
:init init
:constructors {[String] [String]})
(:import (javax.swing JFrame)))
(defn -init [x]
[[x] (ref {})])
i can create a JFrame object and set its title without problems:
test.core> (def f (Testing. "a"))
#'test.core/f
test.core> (.getTitle f)
"a"
test.core> (.setTitle f "b")
nil
test.core> (.getTitle f)
"b"
when/why should i bother with :state ?Is there any public examples of the use of the com.cognitect.aws/athena lib to run a query against athena?
So if I when I use deps, do aliases apply to transitive dependency also? I am trying to make a library that has an optional additional dependency that enables more configuration formats.
My guess is no
no, not transitive
Yeah... speaking of managed dependencies... in java being able to inherit maven configuration from Spring base and have library versions default to specific values in all projects was a game changer and it's a killer feature in larger organizations
Hey guys, I have a really naive question: How do I compose a hash like {:a <any function> :f <any function> ...} I have tried something like (merge-with (fn [a f] {a f }) [:a f] (reaptedly *)) But at the end I just have some similar to {:a 1 :f 1} How do I merge a index with a function as value?
not sure I understand what you want (what's your input and desired output?), but zipmap
comes to mind, is it what you want?
(zipmap [:a :b] (repeat *))
=> {:a * :b *}
that is exactly what I need, cheers ...
one of the most satisfying things about clojure is returning to the source code of a project i worked on 4 years ago and can still completely comprehend wth is going on.
This is extremely satisfying, and something I have the pleasure to experience with Clojure, but not my ClojureScript projects. Frontend is horrible.
A ton of irreducible complexity that is not related to the business problem, it’s just there by virtue of the solution sitting in an extremely hostile environment.
any one know a function like merge-with but that accept 3 or more args at on? like : (merge-with-xxx (fn [a b c] (......)) {:a 1} {:a 2} {:a 3})? or something similar as merge-with-key (merge-with-key (f val-in-result val-in-latter) maps) both kind of function suits me.. cheers...
You could just do something like
(defn merge-with-xxx [f & maps]
(reduce (partial merge-with f) maps))
(merge-with-xxx into {:a [1]} {:b [2]} {:a [3]})
;;{:a [1 3], :b [2]}
it still require 2 args in function .... I`d like the same behavior of merge-with but instead of accumulate in pairs, it should call a function with all values at once (3 values of 3 different maps with the same key at once ) ...
so the merge function arity should always be exactly equal to the (variable) number of args passed after the function?
exactly ...
instead of 2, it should be and arity ..
ignore keys
{:a 1} {:a 2 :g 6} {:a 3} , will call once (f [a b c]) with 1,2,3 and ignore any call with g value
doesn't seem like the best idea tbh 🙂 but maybe something like this:
(defn merge-with-xxx [f & maps]
(let [ks (into #{} (mapcat keys maps))]
(into {}
(for [k ks
:let [vs (map k maps)]
:when (every? some? vs)]
[k (apply f vs)]))))
(merge-with-xxx (fn [x y z]
[z x y])
{:a 1} {:a 2 :g 6} {:a 3})
;;{:a [3 1 2]}
yep, this looks to work, I will brush up a bit.... 🙂
thank you
people that use reloaded style workflows: how do you deal with your work-in-progress code which needs to use the main system
? I have a system which I can fully reset
but this means I cannot refer to the system
at the top level, which is something I usually want to do in a scratch namespace of of stuff I'm working on. Is it as simple as just have your work-in-progress stuff outside of the tools.namespace.repl refresh dirs? Or are there other techniques I'm missing?
in general, I don't think code should ever use the whole system, it should be passed the parts it needs, same for work in progress code
I’ll usually opt a scratch namespace out of tools namespace with disable-reload!
and pluck what bits of the system I need out of the running system map at https://github.com/weavejester/reloaded.repl/blob/master/src/reloaded/repl.clj (integrant.repl has a similar internal value)
No particular need disable-reload!
btw, or to have the WIP ns outside the "refresh-dirs"
You always can access the internal atom, right?
@jjttjj if you're using mount, it will only start/stop the parts of the application state in the namespaces you've recompiled in the REPL (it's not the same as a full refresh, but for ongoing work this is rarely necessary I find). I really like that feature and until actually using it had no idea how it improves your workflow.
Does anyone know how to dynamically set logging level with clojure.tools.logging, preferably when using log4j 2? I've tried something like this https://github.com/jumarko/clojure-experiments/blob/master/src/clojure_experiments/logging.clj#L39-L48 but it didn't work (even without slf4j dependencies on the classpath). I got to some "weird" state when it somehow worked but after restarting the REPL it's not reliably working anymore. The log level seems to be stuck at whatever level was set at startup from the config file
@jumar dynamic configuration will need to go through the underlying logging implementation, e.g., log4j.
@U5ZNLFCQ7 that's true but I do want to do this from code. Maybe even expose it as a "diagnostic feature" of an application.... Not sure I get this: > dynamic configuration will need to go through the underlying logging implementation, e.g., log4j. Isn't that what I'm doing or do you mean something else?
I mean that clojure.tools.logging
provides a consistent interface to write to some underlying logging implementation (e.g., log4j, slf4j). It does not provide an interface to configure that underlying logging implementation; for that you'll need to code directly against whatever specific logging implementation you are using.
Yeah, that's what I've done, I believe, with the code I shared. But it doesn't work properly for some reason
it seems like function metadata in clojure is stored on the var, not the function object
(defn- takes-args? [f]
(->> f meta :arglists (some (comp pos? count)) boolean))
(defn foo [x])
(takes-args? #'foo)
;; => true
(takes-args? (fn [x]))
;; => false
Is there a way to find out whether a function takes arguments from the function itself?usually if you're asking that question, it's a good sign you have gone down the wrong path. what is your actual goal?
oh just saw this. I'm emulating a REPL using load-string
that uses the last return value as the final value. If it's a thunk, I want to invoke it, otherwise it shall not be invoked since args would be missing.
here's the relevant code: https://github.com/den1k/zeal/blob/master/src/zeal/eval/core.clj
@denik You can use introspection to get a list of arities. (I don't remember how vararg is handled.)
using it for my project zeal https://github.com/den1k/zeal
I agree with @alexmiller. That said... https://gist.github.com/ataggart/7f7826507ff99985a7565dd20bba9617
doesn't this miss arities above the limit where you can only use applyTo?
I think there's a limit like that...
(cmd)user=> (.invoke + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
210
(cmd)user=> (.invoke + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
Execution error (ClassCastException) at java.lang.Class/cast (Class.java:3369).
Cannot cast java.lang.Long to [Ljava.lang.Object;
(cmd)user=> (.applyTo + '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21))
231
oh - I'm surprised that invoke with 20 args works when the function arities only specify 0, 1, 2, and N args
I was concerned about the mapping from invoke signatures to arity support, but clearly it's more complex than I thought
Isn’t 20 one example of N?
just as a general handwavey thing, if you're trying to do something in Clojure and it's hard, you should take that as a sign
(cmd)user=> (.invoke + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
210
(cmd)user=> (.invoke + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
Execution error (ClassCastException) at java.lang.Class/cast (Class.java:3369).
Cannot cast java.lang.Long to [Ljava.lang.Object;
(cmd)user=> (.applyTo + '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21))
231
Why does the core.cache README examples use a fifo cache factory with a regular clojure map? That seems misleading given regular clojure maps do not have order guarantees, and a fifo cache is typically only used on data that has order.
first in first out is relative to the insertion order in the cache, it isn't via some ordering of elements of the cache
Sure, but I'm talking about the specific library here. From core.cache fifo docstring: > If the associative structure can guarantee ordering, then the said ordering will define the eventual eviction order. Otherwise, there are no guarantees for the eventual eviction ordering.
If I'm following you correctly, that's exactly my point. Calling seq on a regular Clojure map does not guarantee order.
correct, and as the doc string suggests, if that doesn't work for you, you can seed the cache with some other associative structure that does have some order
there are a number of different clojure map implementations that preserve some kind of order, and I expect it also works on java.util.Maps, of which many preserve order as well
Right - I get that but it seems like the example could be improved by using a structure that does preserve order.
but you may also say "hey, this is the seed of the cache, everything as an arbitrary insertion of now, so just do whatever with them"
because everything you start with in the seed is tied for first, and it just picks a way to break the tie
i'm confused. if you seed with an empty map, add 5 things, when it needs to evict it won't evict the first of the 5 things added after the seed but whichever reports as first when calling seq
on the map
https://github.com/clojure/core.cache/blob/master/src/main/clojure/clojure/core/cache.clj#L173 keeps a queue of keys for eviction
Oh I see. So basically if your cache seed does not guarantee order, eviction order is not guaranteed for the seed values. All values after that get evicted in a fifo fashion.
it's like, imagine there is a clock for the cache, and every operation on the cache advances the clock one tick, so the clock starts and time 0, the first operation becomes 1, etc, etc
because it is an immutable cache, and you can only add one item at a time, no two items will be added on the same clock tick, and at eviction time you always evict the item added at the lowest clock value
but, at the very beginning, the base items are all added at the same tick, so the clock value is the same for all of them
so it is still fifo, it is just fifo, but some times things are added at the exact same time so "first" is an arbitrary member of that set of tied items