This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-10-28
Channels
- # announcements (2)
- # babashka (16)
- # bangalore-clj (1)
- # beginners (93)
- # boot (11)
- # calva (5)
- # cider (13)
- # clj-kondo (49)
- # cljdoc (14)
- # cljs-dev (1)
- # clojure (99)
- # clojure-dev (3)
- # clojure-europe (1)
- # clojure-india (1)
- # clojure-italy (9)
- # clojure-nl (3)
- # clojure-poland (1)
- # clojure-russia (1)
- # clojure-spec (31)
- # clojure-uk (21)
- # clojured (2)
- # clojurescript (18)
- # core-async (12)
- # cursive (36)
- # data-science (1)
- # datomic (54)
- # duct (3)
- # emacs (33)
- # events (1)
- # fulcro (17)
- # jobs (1)
- # joker (8)
- # keechma (1)
- # leiningen (7)
- # malli (8)
- # nrepl (19)
- # pathom (6)
- # planck (18)
- # re-frame (20)
- # reagent (18)
- # shadow-cljs (3)
- # sql (7)
- # vim (31)
Hi, compojure destructuring is my topic, and my question is ... what's the best delimiter to use between vars (for GET requests) ?
I end up having a URL likez
pardon that Z...
http://site.com/S1&L2&P3 -> section 1 lesson 2 part 3... but I'm wondering, is there a better way?
I think I figured it out with /x=foo&z=bar kinda deal
Anybody got any questions or puzzles or problems that need addressing 😄?
hi is it possible to re-export a variable/function from a module? i.e. if you want a wrap or alias a library globally
You can do that with potemkin: https://github.com/ztellman/potemkin
I am pretty sure that the potemkin library is Clojure/JVM only, not Clojurescript.
If you mean something like "I have a library with 3 namespaces, and I want the namespace foo to have a public function x that is identical to function y in namespace foo.a.b.c", then one way would be to write this expression in namespace foo: (def x foo.a.b.c/y)
.
namespace foo would also need to require foo.a.b.c
yes, that's what I ended up doing for regular declarations but that approach unfortunately isn't working with macros
@andy.fingerhut is there a way to rexpose/redeclare macro in a new newspace? i even tried wrapping the macro but doesn't seem to exihbit same behavior
If the macro expands to use symbols that were in the original namespace, it may well fail if you try to export it via your namespace. You might have to re-export the symbols that it expands to.
After using Potemkin and similar techniques in the past, I've sworn off that approach -- it has a number of weird edge cases and it's usually a bad idea, in my opinion.
@seancorfield yeah, coming from js land, I developed a habit of wrapping a library before exposing it in my application. doesn't seem like that's recommend in Clojure? do you just manually write all dependencies in all files?
this is an example of what im trying right now, figured if i wrapped it just as I would a function it wouldn't be a problem
(ns ui
(:require [hx.react :as hx]))
(defmacro defnc [display-name props-bindings & body]
(hx/defnc display-name props-bindings body))
It's not really idiomatic in Clojure to wrap things like that.
Some libraries do it internally, just to publish an API from functions based in multiple namespaces, but mostly folks expect to require
all the namespaces for functions they need to use.
I'm not sure why folks would do that sort of thing in JS -- what supposed benefits does it have?
In Clojure, we prefer to be explicit about dependencies rather than try to hide them...
not so much about hiding, but trying to be control of what you expose to your application i.e. if you want to upgrade a package but there's an API change, or you want to add an enhancement to a function or macro and have it take effect everywhere in your app though in general, I feel that it's useful to also trim down unnecessary dependency declarations - i.e. right now im using a package for react that has components and hooks declared separately so that's two require statements whereas with regular react it would have been one
Hi, I'm trying to use a java class method from clojure, but for some reason it doesn't identify the field, here are the details:
I've already succeded calling a previous method doing
(.connect master)
for the variable master
I've looked up the instance's methods using
(vec (.getMethods (.getClass master)))
and found the method I wanted to call - sendRequest:
#object[java.lang.reflect.Method 0x279701e8 "public java.util.concurrent.CompletableFuture com.digitalpetri.modbus.master.ModbusTcpMaster.sendRequest(com.digitalpetri.modbus.requests.ModbusRequest,int)"]
trying to call it with
(.sendRequest master)
resulted in an error:
1. Unhandled java.lang.IllegalArgumentException
No matching field found: sendRequest for class
com.digitalpetri.modbus.master.ModbusTcpMaster
Reflector.java: 397 clojure.lang.Reflector/getInstanceField
Reflector.java: 440 clojure.lang.Reflector/invokeNoArgInstanceMember
REPL: 119 crystal.core/eval18742
REPL: 119 crystal.core/eval18742
Compiler.java: 7176 clojure.lang.Compiler/eval
Compiler.java: 7131 clojure.lang.Compiler/eval
core.clj: 3214 clojure.core/eval
core.clj: 3210 clojure.core/eval
main.clj: 414 clojure.main/repl/read-eval-print/fn
main.clj: 414 clojure.main/repl/read-eval-print
main.clj: 435 clojure.main/repl/fn
main.clj: 435 clojure.main/repl
main.clj: 345 clojure.main/repl
RestFn.java: 137 clojure.lang.RestFn/applyTo
core.clj: 665 clojure.core/apply
core.clj: 660 clojure.core/apply
regrow.clj: 18 refactor-nrepl.ns.slam.hound.regrow/wrap-clojure-repl/fn
RestFn.java: 1523 clojure.lang.RestFn/invoke
interruptible_eval.clj: 79 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 55 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 142 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
AFn.java: 22 clojure.lang.AFn/run
session.clj: 171 nrepl.middleware.session/session-exec/main-loop/fn
session.clj: 170 nrepl.middleware.session/session-exec/main-loop
AFn.java: 22 clojure.lang.AFn/run
Thread.java: 748 java.lang.Thread/run
I'll be grateful for any insight on this :-]This com.digitalpetri.modbus.master.ModbusTcpMaster.sendRequest(com.digitalpetri.modbus.requests.ModbusRequest,int)
says it expects an int
argument so (.sendRequest master 42)
(or whatever) would be the correct call.
Thank you! the problem was with the arguments, needing a request and an int, but I didn't format the call correctly when I first tried, so incorrectly thought the problem was with accessing the method
Looking for code snippet, which can return the clojure.lang.PersistentTreeMap by removing the key from it. I have an object "myTreeMap" (of type clojure.lang.PersistentTreeMap) this contains 2 keys (1 and 10). I want to remove one of the key( assume value also will be removed) say 10 and after removal "myTreeMap" should be with only one key. please suggest
are you looking for dissoc
? https://clojuredocs.org/clojure.core/dissoc
This is not changing the state of the clojure.lang.PersistentTreeMap object.. can you please provide code snippet by using swap! ?
PersistentTreeMap is immutable.. so we need to write a code which can swap the exisitng myTreeMap which weill be after removing the key 10.
Clojure maps are immutable, you can’t mutate the them. they always return a new instance
if you want something similar to mutation you need to use an atom:
user=> (def a (atom {:a 1 :b 2}))
#'user/a
user=> @a
{:a 1 :b 2}
user=> (swap! a dissoc :a)
{:b 2}
user=> @a
{:b 2}
thank you! If I want to verify whether :a exists or not, how to do that? tried with (contains? a :a), it gives error as "IllegalArgumentException contains? not supported on type: clojure.lang.Atom "
How to make chrome extension using clojurescript? can anyone give me some guidelines about that?
I think there is a shadow-cljs build mode for chrome extensions-
I haven’t tried it though
Hello, I have a basic question regarding namespace aliases, I'm wondering why this code (x.Element) doesn't resolve:
$ clj
Clojure 1.10.1
user=> (require '[clojure.data.xml :as x])
nil
user=> x.Element
Syntax error (ClassNotFoundException) compiling at (REPL:0:0).
x.Element
user=> clojure.data.xml.Element
clojure.data.xml.Element
Namespace aliases do not work for class names.
I am pretty sure Element
is a class name?
You can use full-path class names with no import
statements at all, if you wish. import
enables you to refer to a class by shorter names, yes.
deftype and defrecord also generate factory functions, which are a good habit to get into using because a not uncommon error is importing the class without requiring the namespace
i see - I think the missing piece for me was the difference of referring to java classes vs vars in a namespace
wow didnt knew about this feature to create a starter project:
clj -Sdeps '{:deps
{seancorfield/clj-new {:mvn/version "0.7.7"}}}' \
-m clj-new.create \
\
hello/core
@papachan clj-new
knows how to run Leiningen and Boot templates as well (but those will generate whatever is in the template so usually project.clj
and build.boot
respectively).
Or are you just expressing surprise over clj-new
's ability to run a template from GitHub? (it also knows about :local/root
since it uses tools.deps.alpha
like clj
/`clojure does).
Yes. Didnt know how to generate a quick template from clj command line. And your clj-new is very useful!
Hi, I’ve ran into a bit unexpected behaviour with
reify
macro. I have builder function that during initialisation (just before reify) can throw an exception. For some reason, that exeption is being caught and only shows up when one of the methods of the reified object is called. Is it done intentionlly and I’m missing something?Ah crap, thanks
mapv is strict, if that helps
Yes, thank you
Having subpatterns
inside of the let
is useless in either case (map or mapv)
@andy.fingerhut why? Wouldn’t I be calculating it on every call then?
not sure if you put it in the body just before the call to reify in hopes of causing it be evaluated fully, but either you use lazy map, and having subpatterns
there still does not cause it to evaluate fully, or you use mapv
, and then having subpatterns
in the body doesn't help
That was my intention
to have it calculated once
but I got a bit rusty with Clojure and forgot about lazyness 🙂
The expression (->> s subpatterns-strings ...)
is evaluated on every call to the function new-pattern
as you have written it
It is not re-evaluated on every call to the recognize
method of the object returned by reify
ok, seems like I should check my understanding of laziness
Have you programmed in Haskell before?
just regular stuff like Javascript and Golang
If not, no worries. Just wanted to know your background in order to know what things might help explain Clojure laziness.
In Clojure, many functions return a lazy sequence, including map
filter
and a dozen or two others.
Unless you force them to be evaluated, only as many elements as you actually use are evaluated, when they are needed. (sometimes a few more elements are evaluated ahead of that)
ok, so function supplied to map would be called the first time the elem is actualy used?
and the that value would be re-used in later usages?
yes. And sometimes the function will be called for a few elements after the latest one you have ever accessed, but even then the function will be called at most once per input element of the sequence given to map
ok, got it. you were extremely helpful, thanks
If you want to force any of the functions that return lazy sequences to immediately do all of their processing, which requires the time and the memory to store all of the results, you may wrap the call inside of (doall ...)
in my case mapv would also do, right?
I mention doall
because it works for any expression that returns a lazy sequence, including a call to map
filter
etc.
@andy.fingerhut got it, thanks
you are welcome