This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-23
Channels
- # aws-lambda (1)
- # bangalore-clj (3)
- # beginners (80)
- # boot (8)
- # clojars (1)
- # clojure (200)
- # clojure-dev (37)
- # clojure-greece (26)
- # clojure-italy (11)
- # clojure-norway (3)
- # clojure-russia (14)
- # clojure-spec (21)
- # clojure-uk (30)
- # clojurescript (50)
- # core-logic (10)
- # core-matrix (1)
- # cursive (15)
- # data-science (21)
- # datomic (45)
- # devcards (2)
- # emacs (4)
- # fulcro (12)
- # garden (2)
- # jobs (5)
- # juxt (1)
- # lambdaisland (1)
- # leiningen (4)
- # luminus (20)
- # lumo (26)
- # off-topic (33)
- # onyx (27)
- # parinfer (1)
- # pedestal (3)
- # perun (5)
- # re-frame (20)
- # reagent (27)
- # ring (1)
- # ring-swagger (21)
- # shadow-cljs (259)
- # spacemacs (14)
- # yada (3)
any ideas on why I get errors creating a ThreadPoolExecutor?
(def t-pool (new ThreadPoolExecutor (int 8) (int 2) (long 60) TimeUnit/SECONDS (new LinkedBlockingQueue 100)))
I just get 3. Unhandled clojure.lang.Compiler$CompilerException
Error compiling form-init7780702103892101766.clj at (36:13)
2. Caused by java.lang.reflect.InvocationTargetException
(No message)
1. Caused by java.lang.IllegalArgumentException
(No message)
`https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html
@beoliver The second argument is maximumPoolSize
so it must be at least as large as the corePoolSize
(the first argument).
I suspect you wanted (int 2) (int 8)
instead?
Hello #clojure. I'm curious for your opinion on something. What thing(s) in Clojure would you choose to show someone without prior exposure to the language to demonstrate as much upfront practical value as possible?
Supposing your audience are experienced programmers familiar mostly with traditional imperative and object-oriented programming.
Aye, the REPL can be very compelling -- especially if you show a live, running process and connect a REPL into it and explore and modify it while it's running.
Depending on the background of your programming audience @mingp I think a demonstration of how easy it is to manipulate collections of data with map/filter/reduce. reducible-query
in clojure.java.jdbc
with a transducer pipeline, for example. Depending on the database and the configuration, you can stream very large record sets (that won't fit into memory) and still process them with transducers.
FWIW: What seemed to bowl over some devs I once knew was a single block of routes from compojure. The brevity got them interested.
agree with showing something very practical. A lot of the really cool ideas in Clojure will likely be foreign at first and it might be hard to see the immediate value i.e. they don’t know what they’re missing. But everyone loves the “here’s how much boilerplate it takes to do the same thing in your lang” comparisons 🙂
Kind of a nice use case. Brevity. But also request/response as maps, thus highlighting the "it's all just data" idea.
For the working programmer, I usually demo the way clojure.java.jdbc allows integration of the Postgres geospatial and json queries and then add operators for json/geoquery using honeysql. I show "here's how you register serialization of these types to postgres, deserialization from postgres to my types, and this is how easy it is to query it with my custom operators. Put that together with compojure-api and I have a nice rest api console, validation for my endpoints, and arbitrarily complex SQL generated with very straight-forward functions (including demoing how easy transforms are when you have functions working with data). You can put it together on a couple pages and it's a real example that can be extended out in a useful way immediately.
I've mentioned this elsewhere, but I haven't found any language/ecosystem that is even close to as good at letting me clean data, generate complex SQL, and rapidly prototype well-documented apis. A lot of languages can let you build routes quickly, some even with beautiful documentation (django rest framework comes to mind with python). Almost every other solution gets insanely complex the minute I want to use database-specific features that invariably lead to string bashing.
A good exercise is to show the routes (say) and ask, 'Tell me what this code does' and see if they can get close.
The problem with a lot of Java frameworks (or even Django/Rails) is that it's super hard to figure things out without visiting dozens of files.
In other words, it's not so much language nerd stuff, but all the get-it-done-fast so you can leave early stuff. And being able to figure out what others have done without also having to take anger management classes.
Hi, All. I'm fiddling with macros and and stumble with some behaviour which i think is strange.
(defmacro m1 [] (let [x (java.util.ArrayList.)] `~x))
(m1) ;; []
(type (m1)) ;; java.util.ArrayList
(defmacro m2 [] (let [x (org.joda.time.DateTime.)] `~x))
(m2) ;; #object[org.joda.time.DateTime 0x4011f484 "2017-10-23T13:05:15.179+07:00"]
(type (m2)) ;; CompilerException java.lang.RuntimeException: Can't embed object in code, maybe print-dup not defined: ...
Is it something related with reader
take place in there?@keymone actually i'm working with larger macro to provide some inlining, above is minimalist example in that i guess some reader
take place with the above macro expansion
It seems macro expanded values is serialized and then later need to be read again by reader
yes, and org.joda.time.DateTime is missing the kind of serialization that makes reader understand it’s reading a literal for that class
so, if it is something with reader, i wonder how []
can be brought back to java.util.ArrayList
@reedho the problem is that you are embedding java objects in code, which is kind of a bad idea
Can anyone point me in the direction of a good example of best practice for implementing migrations on top of DynamoDB?
Regarding my question on macro above, actually the macro i'm trying to build is some thing like this:
(defmacro mk-route-check
[routes]
(let [dynamic-clauses
(->> routes
(mapcat
(fn [{route :route
dows :dows}]
[[
route
(if (= "DLY" dows)
'_
`(~'_ :guard
~(->>
(clojure.string/replace-first dows #"^D" "")
(map (comp #(Long/parseLong %) str))
(into #{}))))
]
true
]
)))]
`(fn [route# dows#]
(clojure.core.match/match
[route# dows#]
~@dynamic-clauses
:else false))))
This is okay, inlining the routes
(mk-route-check
[{:route "CGK-DPS" :dows "DLY"}
{:route "CKG-MDC" :dows "D123"}])
But i can not get it work if i want to load routes dynamically from somewhere, e.g.
(let [routes [{:route "CGK-DPS" :dows "DLY"}
{:route "CKG-MDC" :dows "D123"}]]
(mk-route-check routes))
I means, it can be made to work if i store the routes to root binding, and create macro based on that var
@carr0t some thing like:
(def __route-check nil)
(alter-var-root #'__route-check
(constantly (mk-route-check
[{:route "CGK-DPS" :dows "DLY"}
{:route "CKG-MDC" :dows "D123"}])))
Macros run at compile time, so if you want to do something based on run-time variables your macro has to output forms which will do the right thing at run time
At which point the question may well be - why can't you just write a function instead of a macro?
And that external data isn't available at runtime, it's only on the compile host? Can't it be in resources or something?
I want to achieve to e.g. provide simple function that could be used to reload the resource
implementation below doesn't work:
(defn reload-route-check [resource-file]
(let [routes
(-> resource-file
(java.io.PushbackReader.)
clojure.edn/read)]
(alter-var-root #'__route-check
(constantly (mk-route-check routes)))))
Is it bad form to query a db inside a map function if I need extra data for each item in collection? What are the alternatives?
@yonatanel yes, one alternative is to change the query with JOIN clause if possible
@paulbutcher Have you had a look at migratus? https://github.com/yogthos/migratus
I have, yes. And Ragtime and Joplin. I'm quite sure that all of these could work, but all of the examples are very SQL-focussed. I was hoping for some concrete experience/recommendations/best practice around DynamoDB 🙂
@reedho What if my api is against some service that only answers one by one? Which iteration fn is appropriate when each item requires out-of-process data and might fail?
@yonatanel for that case, i will prefer to use mapv
a little bit of macro help? trying to construct a function that has the form (f xs (g (f xs (g (f xs ...)))))
I have the following
(defmacro m1 [pool n]
(if (= n 1)
`(ft ~pool "did not block")
`(ft ~pool (ft-deref (m1 ~pool (dec ~n))))))
for 1
its ok, but for n>1
I can't get the recursion to workfor writing complicated macros, I like to first write FUNCTIONS that do "list of symbols" -> "list of symbols" ... and then afterwards, have the macro call the function
also, this particular macro looks like it can be written as a function of arguments f, g, xs
(defmacro m1 [pool n]
(if (= n 1)
`(ft ~pool "did not block")
`(ft ~pool (ft-deref (m1 ~pool ~(dec n))))))
@beoliver last line ^
Curious to know why you'd want a macro for that though
@val_waeselynck I am playing with thread pools of (including ones of fixed sized)... if there are n threads and n+1 calls to deref then it will block. If I wrote it like this
(defn block-test [n pool]
(loop [n n
expr (ft pool "did not block")]
(if (zero? n)
expr
(recur (dec n) (ft pool (ft-deref expr))))))
then each future is put on the thread pool straight away- so it does not block. I needed to construct an expression so that the calls weren't evaluated (perhaps I could just write the function the other way round?)@val_waeselynck yeah I didn't need a macro
@beoliver good to know 🙂
Hi. Ihave this: [:a :b :c]
. How do i convert it into this: [[:a] [:b] [:c]]
? Thank you!
actually I didn’t. is there a way to pull static final
constants from a Java class into a Clojure namespace?
@bronsa that's a bit confusing -- there's not a way, or (def foo Klass/foo)
is the way?
you can’t pull static field directly in :import
, you have to import class and then use (def foo Klass/foo)
@eggsyntax there's no way, but you can easily alias a static class field as a var using the above
I have a question about sorted sequences - We have a large sequence/vector/set (multiple thousands of entries). We need it to be ordered, and we need to add to and remove from it. When we add we know the value of the thing we'd be sorting on (obviously), but when we remove we do not
Our initial naive version of a vector is hugely slow when we reconstruct it because we have to sort everything
A sorted set looked like it would work, but that converts to a regular set when we remove things from it
> When we add we know the value of the thing we'd be sorting on (obviously), but when we remove we do not can you explain this more?
When we remove, we want to remove all things with id XXX (matching the thing we're about to insert) regardless of the timestamp they have
Or actually, we're sorting on timestamp and ID, because it is possible for 2 items with different IDs to have the same timestamp
one hashed on ID, the other a binary tree by timestamp with collections of things at timestamp?
there's clojure.data.avl or something similar that would keep it balanced for you, not have to reinvint the wheel
“… it should not yet be used for storing data durably over time…” says on the transit-format readme
@U0GC1C09L why use jsonb in this case? Once you have transit-encoded it, it seems to me the JSONB features of Postgres become useless (unless you rely on impl details of Transit), you might as well use a fressian-encoded BLOB
ah, good and valid point. thanks for being the voice of reason, @val_waeselynck
How do I create a map, in which the order is retained like in java.util.LinkedHashMap
? I need vals
to return the values in the order in which they were assoc'ed to the map...
you could add a timestamp to the values and use a sorted map? https://clojuredocs.org/clojure.core/sorted-map-by
@carr0t Do you need fast nth
support on the sorted set? Or you just want to iterate it sorted by your values?
Well, we want to be able to periodically (like, every second) pull from it all events with timestamp < 'now', and push them onto our job queue
Then this should fulfil your requirements: https://github.com/clojure/data.priority-map
A question about ref
s. Assuming that I have a data structure that I wish to share between multiple threads, but I want to both read and update in one single atomic action, do either of the following two examples capture this? I am unsure of the scope of dosync
. As far as I am aware this (and refs in general) are used for atomic transactions using more than one state:
(def mah-data (ref [1 2 3 4 5]))
(defn mah-update-1 [data-ref]
(dosync
(when-let [a-value-that-no-other-thread-can-have (-> data-ref deref peek)]
(alter data-ref pop)
(println "value is" a-value-that-no-other-thread-can-have))))
(defn mah-update-1 [data-ref]
(dosync
(when-let [a-value-that-no-other-thread-can-have (-> data-ref deref peek)]
(alter data-ref #(if (empty? %)
identity
(pop %)))
(println "value is" a-value-that-no-other-thread-can-have))))
Intuitively I feel that I need to be requiring the lock before the deref, but I am not sure if this is the case using dosync
If you only have one piece of state @beoliver you should be able to use atom
and avoid dosync
etc.
ref
is useful when you have two separate stateful things that you want to update together.
(and, thus, ref
is rarely needed or used)
@beoliver You can use a "trick" like this
(let [state (atom [1 2 3])
x (meta (swap! state
(fn [x]
(let [removed (peek x)]
(with-meta (pop x) {:rem removed})))))]
[x @state])
@rauh is there any info on he trick? Is this the only way? If so, I am guessing that this must be an operation that is not considered idiomatic 😄
@beoliver You don't have to, you can also avoid meta
and just keep your "last removed item" in the state, but then whenever you want to use state you have to first get rid of the "last removed item"
(let [state (atom [nil [1 2 3]])
[lr] (swap! state (fn [[_ x]] [(peek x) (pop x)]))]
[lr (second @state)])
would
(defn mah-update-3 [data-atom]
(let [p (promise)]
(swap! data-atom (fn [x] (deliver p (peek x)) (pop x)))
(println "value is" @p)))
be considered a side effect?If swap!
decides to re-run your fn, then your promise will still have the first (wrong) value
@ghadi thanks for your help. I am calling require to include a library within a function used by proxy
keep in mind require
executes at runtime, not compile time... Stuff like this cannot work:
(let [...]
(require 'foo.bar)
(foo.bar/something...))
(proxy [VisionWindowListener] [] (windowOpened [win] (require '[clojuretest.windows.main-window :as script])
try to find a smaller error. Does running (require '[clojuretest.windows.main-window :as script])
work in your REPL?
I am wondering if it is because VisionWindowListener is using a different classloader than the library that I am trying to load
(proxy [VisionWindowListener] [] (windowOpened [win] (require '[clojuretest.windows.main-window :as script])
(require '[clojuretest.windows.main-window :as script]) (proxy [VisionWindowListener] [] (windowOpened [win]
You only need to call require
once at the top of your namespace, unless you are dynamically loading code
I understand. I am dynamically loading code, which is why I am calling it within a function that is within proxy
That is true, first I am trying to get a non-dynamic version working before I make it dynamic
Does functions defined in proxy use the class loader of the interface that is being extended or is the classloader used the same one that clojure uses?
Because the interface I am implementing with proxy uses a different classloader than my clojure code
So I am wondering if that is the issue --- wondering if my proxy uses a different classloader than my regular code
Is it legal to use require inside proxy function definitions? because everywhere else I use require, it works
java.io.FileNotFoundException: Could not locate clojuretest/windows/main_window__init.class or clojuretest/windows/main_window.clj on classpath. Please check that namespaces with dashes use underscores in the Clojure file name. at clojure.lang.RT.load(RT.java:456) at clojure.lang.RT.load(RT.java:419) at clojure.core$load$fn__5677.invoke(core.clj:5893) at clojure.core$load.invokeStatic(core.clj:5892) at clojure.core$load.doInvoke(core.clj:5876) at clojure.lang.RestFn.invoke(RestFn.java:408) at clojure.core$load_one.invokeStatic(core.clj:5697) at clojure.core$load_one.invoke(core.clj:5692) at clojure.core$load_lib$fn__5626.invoke(core.clj:5737) at clojure.core$load_lib.invokeStatic(core.clj:5736) at clojure.core$load_lib.doInvoke(core.clj:5717) at clojure.lang.RestFn.applyTo(RestFn.java:142) at clojure.core$apply.invokeStatic(core.clj:648) at clojure.core$load_libs.invokeStatic(core.clj:5774) at clojure.core$load_libs.doInvoke(core.clj:5758) at clojure.lang.RestFn.applyTo(RestFn.java:137) at clojure.core$apply.invokeStatic(core.clj:648) at clojure.core$require.invokeStatic(core.clj:5796) at clojure.core$require.doInvoke(core.clj:5796) at clojure.lang.RestFn.invoke(RestFn.java:408) at clojuretest.core$create_window_listener$fn__13.invoke(core.clj:22) at clojuretest.core.proxy$java.lang.Object$VisionWindowListener$9dcff2dc.windowOpened(Unknown Source) at com.inductiveautomation.factorypmi.application.runtime.ClientContextImpl$1.windowOpened(ClientContextImpl.java:166) at com.inductiveautomation.factorypmi.application.FPMIApp.fireWindowEvent(FPMIApp.java:1197) at com.inductiveautomation.factorypmi.application.FPMIApp$InternalFrameEventRelay.internalFrameOpened(FPMIApp.java:1319) at javax.swing.JInternalFrame.fireInternalFrameEvent(Unknown Source) at javax.swing.JInternalFrame.show(Unknown Source) at java.awt.Component.show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at javax.swing.JComponent.setVisible(Unknown Source) at com.inductiveautomation.factorypmi.application.FPMIApp$RuntimeWindowOpener.openWindow(FPMIApp.java:1774) at com.inductiveautomation.factorypmi.application.FPMIApp.openWindow(FPMIApp.java:1014) at com.inductiveautomation.factorypmi.application.script.builtin.SecurityUtilities._tempReOpen(SecurityUtilities.java:230) at com.inductiveautomation.factorypmi.application.runtime.UpdateProjectPane.uninstall(UpdateProjectPane.java:103) at com.inductiveautomation.factorypmi.application.runtime.UpdateProjectPane$1.stepsFinished(UpdateProjectPane.java:91) at com.inductiveautomation.ignition.client.launch.AbstractStepRunner$1.run(AbstractStepRunner.java:40) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
if you want to eliminate proxy from suspicion, you can move the body of the proxy method to an ordinary defn
and try it there
because the compiler errors if it can’t find foo.bar - you need a layer of indirection (eg. resolve
) around the lookup of something
to make that work
if it’s required in your ns form
or, if you already ran the code once so it already loaded it earlier…
a small example of require works fine in reify
... I'm wondering if something else is at play
(defn bar [] (reify clojure.lang.IReduceInit (reduce [_ f init] (require 'foo))))
;;; foo.clj
(ns foo)
(println "hey")
In that code example you just gave does require use clojures classloader or does it use the interfaces classloader?
clojure's classloader -- sorry I can't work up a larger classloader example at the moment
@ghadi yes, but if clojure's class loader delegates to its parent class loader and if the parent class loader has the class then the parent class loader will then be used to load the classes in the class, and not clojure's class loader. I'm thinking that might be the problem. I am doing an experiment to find out.
I know we have https://github.com/clojure/test.check in clojure, which is great. Does the reverse exists? For example: I was trying to isolate an error in a biggish generated EDN/JSON structure. I have a unit test that fails with said structure with a given error. Would there be a way to remove parts of the structure to make is smaller to read, while keeping the same error? It seems it is a general debugging process I follow quite often (remove parts until it is small enough to understand it).
@nha I haven't seen such a thing. I suppose it'd be theoretically possible to reuse test.check's shrink-loop if you wrote the code for doing the shrinking. but that's most of the work anyhow.
@ghadi The code that created the proxy object runs in a different thread than the code that calls the methods on the proxy. In the thread that calls the proxy methods the context classloader was not set to the classloader that has clojure. So simply setting the context classloader to clojure on the thread that calls the proxy methods fixed the problem. Thanks for looking into this with me.
@nha https://github.com/pjstadig/humane-test-output might be what you're looking for, but I don't have enough information about your particular situation to say for sure? If you have that loaded and try to compare to maps, then it will produce a diff if they don't equal each other.