Fork me on GitHub
#beginners
<
2019-01-08
>
nifflor11:01:37

Hi, i have a question about growing vectors as refs. I can't seem to update a vector in a ref:

nifflor11:01:46

(def agent-pool (ref []))
(dosync (alter @agent-pool concat (filter odd? [1 2 3])))
(concat [] (filter odd? [1 2 3]))

nifflor11:01:00

line 2 does not work, line three does.

nifflor11:01:33

CompilerException java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to clojure.lang.Ref, compiling:(nubank_task/core.clj:25:1

bronsa11:01:40

(alter agent-pool

bronsa11:01:52

you need to alter the ref, not the dereffed value

nifflor11:01:09

wonderful, thx!

nifflor16:01:58

and another:

(def data (ref [{:a "1"} {:a "2"} {:b "1"}]))
(defn remove-by-a
  [value data-ref]
  (dosync (alter data-ref remove #(= value (:a %)) )))
(remove-by-a "1" data)
gives IllegalArgumentException Don't know how to create ISeq from: nubank_task.core$remove_by_a$fn__4390$fn__4391 clojure.lang.RT.seqFrom (RT.java:542)

dpsutton16:01:53

alter will call your function with the ref's value and then the arguments so you'll end up with (remove ref-value #(= value (:a %)) rather than (remove pred coll)

dpsutton16:01:34

and the error there is saying it doesn't know how to turn that predicate into a collection

nifflor16:01:32

so the problem is with remove's argument order?

nifflor16:01:58

is the solution to write a wrapper around remove that switches arg order?

d.ian.b16:01:28

#object[com.google.cloud.datastore.GqlQuery 0x3a0c97b7 "GqlQuery{type={queryType=null, resultClass=class java.lang.Object}, namespace=null, queryString=SELECT * FROM User WHERE phone = @phone LIMIT @num, allowLiteral=false, namedBindings={allowliterals=Binding{cursor=null, value=StringValue{valueType=STRING, excludeFromIndexes=false, meaning=0, value=True}}, num=Binding{cursor=null, value=StringValue{valueType=STRING, excludeFromIndexes=false, meaning=0, value=1}}, phone=Binding{cursor=null, value=StringValue{valueType=STRING, excludeFromIndexes=false, meaning=0, value=+5512981491039}}}, positionalBindings=[]}"]

d.ian.b16:01:05

how can I change this object property,

allowLiteral=false
to true?

d.ian.b16:01:09

reflect gives me that allowLiteral is

{:name allowLiteral,
    :type boolean,
    :declaring-class com.google.cloud.datastore.GqlQuery$Builder,
    :flags #{:private}}

d.ian.b17:01:25

example code in java:

GqlQuery gquery2 = Query.gqlQueryBuilder("SELECT * FROM SomeUnit WHERE key HAS ANCESTOR key(organization,'somename')")
.allowLiteral(true).build();

d.ian.b17:01:31

(.setAllowLiteral (Query/newGqlQueryBuilder "...") true)

lepistane18:01:15

There is no cheshire channel so i am asking here. I added dep [cheshire "5.8.1"] to my projects (uses clojure 9) and i can't cider jack in anymore Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.async.ByteArrayFeeder does anyone know what this is about? I've used cheshire before without problems

noisesmith18:01:08

@lepistane check the output of lein deps :tree or lein deps :plugin-tree - the cause of an error like that will show up explicitly as a dep conflict there

noisesmith18:01:18

for example, a dep of cheshire could pull in a library version of jackson that lacks a class that cider needs, usually the fix is explicitly asking for the version of the lib that you need

noisesmith18:01:03

maybe it's too much to suggest CIDER not use deps that so notoriously break between versions

noisesmith18:01:35

(or maybe I'm misled and the source of the error has nothing to do with CIDER - the deps tree will expose the source of the issue)

lepistane18:01:53

@noisesmith that was some quick help! i appreciate it very much! luckily this time around i saw that ajax lib is using cheshire but lower version so i am just using that and it works now. There was no conflict with deps but i will checkout how i can request that jackson feeder and use latest cheshire

lepistane18:01:00

btw this is first time i am seeing 'consider adding these excusions' and there are bunch of deps with that. How bad is this since i am first time looking at this? Should i add all exclusions?

noisesmith18:01:53

that is what conflicts look like in deps tree, it's saying that these libs ask for different versions and suggesting how to explicitly decide which wins

noisesmith18:01:45

I usually don't bother until I see a ClassNotFound or similar, those are signs of breakages between versions that mean I need to use the exclusions to manually fix deps

lepistane18:01:49

Alright. I will add this to my checklist. Thank you very much

alexmiller19:01:27

(fyi, a surprisingly high number of dependency problems relate to jackson :)

noisesmith19:01:43

yeah, that's my experience also, using clojure.data.json just for avoiding jackson starts to seem like a good idea

jstaab20:01:29

Any more idiomatic way to do a nested map, e.g. (map (partial map inc) [[1 2] [3 4]]) as (nested-map inc [[1 2] [3 4]])? Not arbitrarily deep, just one deep. Kind of a companion to mapcat

jstaab20:01:57

Also anything equivalent to (assoc % :x (get % :x 1)) like (assoc-if-not-set % :x 1)

seancorfield20:01:55

@jstaab (defn assoc-if-not-set [m k v] (update m k (fnil identity v)))

jstaab21:01:19

ah fnil sweet, knew I'd seen something like that before

mathpunk22:01:39

I've got these log files coming out of a service, and I was going to examine them for some stuff, but they come out with a bunch of gnarly characters. E.g.,

[2m2019-01-08 22:01:17.375[0;39m [32mDEBUG[0;39m [35m624[0;39m [2m---[0;39m [2m[nio-8081-exec-5][0;39m [36mo.n.o.drivers.bolt.request.BoltRequest  [0;39m [2m:[0;39m Request: match (n) return count(n) as nodes with params {}
(wow that looks even worse pasted than it comes out in the repl)

mathpunk22:01:50

I think those are codes to color code the output

mathpunk22:01:56

I also think it's.... "us-ascii"?

mathpunk22:01:36

what's the right way to ignore those characters so I'm just getting the text? I tried feeding in :encode options of "UTF-8" and "us-ascii" to my reader but, same difference

lennart.buit22:01:53

These are ansi escape codes

lennart.buit22:01:11

they tell consoles to print in colour indeed

mathpunk22:01:52

is ansi an encoding that I can feed to the reader?

noisesmith22:01:58

I bet you could find a regex for deleting them

noisesmith22:01:26

ansi uses escape codes to control a terminal, it's not something I would want in stored log data

lennart.buit22:01:33

its not an encoding, they are plain ascii, many terminals just assume that they are ansi escape codes and replace them with whatever is specified

noisesmith22:01:37

in the clojure ecosystem, you can usually figure out which middleware / extension / utility lib is adding ansi escapes into your log messages and turn it off

lennart.buit22:01:16

right, do you know which library is logging those, @mathpunk?

mathpunk22:01:13

no clue, this is a service I don't run. I'm checking with the service owner to see if he knows these are getting logged. It's also entirely possible I'm consuming the wrong thing

noisesmith22:01:02

I find it more believable that it's just a poorly designed logger that assumes that the only way you look at the messages is by piping them directly to a terminal (where the color codes will actually work)

noisesmith22:01:19

if your goal is to just read them, using cat should result in color coded text in term

noisesmith22:01:30

@mathpunk worst case you can use eg. sed to remove the escapes and read normally https://superuser.com/a/380778

mathpunk23:01:30

I actually want to turn this into data, not just read it.... and that link has a regex for "remove all control sequences" so I'm well on my way. thanks again!

mathpunk23:01:49

I actually started building a regex to remove them, and then thought, "Wait this might be solved by the right usage of encodings"

mathpunk23:01:08

so, you've both helped 🙂

noisesmith23:01:23

this isn't an encoding thing, the escapes are meant to mess directly with a terminal

mathpunk23:01:27

yeah I get it now

noisesmith23:01:30

but I guess that was already said :D

lennart.buit23:01:30

(I feel inclined to reference https://xkcd.com/1171/)

lennart.buit23:01:55

especially the title text had me rolling on the floor first time I read it

mathpunk23:01:21

it's very good! i might share it with my non-technical hip-hop-loving girlfriend to get her to stare blankly at me 😄