This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-09
Channels
- # beginners (22)
- # boot (80)
- # cider (6)
- # cljs-dev (5)
- # clojure (190)
- # clojure-berlin (5)
- # clojure-dev (24)
- # clojure-italy (14)
- # clojure-russia (70)
- # clojure-spec (39)
- # clojure-uk (82)
- # clojurescript (121)
- # clojurewest (1)
- # core-logic (2)
- # cursive (25)
- # datascript (186)
- # datomic (33)
- # dirac (266)
- # emacs (9)
- # gsoc (4)
- # hoplon (37)
- # immutant (34)
- # instaparse (22)
- # jobs (4)
- # juxt (6)
- # lambdaisland (2)
- # leiningen (1)
- # liberator (1)
- # luminus (5)
- # lumo (28)
- # off-topic (9)
- # om (23)
- # onyx (26)
- # other-lisps (1)
- # parinfer (39)
- # pedestal (45)
- # proton (1)
- # protorepl (10)
- # re-frame (18)
- # reagent (4)
- # ring-swagger (8)
- # rum (4)
- # specter (13)
- # test-check (14)
- # testing (1)
- # unrepl (164)
- # untangled (10)
- # yada (14)
is there core a function for this? (some #(if (pred %) %) coll)
basically some but returns the element instead of the predicate result
true, that works. thought there might be a single core function but it’s easy enough to write a helper
i have some data with a compound key, eg a 2-tuple (:a, :b)
, and I need to be able to search it for a list of all matches on the first part of the key
I can brute force this, eg a normal map with a normal vector as the key, and then search over the whole map.
But is there a more efficient way? Or a way to make multiple indexes?
I'm contemplating juxt
for that... or index
...
@josh.freckleton is the existing data used extensively in your system in other places? is the data changed often, or is it mostly being read? and, if the data is a regular clojure map already, is it being stored as an atom, or?
it's used through a limited boundary, I have cusomt getters/setters it's mostly being read yes, an atom
(map in atom)
two similar approaches would be:
1) have a 'proxy' data structure that is of the form {:a {:b 1 :c 2} :x {:y 42}}
for your data {[:a :b] 1 [:a :c] 2 [:x :y] 42}
, and then have simple but custom functions that assoc/dissoc/get/update/etc, so that you would do (assoc-compound m [:a :b] 1)
and then (get-compound m [:a :b])
, but which behind the scenes uses the other structure. This would only be feasible if the use and access of your data were limited to a relatively contained portion of your code.
2) maintain your regular map with the actual compound key, but use the indirection map as above as a separate entity. If your regular map were an atom, it would be easy to add a watch to it, and the watch function would silently update the indirection map automatically when the main data changed. Then, when you wanted to search, search the indirection map.
hm, I liked your first idea, but then swap!
ing into it takes ~30s, (it's a big map)...
yep, i'll copy the code over, it looks like upgrading to sorted-map
has slowed things down a bit
i've sorted maps before, but they were very small and it was only for "display" purposes -- i'm curious why you need to sort such a large map?
it doesn't take much longer to load the map, and I (naively?) assumed that a sorted map would read quicker
ah wait, it's fine
cider was trying to print the whole map after swap
ing
i forced the fn that swaps to return nil
@joshjones thanks for your help, this is running perfectly now!
oh, and for the record, does sorted-map not run faster?
i imagined it was a clojure way of saying things were indexed, but now that I think of it, map is a hashmap already, right?
your map is already a hash map, yes -- if it's a sorted map, changes should take longer, since it has to sort on those changes
if i were doing this, i'd at least explore adding a watch to the data atom, and you can then run clojure.data/diff
on the old and new values, and update your indirection map based on what was added, removed, and changed. it may prove too slow to diff such a large map, but i'd benchmark it and see if the performance penalty is high. It would be a great way to make things transparent
(add-watch data :unused
(fn [_ _ prev curr]
(let [[removed added _] (clojure.data/diff prev curr)]
(println "removed: " removed
"\nadded: " added))))
interesting, i'll need to check out watches since I haven't used em yet. My perf seems ok right now though...
I may be able to get by with just this, but i'll definitely keep watches in my back pocket
this will update the indirection map seamlessly, but of course a performance review on diffing large data is warranted
(add-watch data :unused
(fn [_ _ prev curr]
(let [[removed added _] (d/diff prev curr)]
(run! (fn [[[a b] _]]
(swap! data' #(update-in % [a] dissoc b))) removed)
(run! (fn [[path v]]
(swap! data' assoc-in path v)) added)
(println "removed: " removed
"\nadded: " added))))
yeah, it seems to work fine, as long as of course you are using [a b]
format for your keys -- anyway, have fun with it!
I wonder if a book that explains Rich's philosophy and data-oriented programming is needed. Or at least a series of medium articles
@joshjones thanks for the guidance! clojure does indeed rock 🙂
Hello everyone, Clojure beginner here... I have a hugsql question but since nobody is online there right now, may I ask it here, too?
(not sure whether you mean no one is in #beginners or in #hugsql -- I suspect the latter... and I admit I didn't even know there was such a channel! -- and there's also a #sql channel for general SQL-related questions)
@bcbradley like in a macro?
according to https://clojure.org/reference/java_interop clojure integrates type hints for non-class types via the conventions at http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.2-200
@seancorfield Somebody has found me in #hugsql, but thanks for your help 🙂
@bcbradley IIRC you just want ^objects since a string array really isn't a thing on the JGM
i'd like to automagically have type hints in the library i'm generating, but i need a way to programatically generate type hints
if i can't do that, my tool has to change; it would have to output a bunch of files that happen to be clojure code, rather than clojure forms
That really is the better option, imo
outputting .clj files is easier to debug, for what it's worth
alright was just wondering if anyone has done something like this before, doesn't hurt to ask 🙂
you get real line numbers in errors
there is a way to do it, but I forget the incantation
oooh i think i found what i was looking for https://clojure.org/reference/metadata
When people say EDN is a subset of Clojure, what do they mean? I.e. what Clojure syntax does not conform with EDN?
of course
ah but #(+ % 1)
doesn't
so that's one example
btw, were you the one looking for a decent apple-like keyboard sometime back? if so, what did you get?
the only thing I know about edn is that "read clj data" <-- can be dangerous due to things evaled at read time, and edn resolves that issue
@qqq, I got this one: https://www.amazon.com/Apple-MC184LL-B-Wireless-Keyboard/dp/B005DLDO4U pretty much
so not the newest incarnation, which is more expensive
happy with the choice
so I think the difference between .clj and .edn is only that dispatch macros like #()
(what else?) are not supported by .edn
also regex: #"asdf"
and #'
though #_
and #{}
work
https://github.com/clojure/tools.reader/blob/master/src/main/clojure/clojure/tools/reader.clj#L769-L803 is what clojure supports
https://github.com/clojure/tools.reader/blob/master/src/main/clojure/clojure/tools/reader/edn.clj#L313-L337 is the subset that EDN supports
@bronsa thanks
so basically you could write all clojure in EDN syntax, but you'd lose some convenient shortcuts like @
for deref
or macro characters
hah you're right
I'm guessing a regex literal is also compiled only once
but it's pretty close
philosophically it's mildly disappointing that reader literals don't all just desugar to forms
@bronsa, are regex literals the only exception?
hi, i’m getting an error that <! is outside of go block for this code: (go (do-something (repeatedly #(<! channel))))
You’re not technically calling <! inside the go block as it is wrapped in an anonymous function
since you’re using <! inside an anomymous function, nothing guarantees you that that function will be called inside a go block, it might well be not 😛
which is also why macros like for
don't work inside go blocks, as they wrap the body in a fn
or maybe even write a variant for repeatedly that takes a channel instead of a function 🙂
silly question but having trouble using the clj-fuzzy lib [12:03] Could not locate clj_fuzzy/metrics__init.class or clj_fuzzy/metrics.clj on classpath [12:03] I added to project file and namespace [12:03] project file: ;; Fuzzy String Matching [clj-fuzzy "0.3.3" :exclusions [org.clojure/clojure]] [12:04] namespace [clj-fuzzy.metrics :as fuzzy]
ideas?
using this lib ^^^
lein clean
lein deps
and lein run
@rauh any ideas?
@josh_tackett Is it in (System/getProperty "java.class.path")
?
nah its not in the path @rauh
it shouldn’t need to be
I did what I typically do for all other deps
@josh_tackett Posting the exact same question here, just one minute after posting it in #beginners is a bit impatient...
@seancorfield Time is money
Hello everyone, I’m trying to list markdown files in a directory, under resources/blog
, just a flat list of .md
files. When developing, I have it working using something similar to (-> “blog” io/resource io/file .list)
. Unfortunately when I package using lein uberjar
and try to run it, I get an java.lang.IllegalArgumentException
saying Not a file: jar:file:[…].jar!/blog
. I inspected the compiled .jar file, and it contains the blog directory. Any ideas?
So I have to keep them outside the jar?
(or keep track of them somewhere else)
the fact that it sort of works sometimes is unfortunate, but turning a resource (arbitrary bytes loaded rom a classloader) in to a file is not something you can generally do
I understand
yeah, so if you want to treat them like files, you'll need to keep them on the filesystem, if you want to treat them like resources you'll need to some up with some kind of registry or something to enumerate them
I’ll just keep them outside, should work better anyway, at least for a blog ^^
thank you
@josh_tackett Post your full project.clj
hi everyone, I'm trying to wrap my head around clojure.test
, but im quite confused.
is there a way to run a fixture (to insert and delete stuff inside a database) on ONE SINGLE test (declared with deftest
) instead of all tests in the namespace?
@plins if you only want it on a single test (or a few random ones, but not all), then call the functions you want directly from the test definition. Fixtures are a helper for when you want to save some extra code by not having to write the same function calls in every test.
@rauh :dependencies [[org.clojure/clojure "1.6.0"] [clj-stacktrace "0.2.8"] [clj-time "0.11.0"] ;; JSON [cheshire "5.6.3"] ;; SQL [org.clojure/java.jdbc "0.6.1"] [org.postgresql/postgresql "9.4.1209"] ;; HTTP [clj-http "1.1.2"] ;; Encoding [base64-clj "0.1.1"] ;; Redis [com.taoensso/carmine "2.15.1"] ;; Fuzzy String Matching [clj-fuzzy "0.3.3" :exclusions [org.clojure/clojure]] [environ "1.0.1"] [dire "0.5.3"] [manifold "0.1.0"] [overtone/at-at "1.2.0"]]
@plins test-vars (called at the repl takes a collection of vars) will run the required fixtures
lein also can run a single test doing something like lein test your.test.namespace/test-name
is there are any significant time difference when running clojure and pure java app?
i'm planning to make live wallpapper for android
will clojure be slower than java?
if all your app does is call out to other libraries, they are going to be the same. you can write clojure code in such a away as to generate pretty much the same bytecode as javac would, so performance will be the same, and modern jits are very good. But idiomatic clojure is likely to be slower, allocate more than the idiomatic java will. But java isn't automatically fast either, so I dunno
okay, got it
thank you 🙂
Hi there! Could you suggest me, please, how to split regex into multiple line, something like that:
(def uri-regex
"RFC URI regex "
#"^(([^:/?#]+):)?" ; scheme method
#"(//(?<host>[^/?#]*))?" ; authority host domain
#"(?<path>[^?#]*)" ; path location
#"(\?(?<queryparam>[^#]*))?" ; query
#"(#(.*))?") ; resource fragment
(re-pattern (str #"(https?)://" #"(.*)")) ;; ==> #"(https?)://(.*)"
nice, thank you again, @tanzoniteblack !
there’s a clojure lib that does something like this I think
looks nice, thanks. But I made it even simpler:
(def uri-regex
"RFC URI regex "
#"^(([^:/?#]+):)? # scheme method
(//(?<host>[^/?#]*))? # authority host domain
(?<path>[^?#]*) # path location
(\?(?<queryparam>[^#]*))? # query
(#(.*))? # resource fragment
")
so actually I have a prepared statement and I want to execute it on the same connection as the one created by korma with defdb
I have an UPDATE
I need to run that uses .setBinaryStream
so I followed the first example here: https://www.postgresql.org/docs/7.3/static/jdbc-binary-data.html
as far as I know, a preparedstatement can only be executed on the connection that created it
so if you created a preparedstatement, the only way to execute it is with the connection you created it from
@schmee glad to hear it — I’m actually hoping to move to yesql. Meaning no offense to Korma, of course, it just doesn’t suit our needs
It doesn't suit anyone's needs
It's a library that takes a fairly popular approach from other languages, writing a DSL around data, but that approach doesn't really work well in any language
Even back when I used to do C# we ended up going a route closer to yesql, DSL sql wrappers just don't work.
And attempting to recreate its structure in your language of choice generally doesn't give you benefits compared to crafting queries by hand.
What I usually see changing are the structures around SQL - parameterizing queries, extracting results, etc. Because JDBC sucks to use. 😐
I’ve used jOOQ (in Java) for the last couple of years, and that works great for the most part. It’s great to be able to compose your query if you need it. Though I also built a yesql-like tool to deal with the times that the queries got too unwieldy, and in Clojure, yesql definitively seems the way to go.
Don't forget about hugsql - I've not used it much but it's pretty similar to yesql as far as the general approach and is the library that Luminus uses now.
I’m guessing you don’t compose queries in Yesql, just use variants?
any pros/ cons when it comes to hugsql vs yesql @shaun-mahood ?
@chillenious: Good article at https://yogthos.net/posts/2016-02-22-LuminusEmbracingHugSQL.html - about a year old - I haven't used either substantially though, I mainly use JDBC with occasional honeysql
cheers!