Fork me on GitHub

(.requestAnimationFrame js/window (fn [ts] (swap! db assoc :ts ts)))
How can I turn this into a fn that calls itself in the callback? I don't want to move it to its own defn.
(defn im-not-happy
  (.requestAnimationFrame js/window (fn [ts]
                                      (swap! db assoc :ts ts)
                                      (im-not-happy db))))


You can give an anonymous function a name to call itself recursively (although I guess it's not really anonymous anymore, just "keeps mostly to themself")

(fn happy [db]
  (.requestAnimationFrame js/window (fn [ts]
                                      (swap! db assoc :ts ts)
                                      (happy db))))
Why not move it to its own defn? You have to give it a name either way.


Oh right, thanks!

((fn happy-enough []
       (.requestAnimationFrame js/window (fn [ts]
                                           (swap! db assoc :ts ts)

👍 1

I'm hacking away and I want something that I can inline where I'm working. It's for ergonomics really. I'm not being too rational.

👍 1

Or if you wanted to get fancy:

(((fn [f]
    (fn []
      ((fn [f']
         (f (partial f' f')))
       (fn [f']
         (f (partial f' f'))))))
  (fn [happy-enough]
    (.requestAnimationFrame js/window (fn [ts]
                                        (swap! db assoc :ts ts)


if you aren't using recur, you could blow the stack with unbounded recursion - not sure if that's the case with JS engine, last I knew, nodejs at least had dropped TCO support - not sure if browsers ever supported it


@U96DD8U80, that doesn't apply here because it's not being called directly, but via a callback


Ah, thanks, I just read "a fn that calls itself in the callback" - so it isn't recursing at all, its just referencing a first class function/callback by a fn label instead of a def or let assignment? Or are you saying that if a function recurses on itself, it is free of stack concerns if it has first been referenced/elevated to a callback?


I would probably consider it mutual recursion, but the A calling B which calls A doesn't infinitely consume the stack because .requestAnimationFrame 's behavior and event loop acts similar to a trampoline

Drew Verlee03:07:55

Is trampolining a concept i missed in CS class? It comes up from time to time and im guessing there not referring to the idea of bouncing up and down.


trampolining is a common technique for implementing recursion without blowing the stack. It's sort of related to bouncing up and down. If you run a recursive algorithm the normal way (without tail call optimizations) the stack will continue to grow as it recurses. If you run the same algorithm via a trampoline, the stack will push and pop (ie. bounce) for each recurse. Not all recursive algorithms can leverage tail call optimization. The example that kicked off this thread doesn't really have anything to do with trampolining, but it is a recursive procedure that won't blow the stack. Tail call optimization doesn't apply either. The reason the recursive calls with .requestAnimationFrame don't consume the stack is that the next task isn't pushed onto the stack, but is enqueued and processed by an event loop. I'm not totally sure I'm explaining this well.


I'm having trouble finding a great writeup on the internet, but this stackoverflow answer isn't too bad:


Actually, the wikipedia article on tail call is really good:

👀 1
Drew Verlee05:07:43

Thanks for the reply. I'm check this in the morning! :)

Jim Strieter07:07:09

What is the best way to get the git hash inside Clojure? Motivation is that I have a database that records output from my program, and it would be really nice if Clojure could retrieve that hash from OS, and then store in database.

delaguardo07:07:45 there is a provided namespace to do system call.


but it depends how you run your application. from prebuild jar? or you checkout the repository and run something similar to lein run?


One way is to include this information in a text file when you generate the final artifact that's being run (you can simply use git via make for example). Or you can add it to MANIFEST.MF, leveraging - example for leiningen's :manifest

:manifest {"git-commit-sha" ~(fn [_project] (:out ( "git" "rev-list" "head" "-1")))}


for a pure java solution you can use jgit (there is a clojure wrapper too)


you can do the sh call in a macro to have the hash embedded in to build


or copy .git/HEAD - its just a file containing the head (although if you aren't in detached head state, it might just give you the branch name, which could still be valuable) - slurp .git/HEAD in a macro, no need to shell out or have git on the build system

Jim Strieter05:07:58

I ended up doing this:

(defn get-git-hash [] (subs (:out (sh "git" "log")) 7 47))
Works nicely

Jim Strieter05:07:24

I forgot to mention that the solution needs to work at runtime. Every time lein run is called, I want the program to get the latest hash so that hash can be associated with program output in the database. Works nicely.

Jim Strieter05:07:30

Thanks everybody!


Since nil is falsey, (if (:x {}) ...) is sufficient, or is that bad style?


depends on what you are check for. are you check if the map has a key or if the value if truthy?


If it has a key, where I expect the value to be a string, not nil or false.


is your {} a stand-in for a real map?


in general this is fine. But when there are more constraints it is better to explicitly check using predicate: (if (string? (:x {,,,})) ,,,)


if so, it is absolutely common/idiomatic to do

(if (:x a-map)
(if-let [x (:x a-map)] ...)


> is your {} a stand-in for a real map? Yes.


Okay, thank you 🙏


I get this error when using my macro in cljs

[Figwheel:SEVERE] clojure.lang.ExceptionInfo: failed compiling constant: rc.core$defc$fn__9159$update__9160@3552a96e; rc.core$defc$fn__9159$update__9160 is not a valid ClojureScript constant.
works ok when I macroexpand it on the clojure side, but not in cljs
(defmacro defc
  [name args body]
  `(defn ~name
        (fn [n]
          (if (and (vector? n)
                   (-> n
            (if (some #{(first args)} n)
              (let [update (fn [v]
                             (transform (mapv #(if (= % (first args)) v %) n)))]
                (conj n {:update update}))
(prn (macroexpand '(defc simple [x] [:Simple [:div "hi" x]])))


you are emitting a function object from the macro, instead of code that evaluates to the function object


clojure will in some cases allow you to get away with this, but it is bad practice, and clojurescript cannot get away with it because macros are written in clojure and expanded away and then the compiled code is run in a different runtime so they cannot share objects like that


Unfortunately I am not sure how to fix: what would it look like if the snippet correctly had code that evaluated to the function?


you can see update is bound to a function, and then that function is being cons'ed into the expression that is returned from the macro


update should be bound to an expression that when evaluated results in a function


This is bending my brain a bit 😅 I can't figure out what an expression that evaluates to a function would look like. The only thing that comes to mind is wrapping it inside another function, but that would just be another function object…


update does not really need to be a closure in this instance - if I make it a separate defn and move it outside of the macro, would that work?


hmm, it didn't really work, but quoting the function seems to work :thinking_face:


if you type (fn []) into the repl vs. '(fn []) what is the difference


Hmm, I see, one is a function and the other is code that will evaluate to a function


Does that mean I need to (eval) the function before I try to use it on the other side?


what are writing is a macro


the code it emits will be evaluated


like, what is the difference between (defmacro m [] (fn [])) and (defmacro m [] '(fn []))


Ok old security consultant here, trying to keep my mind supple! Last year I delved into latest Object Oriented design and programming but after much research still feel the Object wrappers causes much difficulty in large productive software systems. Cue hook into Clojure immutability benefits (and security design spin offs).


I think OOP/OOD and immutability are orthogonal concerns, you could make an immutable system that keeps logical structures using OOP vs internal mutable state, it just happens that idiomatic clojure stresses using plain old maps/data structures, vs a traditional OOP hierarchy


10 functions that apply to 10 data structures, or 100 functions that apply to 1 data structure (or whatever that quote was) - either way, your complexity space is 100 - biggest design win imo is just keeping systems as small as possible/logically separated (so, microservices in web space, piped CLI utilities vs option heavy in console space, and event driven/separation of GUI front ends/games and the back end processing to handle the events)


It's true that you can avoid immutability in OO languages (to varying degrees depending on the language) but it's a lot more work than Clojure's approach -- mostly because Clojure focuses on "plain ol' data" and separating data and functionality. I'll be interested to see what @bdstaniforth is actually going to ask about OO vs FP regarding security... 🙂

😁 1

Ref Alan Perlis Epigram #9 and the rest because most of them are timeless:


The elevator pitch for OOP is modeling real world objects in code. However, most of those distinctions are arbitrary and we lose information about them and their purpose over time. Object wrappers create high cognitive overhead over time and space. Cue design patterns that try to name objects after the functions they provide. Add in 20 years of Spring and you find classes like Then we ask the question, “but how do our objects behave?”


Part 2, however I feel Clojure error returns still leaves much conjecture as to the language cause of the error, which together with the ())())) issues slows understanding. Notwithstanding this there are clearly some fantastic benefits. After a few weeks learning here is a prime number clojure version I created based on the description of Sieve of Eratosthenes by Dustin Campbell and Melissa E. O'Neill. I wonder about my breakout of fn Sumkey and repetition of recursive call back to erato. Perhaps it shows some non functional background - C TAL Java? Any constructive critique is welcome.

(defn sumkey                                                                   │;   returns the result of applying f to the first 2 items in coll, then
   91   [sie sumks vfact cand]                                                       │;   applying f to that result and the 3rd item, etc. If coll contains no
   92   (if (seq sumks);(not (seq sumks)); (18) [3] 15 (rest sumks)))                │;   items, f must accept no arguments as well, and reduce returns the
   93     (recur (assoc sie (first sumks)                                            │;   result of calling f with no arguments.  If coll has only 1 item, it
   94                    (cons (first vfact) (sie (first sumks)) ))                  │;   is returned and f is not called.  If val is supplied, returns the
   95            (next sumks) (next vfact) cand)                                     │;   result of applying f to val and the first item in coll, then
   96     (dissoc sie cand)))                                                        │;   applying f to that result and the 2nd item, etc. If coll contains no
   97                                                                                │;   items, returns val and f is not called.
   98 (defn erato                                                                    │; --------------------------------------------------------------------------------
   99   "is cand key in table: no- add it keyed by cand'sqr, add to primes           │; doc (word): assoc
  100   : yes- add cand to each val(factor) if sum is key add val to key's facts     │; -------------------------
  101     else enter key sum val(factor) for each sum & val                          │; clojure.core/assoc
  102     remove orig foun


Hard to read that code with the long lines and so much text. Is that in a GitHub repo you can link us to?


(also the code doesn't seem complete)


I know it's sort of the clojure way (esoteric abbreviations for things that those "in the know" may read without issue), but I would make it "noob friendly" and rename sie to sieve, and cand to candidate (as is, cand looks like cond, and could be mistaken as such, or as a typo) - theres a balance between overly verbose java enterprise bean names, and apl/j-lang terseness - I would extend this same critique to some of the built in functions in clojure though


Also, the line numbers and the | characters mean we can't even copy'n'paste this into our own editors to try it out.


re: sie -- I read it as German "they"... I didn't even make the connection with sieve -- good point @U96DD8U80!


Yes sorry that was another issue fixing up neovim and conjure to allow decent cut and paste was awkward, sorry.


It's much easier to get code review feedback if you have a GitHub repo that folks can look at -- copy'n'paste into Slack is problematic for many reasons (imagine being on a phone and someone pastes a code listing into a channel you're trying to read).


And don't worry -- there are some old 'uns here too, like me for example (turned 60 just a few weeks ago) 🙂


(defn erato │; f should be a function of 2 arguments. If val is not supplied, 99 "is cand key in table: no- add it keyed by cand'sqr, add to primes │; returns the result of applying f to the first 2 items in coll, then 100 : yes- add cand to each val(factor) if sum is key add val to key's facts │; applying f to that result and the 3rd item, etc. If coll contains no 101 else enter key sum val(factor) for each sum & val │; items, f must accept no arguments as well, and reduce returns the 102 remove orig found cand entry" │; result of calling f with no arguments. If coll has only 1 item, it 103 ([] (erato {} 2'() )) │; is returned and f is not called. If val is supplied, returns the 104 ([sie cand facts ];prime-nums] │; result of applying f to val and the first item in coll, then 105 (if-let [facts ( sie cand)] │; applying f to that result and the 2nd item, etc. If coll contains no 106 (erato │; items, returns val and f is not called. 107 (sumkey sie (map (partial + cand) facts) facts cand) │; -------------------------------------------------------------------------------- 108 (inc cand) facts) │; doc (word): assoc 109 (lazy-seq (cons cand │; ------------------------- 110 (erato │; clojure.core/assoc 111 (assoc sie (* cand cand) (list cand)) │; ([map key val] [map key val & kvs]) 112 (inc cand) │; assoc[iate]. When applied to a map, returns a new map of the 113 facts)))) )) │; same (hashed/sorted) type, that contains the mapping of key(s) to 114 │; val(s). When applied to a vector, returns a new vector that 115 (= (drop 1(take 156 primes)) (take 155 (erato))


@bdstaniforth That's really just not readable in Slack. Can you put this up on GitHub or somewhere and provide a link?


That will give us a better chance of helping you.


Yes sorry saw the code option in slack entry and assumed cut and paste with that would be good from neovim clearly not data centric like clojure! I have a github account so Ill look at that put it onGit sorry to be overambitious for Slack thnx for trying.


Main bit is fn Sumkey and erato rest is simple divisors implementation of primes...


The first thing I notice is that you are passing facts into erato but then shadowing it with a local binding here


Security wise the immutabilty aspects were attractive after working on OO based systems and SPARC and PROLOG tree state theorem checking. Yes that is related to my concern that erato uses that to fork the recursive call back on erato I was wondering if if I could make the difference a variable into a common recursive call but felt that was where my imperative history was leading my a'way?!


In terms of readability and understandability, naming and non-idiomatic layout are probably your two biggest issues right now. I'd have to refresh my old memory on how the sieve algorithm is meant to work in order to see how well the code matches it -- because I can't tell what the code is really trying to do based on what I'm looking at right now.


Thanks Sean yes sorry legibility probably reflects my hesitancy adapting to functional here is a link to the description of the composite table form of the sieve I was trying to use


Here's an update with more idiomatic formatting and (I think) clearer names:


(that's also now structured into a project with deps.edn and the code in a src folder, so it can be used with the Clojure CLI and so more editors will know how to start a REPL in it -- I use VS Code / Calva, for example)


Grand thanks Sean, looks like Clojure now ! I'll have to digest it tomorrow ! But perhaps add-candidates shouldn't be conjoined into erato.


Here's how I'd probably implement the algorithm as described in that link:

(defn erato-filter
  ([] (erato-filter (iterate inc 2)))
   (let [prime (first unfiltered)]
     (cons prime
            (erato-filter (remove #(zero? (mod % prime)) (rest unfiltered))))))))

  (take 10 (erato-filter))


iterate is lazy. remove is lazy. So you start with all natural numbers from 2 on up. And at each step, you pull off the first number (it's prime) and the remove all of its multiples (lazily).


Wow the (remove anon fn is splendid looks like solution to my my concern that erato used the if-let to fork the recursive call back on erato in two versions; I was wondering how to make a common recursive call.


Yes and regarding the security and /or safety-critical aspects I wondering if current research had identified if Clojure immutable & namespace points aided validation / verification with regard to issues in generating pre-conditions, loop invariants ...?? Or perhaps it just moves the difficult aspects around!


@bdstaniforth Please use threads and don't post huge code listings. See comments in the threaded replies to your previous posts.


And don't feel shame for seeing this, I got the same heads up a few months back, as threads were not used in my prior slack-usage on different org slacks 😄 - but they really help with readability (main channel and contextual)