Fork me on GitHub
#beginners
<
2022-01-03
>
Benjamin12:01:58

what is a transducer way to say some ? What I'm after is finishing the transduction as soon as 1 element satisfies a pred

flowthing13:01:40

halt-when

👀 1
Ben Sless14:01:07

Another way is to do it with a different rf, take a look at the xforms library

Aviv Kotek14:01:45

hi, using spec, is there some way to validate specs with OR? (s/def :a/x int?) (s/def ::A (s/keys :req-un [:a/x])) (s/def :b/y string?) (s/def ::B (s/keys :req-un [:b/y])) (s/explain (s/or ::A ::B) {:x 1}) => {:x 1} - failed: (contains? % :y) at: [:user/A] spec: :user/B while i'd expect it to succeed as :`:A` is valid? what i'm missing here?

Alex Miller (Clojure team)15:01:54

You have the wrong syntax for s/or

Alex Miller (Clojure team)15:01:07

Each sub spec also has a keyword tag

Alex Miller (Clojure team)15:01:47

So something like (s/or :a ::A :b ::B)

Aviv Kotek15:01:10

but doesn't :A include :a ?

Aviv Kotek15:01:17

as :A and :B are huge (each spec defined separately) - i'd want validate on some or some composition of

Ben Sless15:01:37

Or takes a sequence of pairs of name and spec

Alex Miller (Clojure team)16:01:52

The :a and :b are labels for which branch of the or is matched, not references into the spec

sova-soars-the-sora19:01:22

I want to know the last seen shape of data as it flowed through a function. Something like... cider with a local copy of sample results saved when i run things... and a way to pull that info up

sova-soars-the-sora19:01:22

having a "last snapshot of the inputs to this function" by input-arg would be superhuman tooling

sova-soars-the-sora19:01:49

something like "cider cache"

sova-soars-the-sora19:01:07

where I can press a key combo and see the last time this function was run, what the inputs looked like and what the output was.

Ben Sless19:01:22

Something like omni-trace or sayyid?

😮 1
Ed19:01:27

seems like that should be easy enough to cobble together ... here's my attempt:

(comment

  (defn- priv [& args]
    (throw (Exception. "test")))

  (def calls (atom nil))

  (defn instument-ns []
    (->> (ns-interns *ns*)
         vals
         (remove #{#'instument-ns})
         (map (fn [v] (alter-var-root v (fn [f] (if (fn? f)
                                                  (fn [& args]
                                                    (swap! calls assoc v {:args args})
                                                    (let [[r e] (try [(apply f args)] (catch Throwable t [nil t]))]
                                                      (if e
                                                        (swap! calls assoc-in [v :error] e)
                                                        (swap! calls assoc-in [v :result] r))
                                                      (if e
                                                        (throw e)
                                                        r)))
                                                  f)))))
         dorun))

  (instument-ns)

  (priv :thing2)

  (get @calls #'priv)

  )
I guess that'd probably work without cider too ... ??

sova-soars-the-sora20:01:01

Right right, reckon I have a huge project though with many files. And I want cached results for whatever has been run in any file... maybe even independently of runtime

Ed21:01:36

(defn instument-ns []
    (->> (all-ns)
         (filter #(-> % ns-name name (.startsWith "user")))
         (map ns-interns)
         (mapcat vals)
         (map (fn [v] (alter-var-root v (fn [f]
                                          (if (fn? f)
                                            (fn [& args]
                                              (swap! calls assoc v {:args args})
                                              (let [[r e] (try [(apply f args)] (catch Throwable t [nil t]))]
                                                (if e
                                                  (swap! calls assoc-in [v :error] e)
                                                  (swap! calls assoc-in [v :result] r))
                                                (if e
                                                  (throw e)
                                                  r)))
                                            f)))))
         dorun))
you could do it for all namespaces that start with a prefix? ...

Ed21:01:22

I tried sayyid quite a while ago, it seemed good, but I ended up not using it much ... I've not tried omni-trace ...

sova-soars-the-sora01:01:38

It would be cool to peg down a tool. How can I run the code you wrote?

stopa19:01:34

Hey team, I’m having some trouble getting uncaught exceptions to log, in the following case: Say I have a -main function like this:

(defn -main [] 
  (throw (Exception. "test")) 
If I try to run it:
lein with-profile production run -m my-project.my-ns
I get something along the lines of this:
SLF4J: A number (104) of logging calls during the initialization phase have been intercepted and are
SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system.
SLF4J: See also 
Syntax error compiling at (/tmp/form-init7632496378080193395.clj:1:73).
test!

Full report at:
/tmp/clojure-1115158083760869108.edn
Error encountered performing task 'run' with profile(s): 'production'
Suppressed exit
But the problem is, I think this kind of error is completely shutting the system down, and hence no logs are being sent to my logger. I’m not quite sure how to make things work so that even in this case the logger gets a message. Thoughts much appreciated!

stopa20:01:33

(currently just wrapped the work in -main under a try catch, but would be curious to hear if there’s a way to avoid that)

paulocuneo20:01:05

-main is the entrypoint/start/of your javaprocess it wont handle any exception by default. Since an exception is mechanism to stop "normal execution flow" to indicate something "exceptional" has happen, -main cant know what to do with the exception other than stop the program. So if you want to catch and log any exception you should wrap your logic inside main with a try catch, and catch.

(defn -main [& args]
  (try  
      (your-logic-or-service)
      (catch Exception e
        (logging-library-you-are-using/error e)
        (throw e)  )))     
Notice I'm re-throwing the exception, in general, exception handling should handle the exception, i.e. stop the exception propagation, but here I wouldn't know want to do other than stopping the java process with and error. Usually you would log the exception were it is handled

paulocuneo20:01:20

that's correct, main is the top of the stack, if the exception reached main with out being handled, there's no much left to do than stopping the process with an error

stopa22:01:42

This makes sense. Thanks for the context @U487Z32UF!

emccue19:01:35

@ps

(def data {"Leading zeros ids" "00121", "Test" "123", " test 1" "abc"})

(defn interpret-numbers 
  [data]
  (update-vals
    data
    (fn [v]
      (if (and (every? #(Character/isDigit %) v)
               (or (= v "0")
                   (not (string/starts-with? v "00")))
        (parse-long v)
        v))))
        

emccue19:01:43

using parse-long and update-vals from 1.11 - there are sorts of map-vals in multiple libraries and you saw Long/parseLong is a good enough replacement for now

emccue19:01:36

(just combining a bit of the other advice)

seancorfield21:01:32

@emccue Sounds like @ps wants a ClojureScript solution, not Clojure. Or at least, both Clojure and ClojureScript.

grierson22:01:41

Hey, How do I equality check a defrecord with a hashmap which both have the same values? Trying to write a test which compares the two but the defrecord has a tagged literal

Ed22:01:51

You could always construct an instance of your record from the map with the automatically provided conversion function. Something like:

(defrecord Thing [a b])

  (= (->Thing 1 2) (map->Thing {:a 1 :b 2})) ;; => true 
Is that what you mean?

grierson22:01:53

Yeh, That works 🙂 Thanks

👍 1