Fork me on GitHub
Herman Nurlygayanov11:02:00

I would like to override a method on a reified type, but preserve all other methods. What is the simplest way to do that? For example: I have > (deftype Adapter [] > some.namespace/SomeProtocol > (type-name-get [this] (:Adapter)) > ...) > > (def adapter-instance (Adapter.)) And I want to get > (def modified-adapter-instance > (some-magic-fn adapter-instance type-name-get (fn [this] :ModifiedAdapter))) > > (.type-name-get adapter-instance) => :Adapter > (.type-name-get modified-adapter-instance) => :ModifiedAdapter


delegation. write them all out


so make an instance of Adapter, then reify an instance of the protocol


(def adapter (Adapter.))

(def modified-adapter-instance
  (reify some.namespace/SomeProtocol
    (type-name-get [_] 
      (string/upper-case (type-name-get adapter)))
    (other-method [_]
      (other-method adapter))
    (other-other-method [_]
      (other-other-method adapter))))


you don’t have to, but yeah you can


if you need to do this multiple times for whatever reason you can do the same trick as proxy and pass a map of functions


and do an ((or (:other-method funcs) other-method) this)

Richie15:02:19 > Note that `recur` is the only non-stack-consuming looping construct in Clojure. There is no tail-call optimization and the use of self-calls for looping of unknown bounds is discouraged. Is this saying that it's bad to use recur when I don't know how many times I'll recur? I don't think I understand the advice.

(defn my-fun
  (loop [[a & as] xs]
    (when a
      (println a)
      (recur as))))
Is the quote discouraging code like the above or is it talking about something else?


I read it differently. It says to use recur because Clojure does not have TCO, which many other functional languages do have. With TCO you can just "self-call" and the compiler will turn it into a tail-call to prevent stack frames from building up. recur should be safe.


You're right that it could be worded more intuitively


Self-call I take as meaning that you would e.g. write:

(defn my-fn [x]
  ;; do some things
  (my-fn (inc x)))


This does work in Clojure, but this comment is saying don't do it, use loop/recur instead


Yea, I think you’re right. The way I read it confused me. Thanks!


@U5JPZFFR6 not neccessarily loop (defn my-fn [x] (recur (inc x))) is also non-stack-consuming (though just as much of a silly loop)


Oh I didn't realize you could recur without loop, that's convenient. Honestly isn't that better than typing the fn name again? If you rename the fn, you only need to change the head, not the call, and its semantically more obvious that you're self-recursing. I suppose you can't multi-recurse but I've honestly never needed that.


the two reasons to do a direct self-call instead of recur are for lazy-seqs (the self call gets embedded in a thunk that is returned instead of making the stack deeper), or with the kind of branching recursion where the bookkeeping to turn it all into tail calls isn't worth the complexity


something else, calling my-fun from within my-fun (without using loop/recur)

Alex Miller (Clojure team)15:02:31

because a self call adds a stack frame, unbounded self calls == possible stack overflows. so use recur instead.


Oh I see, thanks! I think I wasn't reading the docs correctly.

Luis Angel Prado Postigo15:02:43

Hi, how i can make this work? (def [x] (map inc [1 2 3 4 5]))


Did you want to create a function with defn rather than def e.g. (defn [x] (map inc [1 2 3 4 5]))

R.A. Porter15:02:28

Or are you trying to set x to the mapped value, perhaps as (def x (map inc [1 2 3 4 5]) or, if you want it as a vector again, (def x (mapv inc [1 2 3 4 5]))?


or are you trying to do a “destructuring def”?


(def x (let [[x] (map inc [1 2 3 4 5])]


(no syntax for that one i’m afraid

Luis Angel Prado Postigo16:02:31

i want to set the first value of the list returned by map into x

Luis Angel Prado Postigo16:02:32

in that expression i want x= 2


(def x (inc (first [1 2 3 4 5])))

Luis Angel Prado Postigo16:02:51

@U11BV7MTK that works, but now i wondering if instead of taking the first i want to take 2, or maybe more


if you’re unsure, just set (def x (map inc [1 2 3 4 5])) and then wherever you use x can take as many values as you need


but without any kind of use-case or problem statement its hard to give more advice. And we’re in a bit of a contrived domain to begin with


If you have a few use cases we can think about those

Luis Angel Prado Postigo16:02:17

well i was trying to use destructuring here

(let [[current-child next-child] (readdir path-string)
      current-child-path (path/join path-string current-child)
       next-child-path (path/join path-string next-child)])

Luis Angel Prado Postigo16:02:48


[[current-child-path next-child-path] (map (partial path/join path-string) [current-child next-child])]


yeah - unfortunately not supported. I have wanted something like that though


there is an example in the community docs for this function of doing what you want


so you can copy paste that macro or adjust it to your needs

Luis Angel Prado Postigo16:02:25

i didn't know that you can do destructuring in def,

(def [a] ["a"]) ;this doesn't work


you can’t, but you can write a macro that does

(def+ [a] ["a'])

Luis Angel Prado Postigo16:02:30

i see, i was wrong, but i kind of surprised that this work

(let [[a b] (map inc [1,2]) ] (prn a b))

Luis Angel Prado Postigo16:02:44

btw thanks for the macro


@U0322TLEJFK the generic term for what let does is a "binding block" - the same syntax of destructuring works in fn, for , doseq , loop etc., but on the other hand def isn't a binding block, it's much simpler

Rob Haisfield17:02:05

My project.clj file has [babashka/fs "0.1.2"] , but when I run lein deps :tree it shows that I’m using [babashka/fs “0.0.5"]. Why would that be happening? How can I update it to force it to use the most recent version? Right now I’m using Calva

Rob Haisfield17:02:35

My project.clj:

(defproject functions-for-processing-notes "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url ""}
  :dependencies [[org.clojure/clojure "1.10.3"]
                 [dk.ative/docjure "1.14.0"]
                 [com.rpl/specter "1.1.3"]
                 [babashka/fs "0.1.2"]
                 [orchestra "2021.01.01-1"]
                 [lambdaisland/deep-diff2 "2.0.108"]
                 [org.clojars.arthurbarroso/lovelace "0.1.2"]]

  :repl-options {:init-ns functions-for-processing-notes.core})


Do you have ~/.lein/profiles.clj?

Rob Haisfield17:02:27

Nope, nothing in there


what is the tree output?

Rob Haisfield17:02:13

Output of lein deps :tree

[babashka/fs "0.0.5"]
 [clojure-complete "0.2.5" :exclusions [[org.clojure/clojure]]]
 [com.rpl/specter "1.1.3"]
   [riddley "0.1.12"]
 [dk.ative/docjure "1.14.0"]
   [org.apache.poi/poi-ooxml "4.1.0"]
     [com.github.virtuald/curvesapi "1.06"]
     [org.apache.commons/commons-compress "1.18"]
     [org.apache.poi/poi-ooxml-schemas "4.1.0"]
       [org.apache.xmlbeans/xmlbeans "3.1.0"]
   [org.apache.poi/poi "4.1.0"]
     [commons-codec "1.12"]
     [org.apache.commons/commons-collections4 "4.3"]
     [org.apache.commons/commons-math3 "3.6.1"]
 [lambdaisland/deep-diff2 "2.0.108"]
   [fipp "0.6.23"]
   [lambdaisland/clj-diff "1.1.58"]
   [mvxcvi/arrangement "1.2.1"]
   [org.clojure/core.rrb-vector "0.1.1"]
 [nrepl "0.8.3" :exclusions [[org.clojure/clojure]]]
 [orchestra "2021.01.01-1"]
 [org.clojars.arthurbarroso/lovelace "0.1.2"]
   [cheshire "5.10.0"]
     [com.fasterxml.jackson.core/jackson-core "2.10.2"]
     [com.fasterxml.jackson.dataformat/jackson-dataformat-cbor "2.10.2"]
     [com.fasterxml.jackson.dataformat/jackson-dataformat-smile "2.10.2"]
     [tigris "0.1.2"]
   [clj-http "3.10.0"]
     [commons-io "2.6" :exclusions [[org.clojure/clojure]]]
     [org.apache.httpcomponents/httpasyncclient "4.1.4" :exclusions [[org.clojure/clojure]]]
       [org.apache.httpcomponents/httpcore-nio "4.4.10"]
     [org.apache.httpcomponents/httpclient-cache "4.5.8" :exclusions [[org.clojure/clojure]]]
     [org.apache.httpcomponents/httpclient "4.5.8" :exclusions [[org.clojure/clojure]]]
       [commons-logging "1.2"]
     [org.apache.httpcomponents/httpcore "4.4.11" :exclusions [[org.clojure/clojure]]]
     [org.apache.httpcomponents/httpmime "4.5.8" :exclusions [[org.clojure/clojure]]]
     [potemkin "0.4.5" :exclusions [[org.clojure/clojure]]]
       [clj-tuple "0.2.2"]
     [slingshot "0.12.2" :exclusions [[org.clojure/clojure]]]
 [org.clojure/clojure "1.10.3"]
   [org.clojure/core.specs.alpha "0.2.56"]
   [org.clojure/spec.alpha "0.2.194"]


My output:

$ lein deps :tree
 [babashka/fs "0.1.2"]
 [clojure-complete "0.2.5" :exclusions [[org.clojure/clojure]]]
 [com.rpl/specter "1.1.3"]
   [riddley "0.1.12"]


Maybe try purging ~/.m2/repository/babashka/fs


It would surprise me if that helped, but you never know


are you sure you saved the changes to project.clj?

Rob Haisfield17:02:36

I saved the project.clj and rebooted the REPL and it didn’t work at first, but then I rebooted VS Code and it did! Thank you for helping me with this.


Afk but I see restarting VS Code will likely explain/be solution to why my project deps.edn did not override my user deps.edn, i.e. I had similar problem newer version of dependency in project deps.edn not overriding version in user deps.edn. Edit: oops, when that happened I was not using VS Code, but clj from the terminal. I noticed the anomaly to follow up some arbitrary time in the future.


Hey, I use a macro to generate a core.logic expression. A part of that is a very large "db", in this case it's just a big in memory data structure. I wonder is there a way to reference the data by a name/reference (when I generate an intermediate value inside a let clause)?

(defmacro large-ds [ds]
  (let [intermediate-val (mapv identity ds)]
    `(map identity ~intermediate-val)))

(macroexpand-1 '(large-ds ["very" "big" "vector"]))
=> (clojure.core/map clojure.core/identity ["very" "big" "vector"])

;; and what I would like to achieve

(macroexpand-1 '(large-ds ["very" "big" "vector"]))
=> (clojure.core/map clojure.core/identity reference-to-intermediate-val)


passing large values through the compiler is generally bad


the core.logic dsl is mostly macros, but it also has functions for most things so you construct goals at runtime using those instead of having to rely so heavily on macros


Thank you! I guess I should hide access to the data behind function calls then?


I have some input data and compile it to a core.logic expression. And I have some custom data structures like described here


> passing large values through the compiler is generally bad I don't know how to mitigate this, since I have to work with the data :thinking_face:


the blog post in that link doesn't pass large data through the compiler and doesn't create any macros


so do what it does


Okay simple. I don't know why but I tried to write "pure" functions