Fork me on GitHub
#clojure
<
2018-08-31
>
rahulr9205:08:04

Is there any way to list all the atoms accessible in the current scope?

rahulr9205:08:48

To elaborate a bit more on the use-case, I am trying to see if I can get all atoms and add a watcher to them. This watcher is for a cross-cutting concern, specifically to log their values whenever they change.

jaihindhreddy-duplicate05:08:07

@rahulr92 I'm not sure there is such a thing but you can wrap the atom fn that takes care of the cross cutting concern, and use that wherever appropriate.

rahulr9205:08:30

@jaihindh.reddy Do you mean add the watcher to the atom when its created itself? Yes, that is an option. I was wondering whether there was an easier way.

jaihindhreddy-duplicate06:08:17

@rahulr92 Yes that's what I meant. Even if there is a way to find all atoms currently and attach watchers to them, I would want to answer questions like, "what about atoms that get created after I hook into all atoms?". Having a wrapper like logged-atom would be easier to reason about IMHO.

rahulr9206:08:22

@jaihindh.reddy Agreed that that would be the ideal scenario. In my specific use-case, I do not have direct access to the atom creation but these are passed in. Hence creating wrapped atoms isn’t an option.

jaihindhreddy-duplicate06:08:28

#(if (instance? clojure.lang.Atom %) (cross-cutting-concern %) %) perhaps

deliciousowl07:08:34

is there a naming convention for futures? or is this a dumb question 🙂

mpenet07:08:40

not really (works for both: no convention, and it's not a dumb question)

rahulr9208:08:23

@jaihindh.reddy Thanks. I will consider this option.

dominicm09:08:44

When reading edn, is it possible to capture the line/column of elements, particularly tagged literals? I'm able to capture where they end by using a pushback reader, but the start location is generally more useful.

dominicm10:08:48

@bronsa does tools reader provide some way to get this information?

bronsa10:08:57

not for the edn reader, no

bronsa10:08:26

i'll take patches enabling optional line/col annotation for the edn reader if you care

bronsa10:08:42

with optional being the keyword

dominicm13:08:14

I might consider it, but I'm not sure I care that much. I just wanted my config reader to have a better error message when I process parts of it later.

lilactown15:08:24

Question: Let’s say that I have an application that needs a schema language, written in Clojure, that is back-end agnostic. E.g. it could be used to create records stored in postgres, dynamodb, contentful, or datomic. The schema language doesn’t need to be super powerful, but extensible as our needs grow. It’s for configurations of business rules for components. Does something like this already exist for Clojure? Could it be clojure.spec?

igrishaev15:08:20

Hi, I just published my first Clojure book about Java interop. It consists of 60 pages and seven chapters with practical tasks: https://leanpub.com/clojure-java-interop

36
lilactown16:08:15

@benzap let’s keep it in #clojurescript 😉

andy.fingerhut17:08:08

@igrishaev You might want to send that to the #announcements channel, too

Ramzi17:08:18

This line is producing a table with one text column called data: (j/create-table-ddl :asdf [[:data :text]])) How can I add a second field? My attempts have failed. I tried (j/create-table-ddl :asdf [[:something :text data :text]])) and (j/create-table-ddl :asdf [[:data :text][:something :text]])) and neither worked.

Ramzi17:08:18

I dont know what changed, but I fixed it by copying and pasting some code.

Ramzi17:08:13

So that code produces a table called :asdf. How can I pass in a parameter called table-name and do :table-name so that if I pass in "foo" it will make a table foo?

dadair18:08:11

You would need to wrap that expression in a function definition (defn) that accepts an argument and then pass that argument to the create table expression. You would then need to actually call the function with the appropriate argument.

Ramzi18:08:44

so would it look like :(str table-name)?

dadair18:08:10

No, you would just pass a keyword argument to the function:

(defn some-func [some-arg]
  <your expression>)

(some-func :some-table-name)

Ramzi18:08:52

(defn create-table [table-name] (j/create-table-ddl :asdf

Ramzi18:08:08

how do you replace the asdf with table-name?

dadair18:08:35

By just using table-name instead of :asdf. This is very basic Clojure, you may want to consult a beginners book. Here is a popular one that is both online and free: https://www.braveclojure.com/foreword/

Ramzi18:08:52

Oh I had tried that but I thought it didnt work. Sorry let me try again

Ramzi18:08:39

Thank you. You're right, that was very basic. I dont know what I was thinking. Thanks for your help.

Ramzi18:08:00

Do you see anything wrong with this?

Ramzi18:08:04

(defn update-record [table-name record] (j/update! table-name {:id 8}["body = ?" record])

Ramzi18:08:40

I have a row in my db that has id 8 and a body of "aaa". I am calling update-record with "dddd" as the record param, but it is not updating my db

dadair18:08:13

You aren’t calling update! correctly. If you read the documentation of the function (which you link), the argument after the table name is a map of the columns and their values that you want to set, and the last argument is the where clause. You probably want:

(j/update! ".." table-name {:body record} ["id = ?" 8])

Ramzi19:08:37

Thanks! That worked!

Denis G18:08:54

Aren't agents like actors but with a function being passed to the actor in order to process the message? I mean of course agents do not have supervision and stuff, but it could have been implemented, right? Link to stackoverflow question: https://stackoverflow.com/questions/3259296/how-clojures-agents-compare-to-scalas-actors

deliciousowl19:08:21

I'm reading a style guide that suggests to use "Prefer function pre and post conditions to checks inside a function's body." e.g. using :pre .... does this really make sense?

noisesmith19:08:01

depends, do you want the error to be fatal but disabled conditionally in prod (AssertionError) - if so, pre and post conditions are idiomatic

noisesmith19:08:10

if you want an exception that a try/catch could handle (eg. it's a response to user data and not catching a problem with your code per se) then AssertionError is likely not what you want

idiomancy21:08:09

what's the predicate I'm looking for to test to see if something supports keyword lookup?

benzap17:09:57

There's also associative?

benzap17:09:33

hmm.. that might just be for checking if it supports assoc

idiomancy22:08:47

I guess I'll just use the less general map?

john22:08:19

satisfies?

john22:08:36

maybe ILookup

john22:08:31

Sorry, ILookup is available in cljs

idiomancy22:08:45

ahh, interesting. thanks!

emccue23:08:18

@igrishaev Just read the example chapter. Buying now

Dormo23:08:00

Is anyone familiar with monger? I can't figure out how to use the $position modifier with it. Is there support? https://docs.mongodb.com/manual/reference/operator/update/position/

Dormo23:08:36

Nevermind! figured it out

Dormo23:08:50

I just had to use $position as a string

Dormo23:08:11

(mc/update db "rooms" {:name room-name}
             {$push {:tracks {$each [track]
                              "$position" index}}}))

emccue23:08:58

oh thats interesting

emccue23:08:09

well, at least there was a workaround