clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
ccann 2020-10-14T01:05:49.435300Z

does anyone know how to interpolate a vector of ids into jdbc/update! e.g. I want my WHERE clause to be where id in ? and then pass in a vector

dominicm 2020-10-14T11:51:35.452900Z

You might be able to create a connection, and create parameters for that. I had a lot of trouble with this though.

šŸ‘ 1
2020-10-14T01:08:56.435600Z

You cannot

šŸ‘ 1
2020-10-14T01:10:23.437800Z

The underlaying java jdbc drivers expect arrays (not vectors)

2020-10-14T01:10:51.438500Z

So at the very least, you need to pass it an array not a vector

2020-10-14T01:12:20.440700Z

clojure.java.jdbc also uses setObject which if I recall doesn't work right with arrays even if you pass one in

2020-10-14T01:12:50.441400Z

I believe that is overridable though

seancorfield 2020-10-14T01:29:16.442400Z

@ccann HoneySQL takes care of that for you (by building the appropriate where id in (?,?,?,?,..,?) clause given your vector of IDs.

ccann 2020-10-14T01:29:47.443300Z

@seancorfield Iā€™m working on a legacy project where we only have jdbc pulled in and trying to hotfix something

ccann 2020-10-14T01:29:57.443700Z

but definitely would prefer to use honeysql

seancorfield 2020-10-14T01:30:05.443900Z

If you are using PostgreSQL and next.jdbc you might be able to do this via the array syntax -- see the PostgreSQL Tips and Tricks in the next.jdbc docs.

šŸ’Æ 2
seancorfield 2020-10-14T01:30:35.444400Z

But I think with clojure.java.jdbc you're out of luck in that case.

Darin Douglass 2020-10-14T02:15:26.446600Z

if your vector gets too large, you'll quickly run into parameter count limits (for PG it's something like Short/MAX_VALUE). but you can do something like below to get around that:

(j/update! db-conn :my-table ["id = ANY(?)" (into-array TheProperClass my-ids)])

seancorfield 2020-10-14T02:43:37.447100Z

@ddouglass Does that work with clojure.java.jdbc? I know it works with next.jdbc

seancorfield 2020-10-14T02:44:20.447600Z

(that syntax is what is recommended in the next.jdbc Tips & Tricks for PostgreSQL)

Darin Douglass 2020-10-14T11:24:16.450300Z

@seancorfield it does. Weā€™re currently using clojure.java.jdbc and that was a sanitized chunk of code from one of our apps

Ivan Fedorov 2020-10-14T11:28:59.452500Z

Wishing a good day to fellow Clojurians šŸŒ± Does anyone know a recurrence implementation in Clojure? (as defined by https://tools.ietf.org/html/rfc5545#section-3.3.10) Or maybe in Java[Script]. Thinking about a plain rule parsing / compiling for starters. They look like RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z Iā€™ve found http://github.com/jakubroztocil/rrule, but I hope it could be done both easier and simpler, in like 200 lines of thoughtful Clojure code. UPD: Iā€™ve been advised to use spec or malli to build a parser. Canā€™t say yet if thatā€™s a good idea or not.

2020-10-14T13:42:37.456400Z

Does anyone here recall a recent call for an upcoming virtual Clojure conference? Maybe within the past 30 days. Seems like they were asking for ~20 minute talks. I can't remember if I saw it here, on twitter, reddit, ... too many platforms.

alexmiller 2020-10-14T13:43:54.456800Z

https://reclojure.org

šŸ‘ 2
2020-10-14T13:45:10.457400Z

Thanks! I tried euroclojure, clojuretre, clojureD,....

schmee 2020-10-14T16:09:31.459Z

is anyone aware of a DSL/macro library for Clojure bit operations?

schmee 2020-10-14T16:09:43.459300Z

idk about you but I find (unchecked-byte (bit-and (bit-shift-right inv 8) 16rff) way harder to read than the Java equivalent

2020-10-14T16:10:26.459700Z

seems like you could get pretty far with a few well chosen defs

schmee 2020-10-14T16:10:59.460800Z

yeah, Iā€™ll probably whip something up but curious to see if anyone else has dealt with this before šŸ™‚

2020-10-14T16:11:09.461100Z

|, >>, << are all valid function names

2020-10-14T16:11:29.461400Z

also why use 16rff instead of 0xff?

schmee 2020-10-14T16:13:39.462200Z

I thought that was not a thing in Clojure?

schmee 2020-10-14T16:14:04.462800Z

ahh, itā€™s 0bxxxx thatā€™s not a thing, I misremembered, thanks!

2020-10-14T16:14:12.463100Z

user=> (= 255 0xff 16rff 2r11111111)
true

šŸ‘ 1
2020-10-14T16:15:51.463600Z

forgot to include 0377 above :D

schmee 2020-10-14T16:17:30.464200Z

octal, everyoneā€™s favorite šŸ˜

2020-10-14T16:19:12.464400Z

@schmee an idea:

(ins)user=> (def |b unchecked-byte)
#'user/|b
(ins)user=> (def |& bit-and)
#'user/|&
(ins)user=> (def |>> bit-shift-right)
#'user/|>>
(ins)user=> (|b (|& (|>> 292928 8) 0xff))
120

schmee 2020-10-14T16:21:12.465200Z

neat!

2020-10-14T16:21:14.465400Z

actually it works without the | on every name, I didn't realize & was a valid function name but it is

schmee 2020-10-14T16:21:32.465900Z

I found https://github.com/rm-hull/infix, which I have seen before but it actually seems perfect for this

schmee 2020-10-14T16:21:58.466900Z

normally I wouldnā€™t consider using infix, but when I have to write pages of byte manipulation I might make an exception

2020-10-14T16:22:01.467Z

having done most of my bit fiddling in assembly, I find breaking the ops into one per line more helpful than infix :D

2020-10-14T16:22:17.467200Z

but ymmv clearly

schmee 2020-10-14T16:22:35.467500Z

appreciate the input! :thumbsup:

2020-10-14T16:23:22.468100Z

as a reader of code, I'd rather review a java file than infix in the middle of a clojure file

āœ”ļø 1
schmee 2020-10-14T16:27:31.468500Z

yeah Iā€™m not sold on it either, but Iā€™ll give it a try and see how it pans out

seancorfield 2020-10-14T17:00:20.468700Z

Nice. Thanks!

quoll 2020-10-14T17:22:20.469Z

Cuteā€¦ I thought that the special form would get upset with this, but it doesnā€™t

(def & bit-and)
(defn f [& b] (& (first b) (second b)))
=> (f 3 9)
1

2020-10-14T17:31:59.469200Z

right, I think this is because [& ...] is a special case in arg list parsing rather than a special form

2020-10-14T17:32:08.469400Z

since it's not used in call position

2020-10-14T17:32:47.469600Z

though (special-symbol? '&) does return true

2020-10-14T18:29:00.470500Z

any Clojure projects out there that actually want Hacktoberfest participation?

vlaaad 2020-10-14T19:45:46.474700Z

For all practical purposes the right data structure for my problem is nested vectors. Except one thing: it needs a small piece of data attached that I want to lookup by it's key. Is it okay to put that into metadata, or should I force everything to be a map with 2 keys (data and a vector with maps of same shape)?

phronmophobic 2020-10-14T19:48:03.475Z

it might help to see an example data value, but one is the data you are considering putting in meta data actually meta data?

vlaaad 2020-10-14T19:49:34.475200Z

example:

=> {:grid [[{:grid [[{:grid [[{}
                              {}]]
                      :cursor [311708 475413]}]
                    [{:grid [[{}]
                             [{:grid [[{}
                                       {}]]
                               :cursor [121699 68040]}
                              {:grid nil
                               :cursor [930988 26115]}]]
                      :cursor [136580 790398]}]]
             :cursor [162873964 1315]}
            {}]]
    :cursor [42984 203305]}

vlaaad 2020-10-14T19:50:55.475400Z

the data I'm considering putting is not a meta data, it's pretty much very important data

vlaaad 2020-10-14T19:52:59.475600Z

but this structure makes lookups and updates hard, since I have to switch maps and vecs all the time, instead of doing assoc-in/ get-in using only coordinates

phronmophobic 2020-10-14T20:00:58.475900Z

why not just make a function that looks like assoc-in / get-in but works with these structures?

vlaaad 2020-10-14T20:03:18.476100Z

that's what I did for now

vlaaad 2020-10-14T20:04:06.476300Z

I just don't like it. It could be get-in, but it has to be a separate function that interposes items with :grid

2020-10-14T20:08:33.476600Z

a common problem I feel like I see a lot is entangling the kind of logical structure of data with the physical structure.

2020-10-14T20:10:02.477100Z

one way this happens a lot is people writing games with grids try to represent the logical grid structure as a physical nesting of vectors, since that is the physical structure that most closely kind of looks like a grid

2020-10-14T20:10:37.477300Z

but a grid is just an index

2020-10-14T20:11:17.477500Z

a way to look up values by column, row, and maybe a combination of column and row

2020-10-14T20:12:05.477700Z

and once you treat it as a general index/lookup function you can start hanging other data in there and other indices and ways to do lookups

phronmophobic 2020-10-14T20:12:26.477900Z

I concur with @hiredman

2020-10-14T20:13:01.478100Z

so the common advice is to make it a map with a pair [col, row] as keys

2020-10-14T20:13:58.478300Z

and depending on what you are doing that might be enough, but it isn't a fully general indexing system (you can't lookup all the items in a colum or all the items in a row, etc)

Nassin 2020-10-14T20:15:45.478500Z

looks like nested vectors is the more flexible then šŸ˜‰

2020-10-14T20:17:09.478700Z

absolutely not

Nassin 2020-10-14T20:18:12.478900Z

well, for you grid example, query individual columns or row is trivial

phronmophobic 2020-10-14T20:18:15.479100Z

I think focusing on the data type and what abstract operations it should fulfill is a good idea. you can then change the underlying implementation as needed depending on usage and performance

vlaaad 2020-10-14T20:20:29.479300Z

well, for all the uses I have nested vectors beat maps. I need to know last row. Given a row, I need to know last column. In the app there will A LOT of such data structures, so memory is also concern

2020-10-14T20:20:41.479500Z

but you can't hang any additional data anywhere, and you can't efficiently do inverted queries "give me all the column ids that a value bar somewhere in them"

vlaaad 2020-10-14T20:21:01.479700Z

I don't have such use case

2020-10-14T20:21:24.479900Z

"Except one thing: it needs a small piece of data attached that I want to lookup by it's key."

vlaaad 2020-10-14T20:22:15.480100Z

That one thing is "given coordinate path, I want to lookup a cursor at that path"

vlaaad 2020-10-14T20:22:55.480400Z

poor wording in the initial description, sorry

2020-10-14T20:24:10.480600Z

updates another place where conflating the logical structure with the physical structure makes things tricky

2020-10-14T20:25:00.480800Z

it is a lot easier to update a "flat" index (thing a normalized database schema) then a deeply nested tree structure

vlaaad 2020-10-14T20:25:53.481Z

But I do have nesting, it's not a matrix NxM, it's a rows of varying length, where every item is itself a rows of varying length

2020-10-14T20:26:22.481300Z

that is literally the description of the logical structure of data in a graph database

2020-10-14T20:26:38.481600Z

where the data itself is stored in a flat index structure

vlaaad 2020-10-14T20:26:46.481800Z

hmm

donyorm 2020-10-14T22:55:02.483Z

Is there a way with tools.deps to list all dependencies (including transitive ones)? I want to be able to search for a specific dependency to see if it's included in my classpath?

p-himik 2020-10-14T23:07:54.483500Z

clj -Stree

dpsutton 2020-10-14T23:08:08.483700Z

-Stree Print dependency tree from clj --help

wombawomba 2020-10-14T23:14:11.485200Z

So I noticed that a library I was using (cljs-ajax) has deprecated EDNĀ as a HTTP request format in favor of Transit, which got me thinking: what are the downsides of using EDN over HTTP? And how is Transit better?

wombawomba 2020-10-15T10:13:06.497Z

cool, thanks for the elaborate response :)

dpsutton 2020-10-14T23:18:17.485300Z

(so you know where to find help with that in the future)

p-himik 2020-10-14T23:28:22.485500Z

https://github.com/JulianBirch/cljs-ajax/issues/47#issuecomment-51251520 Couldn't find where exactly EDN is "explicitly not recommended for browser API" though.

p-himik 2020-10-14T23:30:24.485800Z

Another bit of useful information: https://groups.google.com/g/clojure/c/9ESqyT6G5nU/m/2Ne9X6JBUh8J

wombawomba 2020-10-14T23:41:03.486Z

alright, thanks

wombawomba 2020-10-14T23:41:47.486200Z

sounds to me like there arenā€™t any clear drawbacks to using EDN per se, except that there arenā€™t good parsers in most languages

wombawomba 2020-10-14T23:43:13.486400Z

I guess another drawback could be that thereā€™s less tooling for e.g. routing or deep packet inspection for EDN payloads than for JSON

p-himik 2020-10-14T23:43:48.486600Z

I think there were some other reasons. The one that I can remember is that compression is built into transit, which is nice to have.