This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-13
Channels
- # announcements (2)
- # babashka (2)
- # beginners (112)
- # calva (29)
- # cider (33)
- # clj-kondo (41)
- # cljdoc (10)
- # cljs-dev (2)
- # clojure (72)
- # clojure-berlin (3)
- # clojure-europe (10)
- # clojure-italy (6)
- # clojure-nl (15)
- # clojure-spec (5)
- # clojure-uk (40)
- # clojurescript (1)
- # clr (6)
- # community-development (6)
- # core-async (21)
- # cursive (42)
- # datascript (12)
- # duct (6)
- # flambo (1)
- # fulcro (50)
- # jobs (1)
- # leiningen (3)
- # off-topic (16)
- # re-frame (6)
- # reagent (23)
- # reitit (7)
- # ring-swagger (14)
- # shadow-cljs (35)
- # tools-deps (39)
- # vim (12)
I've got this:
(def instance-type [InstanceClass/BURSTABLE3 InstanceSize/LARGE])
and I can call a method like this:
(InstanceType/of (first instance-type) (second instance-type))
but is there a way to just pass in instance-type
as if it were the collection of args to InstanceType/of
?or is this the closest I can get?
(let [[class size] instance-type]
(InstanceType/of class size))
If InstanceType/of were a Clojure function, you could use apply
, but apply
will not help if it is a Java method, not a Clojure function.
(let [a (into-array Integer/TYPE (range 10))
b (int-array 10)]
(.invokeWithArguments (.findStatic (java.lang.invoke.MethodHandles/lookup)
System
"arraycopy"
(java.lang.invoke.MethodType/methodType
Void/TYPE
[Object
Integer/TYPE
Object
Integer/TYPE
Integer/TYPE]))
[a
(int 0)
b
(int 0)
(int 10)])
(vec b))
Is there a way to pass an existing collection of keys to clojure.spec.alpha/keys
?
(def ks [:a/b])
(s/keys :req-un ks)
Syntax error macroexpanding s/keys
Don't know how to create ISeq from: clojure.lang.Symbol
@pferron Not easily in Spec1. It will be supported by Spec2.
See https://github.com/clojure/spec-alpha2/wiki/Differences-from-spec.alpha for more information.
What we do with Spec is to use the spec itself as the system of record for the keys -- so we define the spec, then we introspect it to get the list of keys and use that in our code.
Ah, ok 🙂. Shouldn't do that in general (unless it's something rude, offensive, etc.)
Hi. I am having troubles extending a type with a protocol defined in other namespace, namely StreamableResponseBody
from ring.core.protocols
. Firstly, how do I require the protocol properly in my namespace?
I am doing simply (require 'ring.core.protocols)
and (extend-type ... ring.core.protocols.StreamableResponseBody ...)
, but getting an error java.lang.IllegalArgumentException: interface ring.core.protocols.StreamableResponseBody is not a protocol
.
Ok, problem solved. I need to reference the protocol as ring.core.protocols/StreamableResponseBody
.
I just discovered you can define your tests inside the meta data of a function, are there any pitfalls to doing this as a kind of documentation?
clojure.test is doing similar inside of deftest macro. You can also use clojure.test/test-var
to execute your tests in the context of clojure.test
https://gist.github.com/DeLaGuardo/55ae4be5a932cf32b0f1e828e5109bb6
you can run it clj -Sdeps '{:deps {some {:git/url "
Hello, maybe someone here can help me out.
I started looking into luminus and I want a little service with 2 endpoints one for retrieving a value and one for updating it.
At this point I don't want to mess about with data bases.
I found mount
but my value is always coming back as 42
when calling GET
These are my routes:
["/number"
{
:get (constantly (ok {:number @state/number}))
:post (fn [{:keys [body-params]}]
(swap! state/number (fn [_] (:number body-params)))
(ok {:number @state/number}))
}]])
File with state:
(ns todo-service.state
(:require
[mount.core :as mount]))
(mount/defstate number
:start (atom 42))
I think I might be fundamentally misunderstanding something about mount.Yeah, it was the constantly Thank you
user>
(def foo (atom 1))
#'user/foo
user> (def f (constantly @foo))
#'user/f
user> (f)
1
user> (reset! foo 300)
300
user> (f)
1
I have a transducer (pipeline) that goes something like this (into {} xform a-thing-that-returns-results-as-a-list-of-containing-maps)
. Inside the xform
, I have this (as one of the steps) (map #(into {} (filter (comp some? val) %)))
- in order to remove nil values from the map. I'm just wondering, is there a different way of removing nil values from the map as part of a transducer pipeline?
Check out keep
(keep identity)
gives you a transducer that will strip out all nil values.
Anyone with experience with the Luminus framework can help with some issues?
What function should I use to get true
if any item in a collection is true for the given predicate and false
otherwise? some
gives me nil
instead of false.
(not (not-any? pred coll))
works but I feel like I'm not seeing the obvious core function on the cheat sheet that is probably there.
If I run the command "clear" on my terminal it works as normal. But when I run the command in the boot-clj script it does not work. Any way to clear the screen in boot-clj script?
Or is there way I can call a function and prevent any console output that it might produce?
I am guessing what is happening is that when I am running that command its not executing in the current process I am running the script from so I am not seeing anything but I am not sure
@mario.cordova.862 Right, it's clearing the shell it is running in -- which is not the same shell process that you are running boot in.
Hi! I just began using Clojure today.
I've been trying to write some functions, but I want to write some function specifications right in the code. Like what every function expects to receive, what it'll return, and some example uses.
For JS I've used JSDoc, which allows for something like this:
/**
* This is a function.
*
* @param {string} n - A string param
* @return {string} A good string
*
* @example
*
* foo('hello')
*/
function foo(n) { return n }
Is there a similar "comment syntax" for Clojure?
I am not aware of anything that has widespread use or is any kind of de facto standard there, although Javadoc syntax is used by a few projects, I believe.
Yeah, Clojure spec is the other thing I was about to mention. It gives executable specifications for such things, without any natural language other than what you put in comments, again with no special syntax in wide use.
Is there any special convention or meaning in the use of the &
character in identifiers, or is it just like other punctuation marks (`-`, !
, ?
, etc.)?
most commonly & is not part of an identifier(although it can be), but an indicator of varargs
Ah, right. So it's better to avoid it, to avoid confusion with varargs vars. Got it 👍
@davidomarfch depending on what you want, you might be happy enough with just docstrings
user=>
(defn foo
"Takes an s, returns an s"
[s]
s)
#'user/foo
user=> (doc foo)
-------------------------
user/foo
([s])
Takes an s, returns an s
nil
user=>
depending on the project I may even look for the rich comment block first so I can actually run the forms.
I think I'll do that. I checked what you sent and it doesn't look like a "readability-oriented" thing. It may even be used for development and testing.
I'll stick with docstrings for now.
I was originally worried that it isn't very readable, but I guess you eventually learn to grasp it with no trouble.
Thanks for answering, @hiredman, @gerred, @andy.fingerhut!
cljs.ajax .... i'm getting an atom from the server
a vector of maps
i expect reset!
would work on the cljs client
to set the atom.
but alas no go
A cljs client can modify the state of an atom in the client, but cannot directly modify the state of an atom on the server.
Not sure if that is what you are trying to do, and maybe too obvious a thing to say...
the server is sending data to the client
i'm trying to update the clientside version of the atom with the ajax response
Sure, but the server is either sending the value that is the contents of its atom to the client, or something else I don't understand.
The server can't send a thing to the client that the client can directly use to mutate the server's state via reset!. The client might have an atom local to it that it can reset!, but it cannot reset! the atom's server without sending an agreed-upon message to the server that causes the server to perform the reset!
i'm not trying to change the state of a serverside atom
i'm trying to change the state of the clientside atom via reset! on ajax response
client initiates an ajax POST and the server replies with the contents of an atom, like you suspected,
So is the server sending the vector of maps to the client?
And the client is then somehow storing that in a client-local atom?
what is ajax, is it a send or a receive? to me it's both, so the answer to your first question is yes. and yes to the 2nd question.
The client should be able to reset! its client-local atom all it wants, with no effect on the server's state.
Not directly that is. Someone could customize things so that reset! on the client side might have additional side effects, but I don't know if any client-side cljs libraries play those kinds of tricks.
i feel like i'm taking crazy pills
when you say "no go", what are you trying, and what do you observe afterwards that leads you to conclude "no go"?
i'm using rum which has a rum/react atom watcher that i expect to render the page once the atom is reset!
my console log print actually gives me the atom contents which is what i expect, but somewhere it is not being used in the clojurescript app. so i'm not really sure what's missing
Is it straightforward to modify the rum/react atom watcher code to log additional messages to show what it is doing?
because instead of rendering the divs with words from the atom, they are blank
hmm i dunno, that's a good question, i don't know what that looks like and have not come across it at all. probably somewhere in the rum source?
i expect it to 'react' to the atom's change
I haven't used rum, so do not know if there are easier ways to debug this.
right on. well i'll take a look again, i'm probably just missing something obvious
thanks for your help
Also of course, log messages before and after your reset! call, to ensure it is actually happening when you think it is.
And FYI, if the server is sending a vector of maps to the client, it is a little confusing for other developers when you say it is sending an atom to the client. An atom is a mutable container that can hold a value, not something one typically serializes and sends to a different process "over the wire".
If the server is storing that vector of maps inside an atom in its Clojure code, but deref's that atom and sends the contained value (which is a vector of maps), then that is notably different than trying to send an atom.
I started logging like you suggested and then did a lein clean, now it works 😄
^_^ thanks
Yeah, running code that does not match with what you are modifying in your editor is a reasonbly-common source of confusion.