Fork me on GitHub
Michael J Dorian14:02:20

Hey friends, what are the performance characteristics of swap! ? I have a program that stores a bunch of state in a single atom, (roughly 30 kb of data once exported to json, but that should grow substantially as the simulation becomes more complex) and calls swap! on that atom a minimum of once per frame, which is roughly 20 times a second. I wonder partly because I seem to have a pretty bad memory leak, I haven't had time to really search it out yet but I thought I would check if my whole premise was an issue before I search too hard simple_smile profiling indicates that it comes from the hottest loops, which is where the global atom is being processed and swapped. Thanks!


swap! is, most likely, not your bottleneck. It’s pointer change. How do you change data structure kept in atom? Problem would rather be in processing data before swap! call, or any code that listens to atom change and processes new value.

Michael J Dorian14:02:56

I change it lots and lots of ways! Thanks, that makes sense, glad to know I'm not totally misusing swap 😄


Have you tried benchmarking (swap! your-atom your-change-fn) vs (your-change-fn some-data)? I'd suspect your-change-fn might be a major contributor.

Michael J Dorian14:02:51

I haven't yet! The profiling made it look like swap was the hotspot and I didn't find any information on if it was ok to run swap dozens of times a second, this narrows my search a lot!


Hi everyone. Is there any way to convey a dynamic var's binding into the body of a list comprehension without using another var?

user=> (def ^:dynamic *foo* 0)
user=> (binding [*foo* 1]  *foo*)
user=> (binding [*foo* 1] (for [n [1]] *foo*))
user=> (binding [*foo* 1] (let [p *foo*] (for [n [1]] p)))
I want the (0) to show (1) without doing the last line. I want *foo* itself to resolve to 1. Is this possible?


(binding [*foo* 1] (doall (for [n [1]] *foo*)))


The issue here is that the lazy collection returned by for is realized only outside of the binding.


aaaah riiight


What is the fastest way to reset the REPL to the state when it is freshly runed? I want be sure all ns are unloaded and I don’t want to lose time for reset REPL each time to be sure if dependencies are correctly reduced.


@borkdude I don’t think this is what I need. Does it demand Sierra component? Also the issue is not reloading, but unloading in this case.


I remove dependency from ns dependencies and reload ns. It works. But if I kill REPL and load ns then not.


ok I found manually which dependency make the issue, but anyway I am interested in how to reset the REPL without kill and run again


But first, it will unload (remove) the namespaces that changed to clear out any old definitions.


which i think is what you want


Hmm it could work. I have to try. Thanks.


yes, I think you seek refresh (only affects namespaces), as opposed to reset (affects a System)


What does one typically do when your deps.edn includes dependencies that have transitive dependencies that are only available in 3rd party Maven repositories? Is deps expected to be able to handle this case?


@mac01021 there is a :mvn/repos option in the deps.edn


you should add the repository there

thanks2 4
Ramon Rios18:02:29

Does anyone here uses postal for sending email by SMTP?


What libraries are people using for testing http calls?


we use postal to create messages but just use javax.mail to send it

Ramon Rios19:02:00

My doubt it was i was able to send a attachment by a inputstream

Ramon Rios19:02:32

Because i saw that i can send with


I was using Postal in the past, but I don’t remember too much


I haven't use .File with postal, but my experience with the jvm generally is that people often assume they need a File when really they could use something more abstract, or not reliant on using a fileystem

✔️ 4

eg. io/resource to read from inside a jar, or ByteArrayInputStream to read directly from bytes in memory


I think postal shells out so it may actually need a file


If I remember postal has :attachments key in map so you can set paths to files.


I didn’t use any other method


oh actually it is not a path ,but so probably you can use stream too

Jakob Riishede Møller20:02:12

Remember that (+ [1 2 3]) is erroneous - I wonder why (max [1 2 3]) is not - the result is [1 2 3] even though no ordering of vectors is defined: (max [1 2 3] [4 5 6]) is erroneous as well. I think it would have made more sense to return an error for (max [1 2 3])?


There is a defined order for vectors, max just doesn't use it

Jakob Riishede Møller21:02:06

OK? For all pairs of vectors? Which is greater [1 50 0] or [-100 1000 9]?


Use compare and see

Jakob Riishede Møller21:02:52

OK - I tried and got some 1 and minus 1 results - does it make sense? - to me it does not - please explain.

Jakob Riishede Møller21:02:16

And why does max not use that order then?


The doc string for compare explains the returned numbers


max only works on numbers

Jakob Riishede Møller21:02:46

It seems that compare is another buggy function - unless you can make sense of that order for any given pair of vectors?


it is a well defined total order

Jakob Riishede Møller21:02:36

Ha ha - it seems to compare strings?

Jakob Riishede Møller21:02:00

Yes - I read that - try to explain the math


it isn't math

Jakob Riishede Møller21:02:31

so max is not a mathematical function?


I forget the exact algorithm vectors using when comparing


like I said, max is only for numbers, which is why max doesn't use compare, which is a generic way to compare things

Jakob Riishede Møller21:02:43

OK - which is intuitively biggest: [0 1] or [1 0]?


sort for example (which doesn't actually use clojure.core/compare, but is built on Comparable) can sort lists of vectors


it doesn't matter to some degree


there is a defined order though

Jakob Riishede Møller21:02:21

But one must be biggest if there is an order?


and clojure.core/compare tells you

Jakob Riishede Møller21:02:58

And I guess it is [1 0]

Jakob Riishede Møller21:02:35

But it makes no sense from a mathematical viewpoint - remember I asked about max


the algorithm is, a vector with fewer elements is always less than a vector with more, and if vectors have the same number they are compared element by element until they differ, returning the first difference


you said "even though no ordering of vectors is defined"

Jakob Riishede Møller21:02:52

Yes - fine - I wonder what the use of that is


and I am correcting that


sorting things is extremely useful

Jakob Riishede Møller21:02:18

OK - from a mathematical viwepoint there is no ordering


I dunno why you keep saying mathematical viewpoint

Jakob Riishede Møller21:02:13

Becausemax is a mathematical function


math is perfectly fine with non-numeric objects and arbitrarily defined orderings

Jakob Riishede Møller21:02:16

Do you know what a mathematical ordering is?


I think you may be saying math and mathematical when you mean numeric, but please enlighten me

Jakob Riishede Møller21:02:13

Anyway - there does not seem to be a such for vectors in clojure - at least it does not apply to the max function

Jakob Riishede Møller21:02:53

And that was my point - sorry if it was not clear


still isn't clear, you keep saying there isn't a mathematical ordering for vectors, and as far as I know math would be fine with the ordering that is defined


> The notion of order is very general, extending beyond contexts that have an immediate, intuitive feel of sequence or relative quantity. In other contexts orders may capture notions of containment or specialization. Abstractly, this type of order amounts to the subset relation, e.g., "Pediatricians are physicians," and "Circles are merely special-case ellipses."

Jakob Riishede Møller21:02:23

So again - an order can be defined that makes [1 0] larger than [0 1] - but it does not apply to tha max function

Jakob Riishede Møller21:02:42

No - exactly max does not work for vectors


I see, yes, by "max doesn't use it" I meant: there is a defined order for vectors and max doesn't use it, and since max doesn't use the defined order for vectors, it doesn't work on vectors

Jakob Riishede Møller21:02:28

Fine - I know that you can define orderings and partial orderings, but that was not my point here


no idea what your point is, I was just correcting a specific part of what you said, that there is no ordering of vectors defined, which as demonstrated by both compare and sort there is

Jakob Riishede Møller21:02:13

Like reading out of context?


@jakob.riishede.moller many clojure functions return identity for a single arg without consuming it or checking its validity in any way


user=> (conj :a)


this is useful for cases where you would reduce across an input collection and need a sensible base case

Jakob Riishede Møller20:02:55

Is [1 2 3] a sensible base case for a function that computes the maximum?


and identity of [1 2 3] is [1 2 3]


it works accidentally, whether clojure should eagerly reject invalid inputs is a ship that has sailed already

Jakob Riishede Møller20:02:25

Well, if at least there was consistency, so that (+ [1 2 3]) returned [1 2 3]?


I agree that the inconsistency is annoying, I doubt there's an easy way out. That said I find clojure generally more consistent than most languages I've used.


Pragmatically, I think it's better to avoid invalid inputs (knowing you can't count on being saved from them by the compiler or the runtime). I have my own pet peeves (eg. merge accidentally working, sometimes, on things that are not hash-maps)


or (Math/PI) working in the place of Math/PI (despite not being an invocation)

Jakob Riishede Møller20:02:15

Well - thank you @noisesmith - at least: now i know 👍


Are there any libs or examples of neat ways of interacting with nav-able structures at a REPL

Alex Miller (Clojure team)20:02:36

REBL works with Cognitect's AWS lib, dirs/files, data for charting, Java class hierarchies, jdbc data, etc


@lilactown next.jdbc produces datafyable/`nav`able result sets by default from execute! and execute-one!


I wasn’t clear enough. I am not looking for examples of datafy and nav, and I know about REBL. I’m wondering if anyone has explored a more ergonomic way of interacting with datafy/nav directly at a REPL. Sort of like


(preferably that you could evaluate in a buffer though, so datawalk doesn’t fit exactly what I’d like)


I guess the “semi-interactive” version of datawalk is basically what I’m thinking of


omg I'm dying waiting for that datawalk gif to load


you can totally make something like datawalk for nav, but then you'd be re-building REBL @lilactown


yeah it takes forever


I understand. the tradeoffs I’m thinking about for REBL are:


• doesn’t work in CLJS (which is where I actually want to use this right now) • have to leave my editor to use it


You might already know about it, but shadow-cljs has a an "inspector" which is quite handy!


I do know that. like I said, there’s already a purpose built external devtool for this. I specifically want something I can use in my editor / REPL

👌 4

for my use case, there’s already a purpose built set of devtools for visualizing the objects I want to datafy and nav and I’m interested in building some utilities to interact with them at the REPL


I was hoping I could implement some wrappers around it with datafy + nav and call it a day 😉 but looks like I’d have to build it myself anyway


i think @thheller is cooking something exciting up along these lines


datawalk doesn't seem work in nREPL, womp womp


thheller’s inspector looks awesome but, again, there is already an excellent external devtool for inspecting this data; I specifically want something that works at a REPL. I have my answer, which is, this does not exist yet as far as we know 😄


I don't think you can get very far if you constrain yourself to a text interface

✔️ 4

as long as you can assume no background thread is trying to print, you could have a curses UI inspector


not sure if it would be worth the trouble to implement...


There are certain use cases that are better at a REPL imo


I'm trying look at the current state and props of a react component instance without leaving my editor


It's simply walking the react fiber tree and finding where the element type equals the component I want to inspect


But react fibers are opaque data (to CLJS) so I thought I could buy myself some time and effort and use datafy/nav. But datafy/nav is too raw to really be usable at the REPL for that


sounds like you just want tree-seq and filter

👍 4

navigating is such a low level thing, don't do it, be lazy, search


This code:

(defn- grant->pin [grant]
  {:id (:id grant)
   :amount_recommended (:amount_recommended grant)
   :organization_name (:name grant)})

(defn aggregate-pin-data [data]
  (-> data
      (get-in [:records :grant_request])
      #(map grant->pin %)))
is throwing:
class clojure.lang.Symbol cannot be cast to class clojure.lang.IPersistentVector (clojure.lang.Symbol and clojure.lang.IPersistentVector are in unnamed module of loader 'app')
Which symbol is it trying to cast to a vector? Seems to be complaining about the #(map ...) lambda but beyond that I'm a bit lost...


(defn aggregate-pin-data [data]
  (-> data
      (get-in [:records :grant_request])
      (#(map grant->pin %))))


try this ☝️


that works!


I could've sworn I've passed lambdas like that to -> , does this not work at all? Or only in specific cases?


It shouldn't work


@ctamayo it helps to look at the macroexpansion of the -> form


@noisesmith I should've thought of that 😛


user=> (macroexpand '(-> x #(+ % %)))
(fn* x [p1__224#] (+ p1__224# p1__224#))


notice how x becomes the name of the fn, and doesn't appear in the body at all


don't try to fit everything into a -> chain


it's not like a Street Fighter combo


here's how the (()) hack fixed it:

user=> (macroexpand '(-> x (#(+ % %))))
((fn* [p1__228#] (+ p1__228# p1__228#)) x)
and agreed with @ghadi, it's cleaner not to try to wrap it like that





👍 12

that'll finish him.


anyway, the (#(,,,)) hack definitely feels gross...what's the suggested style?


The simplest: extract a function

👍 4

it took me a while to get that to be honest


use a let, or arguably (-> x (f y) (->> (map g))) but even this is controversial


->> / as-> etc. are intentionally designed so that they can nest inside -> fwiw, but opinions about actually doing so are mixed


(->->><?as->cond->! x
 :-> (f y)
 :->> (map g))

😂 8

OK poe's law definitely got me on that one


I'm gonna go with "yikes"


Without threading it's pretty simple.

(defn aggregate-pin-data [data]
    (map grant->pin
      (get-in [:records :grant_request] data)))


if you're not using randomcorp's thread-first-thread-last-backwards-question-mark-as-arrow-cond-arrow-bang, you're missing out: Also check out the video:


Thanks for the much-needed reminder. I've literally seen every clojure talk ever....But I guess I still haven't learned my lesson because I'm not modeling this in XML.