This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-22
Channels
- # architecture (30)
- # beginners (56)
- # cider (16)
- # cljs-dev (12)
- # cljsrn (21)
- # clojure (169)
- # clojure-austin (1)
- # clojure-estonia (1)
- # clojure-italy (3)
- # clojure-russia (1)
- # clojure-spec (56)
- # clojure-uk (46)
- # clojurescript (53)
- # consulting (3)
- # core-async (3)
- # cursive (14)
- # data-science (16)
- # datascript (1)
- # datomic (26)
- # defnpodcast (11)
- # docs (3)
- # emacs (6)
- # fulcro (4)
- # graphql (24)
- # hoplon (8)
- # instaparse (4)
- # java (2)
- # jobs (1)
- # jobs-rus (1)
- # jobs_rus (1)
- # keechma (1)
- # luminus (2)
- # lumo (1)
- # mount (36)
- # off-topic (30)
- # om-next (5)
- # onyx (29)
- # precept (23)
- # re-frame (20)
- # reagent (2)
- # remote-jobs (9)
- # ring (2)
- # ring-swagger (3)
- # rum (3)
- # shadow-cljs (100)
- # spacemacs (17)
- # sql (10)
- # timbre (2)
- # unrepl (29)
- # yada (2)
is it possible in cljs to have a map with one of its values being the map itself? If yes, how?
Oh, I found it. (assoc! transient-coll key transient-coll)
@vincent.cantin I don't think that's possible... but it might work if you override the ILookup protocol on a record or something......
Ah, that's a good idea, that might be better for my use case. Thank you.
@vincent.cantin Just out of curiosity, what are you trying to do?
I try to have a graph of data with cycles.
For example, a graph of friendships. I wish to present the graph as a plain-ish map.
Hopefully this is a more general problem that is quick to answer but I’m using pprint
to print each item in a go-loop
. In the repl it works as expected, but when running as a program using clj -m app
it doesn’t error but no output is printed. It’s like in the program context it’s exiting before the go-loop finishes.
@jayzawrotny Yup, in the REPL evaluation is forced because it needs to print it. If you're not printing it, you'll run into the same problem as with lazy sequences.
@seancorfield Ohhhh is there an equivalent to dorun
for go-loops or channels?
Not really sure. I don't use core.async
much.
Aha! I learned what to do: Whatever your last channel is do (async/<!! ch)
that will block until the output which in the case of a go-loop will wait until each item in the input channel has been read.
I am trying to create a plugin for a Java application. The plugin must extend a plugin
class and implement a constructor with a parameter. This parameter is another class (Say PluginInfo
) that comes from the Java app. I have added the namespace which declares the clojure plugin to be aot
compiled. However when I try to run repl/clojure-app I get a null pointer exception stating that the PluginInfo
was null.
I am not sure what I am doing wrong here.
The source code for the clojure app can be found here -> https://gitlab.com/yudistrange/josm-clj-plugin
If it helps the Java app in question is JOSM and the way to create new plugins for it in Java is documented here -> https://josm.openstreetmap.de/wiki/DevelopersGuide/DevelopingPlugins
@udit why isn’t state defined?
you reference it but I don’t see it in the code anywhere
oh, never mind, now I get it
@udit if you run this -main, where does info
come from? who creates it? how?
Hey Everyone, I have the following function(analyze-data) which takes the data 'vd' in following form: (FAM FAM {:well A1, :name PC, :Ct 20.7274} {:well A1, :name PC, :Ct 20.727} {:well A10, :name , :Ct 0 } {:well A11, :name , :Ct 0}) 'I need to write the code in the following analyze-data function which can clean this data and remove the rows where :name has no data.' (defn analyze-data "Runs the data analysis on the passed validated data" [vd] (println "In analyze-data function ", vd) (try (fx/run! (let [tree-items (build-tree vd)] (fx/at! view (to-id :tableTTV) {:root tree-items})) ) (catch Throwable t (display-message "ERROR" "ERR008: ERROR ") (prn-level :error "ERR008: ERROR" (.toString t) (.getMessage t))(.printStackTrace t) ) )) Any idea/solution? 🙂
@rachna.rajput99 Sounds like a basic filter
to me: (filter (comp seq :name) vd)
-- (comp seq :name)
gets the :name
element and returns truthy if it is not empty (missing, nil, or empty string).
Wow. its working. but as you said early: in clojure data structures are immutable. so how can I store this modified data to new data structure say 'vd1', and pass it to the try block.
By using let
(let [cleaned-vd (filter (comp seq :name) vd)
tree-items (build-tree cleaned-vd)]
,,,)
Hi everyone, Clojure noob here. I am wondering how to idiomatically implement a function that can handle the following scenarios:
my-fn :: a -> [a]
my-fn-for-vector :: [a] -> [a]
If I try to create a function that is overloaded, where each overloaded function has the same arity, that doesn't work.
I am thinking along the lines:
(defn my-fn
"Function that handles integers and vector of integers"
[x]
(cond (vector? x) x
:else [x]))
Not sure if it is idiomatic.FWIW: Stuart Sierra recommends against it here https://stuartsierra.com/2015/06/10/clojure-donts-heisenparameter
@U0J9LVB6G Thanks... this was exactly what I was looking for!
@ahmadnazir I'd probably just test vector?
inside the function... but it depends on what you're really trying to do.
What's the best way to run both a API server and a server with a --main function? right now I'm set up with a working composure project, but I also want a main function to run
You could use a multi-method or a protocol or a conditional test or...
@josh_tackett Not sure what you mean... what would -main
do if not start up the Ring server?
@seancorfield Exactly I want to start a main with a Ring server, but right now my ring server just starts an API
I'm reading here: https://github.com/ring-clojure/ring/wiki/Setup-for-production but not sure what lein command to run to run the server so the --main executes
@seancorfield Thanks for the response. I am just wondering if it is idiomatic? I want one function that can process a vector as well as a single entity.. because it helps readibility of the code (since there are less functions). Are there any disadvantages?
@seancorfield NVM figured it out
@ahmadnazir I would say it's not really idiomatic and you should instead require a vector for the argument in all calls. But if the intent is just to treat a non-collection argument as a vector of one element, you could do this
(defn my-fn [a]
(let [a (if (vector? a) a (vector a))]
,,,))
(like I say: not idiomatic but if you really must ... 🙂 )
@josh_tackett FWIW, I never bothered with the whole lein ring server
thing to start web apps -- I just ran -main
and had it start the server explicitly.
I think the main advantage there is if you need an uberwar - it streamlines things for that
@seancorfield What if @ahmadnazir created a general as-vec
function? That would keep the defn from being too muddy with argument guards wouldn’t it?
(defn as-vec
"Takes an argument and returns a vector or itself if already vector"
[x]
(if (vector? x) (vec x) x))
@jayzawrotny doesn’t vec already do that?
Oh, I’ll confirm
penguin.babysit-test=> (source vec)
(defn vec
"Creates a new vector containing the contents of coll. Java arrays
will be aliased and should not be modified."
{:added "1.0"
:static true}
([coll]
(if (vector? coll)
(if (instance? clojure.lang.IObj coll)
(with-meta coll nil)
(clojure.lang.LazilyPersistentVector/create coll))
(clojure.lang.LazilyPersistentVector/create coll))))
nil
user=> (vec 2)
RuntimeException Unable to convert: class java.lang.Long to Object[] clojure.lang.Util.runtimeException (Util.java:221)
but your code fails the same way
Right
if you want vector, use vector not vec
Good catch
also, wanting to do that tends to be a symptom of bad design
Agreed, plus I read the https://clojurians.slack.com/archives/C053AK3F9/p1516651087000547 article but was curious if a function like that made the pattern any more tolerable.
;; anti-pattern
;; but hypothetical solution
(defn as-vec
"Takes an argument and returns a vector or itself if already vector"
[x]
(if (vector? x) x (vector x)))
but in thinking about it I see the problem: It’s less the extra code required & more about the ambiguity it leaves when calling it in the mental or even in an explicit type-system
FWIW, clojure.java.jdbc
allows this sort of ambiguity -- but mostly because the parameter handling was broken in a long-ago version and I decided not to break code that relied on the bug. For example, these are both valid: (jdbc/query db "SELECT * FROM table")
and (jdbc/query db ["SELECT * FROM table"])
-- the second form is the "correct" way to do it (and if you have SQL params, you must do it this way!) but the first form is a useful shorthand. Applies all across the java.jdbc
API.
(and you can use a PreparedStatement
object in place of the SQL string -- in both cases)