Fork me on GitHub

Hello, what is best way to slurp a file in resources for test? I understand I can do a (slurp (io/resources "dummy.edn")) but my issue is that I'm using lein-modules so depending on where i start the repl, (io/resources "dummy.edn") might not work in the repl. Do I need to use the second arity of io/resources and include a loader ?


io/resource lets you read a resource/file from the classpath, so as long as your files are on the classpath, that will work. So the solution is: start your REPL somewhere that has the classpath you need.


If you start a REPL in the wrong directory, you will have an incorrect classpath.


I have a directory/project that is for starting the repl in (like a dev folder), where I load the other modules as source-paths . Is there a way I can do a slurp that doesn't involve using the full path, but still allow me to this code to work in the repl?


All the resource files from your classpath will be available. You could try listing the resources available in io/resource and you'll see what's there:

(-> (io/resource ".")


If it's something that should work long term/in prod you should figure out the class paths so that the code that uses the file has access to it. If you only need it for a while you can use plain slurp from your own filesystem


with an absolute path


That didn't really list out the resources but it did help me find out what's my classpath


Is there a way I can include more directories in the class path? Or change the classpath of the repl? Am trying to think of the best ways to work around this, given how my project is set up


apparently i can using cemerick.pomegranate/add-classpath


I have a scenario where i want to fetch a value, which is updated outside of the system itself. If this value is "In-progress" i will need to repeat fetching and checking the value until the value has changed to another status, maybe even with some delay. Is there a common way to approach this problem in clojure?


@U01GQRC8W30 It probably depends on what you want to do with the value once you've fetched it. You could use a promise so that the caller can block until the fetch returns a non "in-progress" value by using deref on the returned promise

(let [p (promise)]
      (loop []
        (let [v (fetch-value)]
          (if-not (= v "in-progress")
            (deliver p v)
Alternatively, if you don't want the caller to be blocked, the fetch function could be passed a callback to run on the fetched value once it returns a non "in-progress" value.
(defn fetch-and-doit [doit-fn]
    (loop []
      (let [v (fetch-value)]
        (if-not (= v "in-progress")
           (doit-fn v)

🙏 1
🙌 1

There's also libraries which help with this sort of thing. I like


I'd like to add a docstring to a memoized function, presenting the appropriate times to use it vs the non memoized version. If I only do: (def f-mem (memoize f)) then there's no docstring. If I do: (defn f-mem "docstring" [] (memoize f)) there's a docstring but not the correct argument list Is there a nice way to do this?


(def f-mem "docstring" (memoize f)) ?


(def ^{:doc (-> #'f meta :doc)} f-mem
  (memoize f))

👍 1

docstring is just a metadata attached to the var


I thought you wanted a different docstring for the memoized version but yeah if you want to reuse the one of the original function you have to do what @U04V4KLKC suggested. Btw, in your defn example, f-mem is a 0-arity function, so you have to call it in order to get the memoized version of f, that's why (f-mem x) doesn't work but ((f-mem) x) does. (memoization won't work with the latter though - you'll get a different memoized function every time you call it)


Adding to what has already been suggested, if you want to annotate the arglist of the memoized version you can add the arglists metadata

^{:arglists '([arg1 arg2])}


Ah interesting, I'm giving it a shot! due to the length of the comment I also tried the with-meta , and when I looked at the source code I eventually went with that style of metadata:

 ^{:arglists '([^clojure.lang.IObj obj m])
   :doc "Returns an object of the same type and value as obj, with
    map m as its metadata."
   :added "1.0"
   :static true}
 with-meta (fn ^:static with-meta [^clojure.lang.IObj x m]
             (. x (withMeta m))))


A always, much thanks to you all 🙂


def'ing a memoized function is often a bad idea


memoize doesn't provide any control over what it saves, and that just grows and grows, and a def is a global

👍 1
🙏 1
Sam Ritchie21:12:49

Mandatory plug for if you do need to do this


Thanks I'll think about this an extra time 🙂


How can I add lazyseq of maps from database into #{} ?


a set of maps sounds kinda strange to me. But presumably you can just do (into #{} coll) and have the result you want


I want to only add a particular key value into #{}.


can you type out some representative data?


data in form of lazyseq is [{:key1 "value1" :key2 "value2"} {:key1 "value3" :key2 "value4}] I want to do into #{} only key1 values


I am trying to do (into #{} (mapcat #(get % :key1) data))


But it is not working


(into #{} (map :key) coll)


(into #{} (map :key1) [{:key1 "value1" :key2 "value2"} {:key1 "value3" :key2 "value4"}])
#{"value1" "value3"}


THis is not working


Is there a common idiom for: you've got a map shape and a function foo that processes it, but you want to use the same function name for a collection of those maps?


I was thinking multimethod


but map is a coll? so that's not the predicate


you typically don't create functions that operate on a collection of the same item since it's usually the same number of characters and less general than (map foo my-maps)


yeah, that's a good argument


i was trying to avoid keeping track of when i had a single or a collection but, i guess that's a programmer job not a computer job


yea, that's a tempting pattern, but I've found that it usually just leads to unnecessary complexity and hard to find bugs


it's the same trap that you end up in any time you think you want to use flatten


(and I think he has one about "heisenparameters" which covers the case of a parameter which might be a collection or not...)


Oh, haha, yeah, he even links to it in the first line of the redundant map post!