Fork me on GitHub

so i notice that my code has this pattern around quite a bit

(if (some? x)
 (do-stuff x)
what's the idiomatic way to address this more elegantly ? am i simply oblivious to a function that exists for this ?


Your else case always returns nil here I guess, so there's that.


yeah i think i gave a bit wrong example

dominicm08:12:53 This is useful if there's an expression involved


i think this better illustrates the pattern:

(if (condition x)
  (do-stuff x)


i ended up rewriting this as

(cond-> x
 (condition x)


Yeah, I find myself doing that a lot, don't like it either.


There's a macro by a JUXTer called condas-> which solves this a little I think.


I actually just wrote this code for a coercer.


i guess

(some-> x do-stuff)
does the trick ?


some? returns true if its argument isn't nil … depends on whether you really care about nil or logical false. If logical false is ok:

(when x
  (do-stuff x))
also works


it will return nil in the case where the test doesn't return logical true, so be aware of that ...


Struggling to get clj / tools.deps to work, getting: Error building classpath. Unknown coordinate type for org.clojure/clojure: {:type :mvn, :version "1.8.0"} Anyone know why that could be?


@martinklepsch what's your command line?


and your deps.edn?


@pesterhazy just clojure or clj. I do have a deps.edn which contains this:

 {clj-time {:mvn/version “0.14.2”}}}


does it work from an empty directory?


(which is copied from the guide)


same error in empty directory


is it the latest version? (`brew upgrade clojure`)


I did reinstall / make sure it’s up to date


what's the output of clojure -Sverbose


(also which clojure)


check the files in

config_paths = /usr/local/Cellar/clojure/ /Users/me/.clojure/deps.edn deps.edn


you may need to delete ~/.clojure


that was it, thanks!




I looked for the user config but didn find it (thought I never created one so probably just didn’t look hard enough)


Would be nice if the error indicated which file the error comes from


The config is an overlay of the 3 edn files laid on top of each other, so this might be difficult


But maybe it could say "Error: ... - the config files used were x, y and z"


yeah I guess that would already be a good improvement


@pesterhazy do you know if the clj command does any preprocessing of the deps.edn file?


(tools-deps/resolve-deps (read-string (slurp "deps.edn")) {})
trying this but getting Unknown coordinate type for clj-time: #:mvn{:version “0.14.2”}


just running clj works fine


is it because of the overlay effect I mentioned?


Try using this instead:

(->> "/usr/local/Cellar/clojure/ /Users/me/.clojure/deps.edn deps.edn" split-by-space (map slurp) (map read-string) (apply merge))




not sure how that explains the error message though


not sure then


FWIW all files seem to be using the same syntax as my local deps.edn


There's #tools-deps as well btw - Alex is hanging out there


(defn vec->map [v]
  (into {} (reverse  (for [[i k] (keep-indexed vector v)]
                    [k i]))))

(vec->map [:a :b :a :d])
 {:d 3, :a 0, :b 1})

is there a builtin for this/ it builds a map of "first of index" for each value



(vec->map [:a :b :c])
=> {:c 2, :b 1, :a 0}
(zipmap [:a :b :c] (range))
=> {:a 0, :b 1, :c 2}
but I assume I'm missing some aspect of the requirement here?


@mbjarland that will give you the last index instead of the first if you have duplicates in the array


ah, ok just saw the second :a


here’s my variation:

(reduce (fn [m [i k]] (if (get m k) m (assoc m k i))) {} (map-indexed vector [:a :b :a :d]))


you could still go zipmap:

(defn f[v]
    (zipmap (reverse v) (reverse (range 0 (count v)))))


that will be slow if you have large sequences though


how about:

(defn f[v]
    (zipmap (rseq v) (range (dec (count v)) -1 -1)))
? rseq is constant time, range is fast as it's bound and not explicitly reversed etc


a simplistic bench seems to put them more or less on equal footing, though it seems that rseq is picky and <edited>only lkes vectors and sorted maps</edited>:

(def my-v (mapv (comp keyword str) (range 1000000)))
=> #'user/my-v

(defn f[v]
    (zipmap (rseq v) (range (dec (count v)) -1 -1)))
=> #'user/f

(defn g [v]
  (reduce (fn [m [i k]]
            (if (get m k)
              (assoc m k i)))
          (map-indexed vector v)))
=> #'user/g

(take 2 (time (f my-v)))
"Elapsed time: 504.559267 msecs"
=> ([:821597 821597] [:323982 323982])

(take 2 (time (g my-v)))
"Elapsed time: 744.353442 msecs"
=> ([:821597 821597] [:323982 323982])


neat benchmark!


can you give it a shot with a transient map for the reduce version?


Might also consider using reduce-kv:

(reduce-kv assoc
           [4 3 2 1])


I am trying to extend a Java class (add a new method) and call a private method of that class inside it. I tried with proxy, I can override an existing method (not a new one) - but can’t seem to access this in the overridden code. Is gen-class my only option?


With this sort of OOP gook, it's often better to write your code in Java and then call that via Clojure.


Or use gen-class, but that's rather hard to get right


@nha if you go the java route, I highly suggest trying this lein plugin: it autoreloads Java classes when the .java files change. Makes playing with Java/Clojure interop a lot faster.


Appreciate the advice, thanks 😄 (yeah gen-class doesn’t look like the most welcoming bit of Clojure)


yeah stay away from gen-class


there's which would help in most cases where people now reach for proxy/gen-class


if you care, vote for it


it’s an option to define a protocol with the method you want to implement, then proxy the class and also that protocol


yes the real problem is access to this private methods (overriding an existing method is fine, performance is not a problem) I am actually considering a thin separate Java wrapper library now


I would probably have used CLJ-1255 if it was there already - so will upvote even though I can’t comment on the actual implementation (EDIT - will upvote once I find my login back)


Thanks all 😄


bronsa: do you think you could write that as a macro and release it as a library outside of core?


it's not worth it tho


I mean, the current impl reuses half


I really don't have it in me to reimplement it


(I must admit I eyed the patch to check if it was self-contained in a ns 😛 )


but yeah, I've thought about it


anyone have insight into this behavior? I am really at a loss here


=> (do (defrecord Foo []) Foo)
=> (identity (do (defrecord Bar []) Bar))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: Bar in this context, compiling:(null:1:10) 


it's commonly called the gilardi scenario


ahhh interesting. That's a very helpful link, thanks!


tl;dr: clojure's compilation unit is the top level form, the compiler explodes top level dos and treat them as N compilation units. From within a single compilation you don't see the effects of runtime operations on namespaces like require (in the case of that blog post) or import (in your case)


@tbaldridge: iirc, in one of your odin videos, you take an XML tree, flatten it into eav triples, and use a mini-logic language to query over it this seems to be like "collection oriented programming", in the spirit of SQL/APL do you know of other examples / tutorials of doing "collection oriented programming" ? (it s'like mapfilter/..., but more 'structured')


@qqq there's some stuff like that in core.logic.


And it's all fairly close to XSLT


@tbaldridge: 1. is XSLT ? (I'm not drawing the connection, and not familiar with XSLT) 2. I've been looking at APL + SQL a lot lately, and it seems possible to write general purpose (for some def) programs in those languages, and they have this notion of "everything is a tensor . everything is a table of rows" that is a bit stronger than clojure collections, but I can't quite understand it


seems like everyone gave up on xslt which is too bad


xslt is great


@qqq yeah, XSLT is a query/transformation language for XML written in XML ( 😐 )


the syntax is mindboggling, but you can do some very complex translations in an incredibly simple way


unfortunately, pretty much everyone left it behind 10 or 15 years ago


that's not too bad right? xml is basically (foo ...) -> <foo> ... </foo>


Exactly, convert all the XML to EDN and it wouldn't be half bad.


So those are the big "query arbitrary nested data" techs I'm aware of.


There's also JQuery and libs like Specter, but they differ a little in their lack of joins and multiple data sources.


I'm interested in using APL / SQL for things beyond 'querying databases', and then porting the style to Clojure. For example, things like dijkstras algorithm / topological sort can get implemented in a particular way in APL, as well as game of life.


And translating that over would get clojure code very different in style from 'common/idiomtic clojure style'


> Exactly, convert all the XML to EDN and it wouldn’t be half bad. Sort of funny in the use of the word “half” here


It’d be about half


(well better really, but close enough 😛 )


funny enough though, whenever I work with XML I really want something like XSLT in Clojure. Especially something where I can feed it 20 queries and an optimizer will compile it all down to a single fast gather function


sounds like a TODO for someone


Well, this might be too literal of an interpretation of "XSLT in Clojure", but 😛


>xslt is great Must be the first time I've ever heard anyone say that. Not quite everyone left it behind, though — it's still quite heavily used in the wonderful world of technical documentation and publishing.


love the headshot that goes with it


It's pretty obnoxious to see your own face like that, I always forget that happens when you paste a GitHub link here… sorry about that.


haha well it’s github’s fault really


> xslt is great I feel like this might have been more common to hear pre-json.


i seem to remember finding it to be neat back in college, but we’re talking early->mid 2000's.


Hi all: I'm working on a boot based web-app. The environment is set up in a way that allows me to develop and deploy it directly, but I would also like to be able to use it as a basis for derived apps that would extend the core tech. It seems there would be two approaches to this: 1) fork the repo, and use standard distributed git workflows to merge features between the core and derived apps 2) package the core app as a library to be consumed by other projects The first would seem to make it pretty trivial to merge features back and forth between the different forks, but would eliminate the distinction between upstream and downstream. The second would maintain a very clear distinction about where features are coming from to make a coherent system, but makes integrated development a little more cumbersome. I'm wondering if folks have had experience with deriving one app from another, and what approach they used to do integrated development (either the first or second approach above, or some third approach).


could use git submodules instead of forking


not everyone likes them though, but it’s a matter of taste


@jgh: Just curious, did the headshot disappear for you? Wondering whether what I did to remove it works for just me or everyone…

New To Clojure21:12:36

@U4ZDX466T How did you removed that picture? Wherever I paste Github link it always looks like that.


If you hover over the area where the picture is, an X icon appears to the left of the gray border. Click there and Slack asks you whether you want to remove the attachment.


yeah looks like whatever you did removed your profile image


@zalky for #2 i think the question becomes how often are the sdk and clients developed in tandem? If the workflow is very often going to be the client dev is also working on the core sdk to create their features then perhaps doing a lib distribution is not the most efficient way of doing least at the beginning. If your core sdk doesnt change much and may occasionally need updates maybe the extra overhead is something you can live with. Maybe it’ll also encourage more thoughtful api design since it’ll be a pain to work on the core sdk 😉


@jgh: thanks for the submodules rec, I haven't used them in a while, so I will have to do a bit more reading. In another channel, someone recommended the use of "checkout" dependencies, which I had not heard of, but also looks like an interesting alternative.


ah yeah i guess that’s a boot-specific thing


how can I format

to be
? I was trying like this:
(format "%.8f" 0.0115)


but throws an error all the time


hmm it seems to be working in repl 😕


@tbaldridge you seem to give a lot of value to logic programming. But I don't ever seem to find a practical use for it in my work, so I presume I'm missing something. Any hints?


Logic programming is a tool I use in what I call "code compression". As time goes on and a codebase grows I tend to see high level patterns that I would like to compress. For example, I may have a large XML structure that I'm parsing and I'm using tons of get-ins, reducing over the results and emitting them into some other format.


So at a basic level what I try to find are "code compression" algorithms that allow me to express the "what" without the "how". Often this goes beyond stuff like get-in or specter as I also need aggregation, advanced predicates, etc.


Most of the time what I end up using is an ad-hoc engine that does just enough to fulfill the needs of a given client, but I'd love to see something more advanced.


Once you have a DSL of sorts that expresses declaratively what you want to do, the next question is "how do I optimize the output of this engine". This also then allows me to separate the "what" from the "how to do it fast".


Instaparse follows this, you express the "how" in the EBNF language, and then the parser engine figures out how to make it fast. If in the future Instaparse figures out how to optimize some construct, I don't have to change my code, I just pull down their new lib.


The same thing could be done in a very simple way with something like get-in. Perhaps get-in could take many paths and then find common sub-paths in the arguments only traversing into children once per unique subpath.


I would presume this kind of thing can only come in later in a project then, after the patterns have emerged. That would explain why our experiences differ. I've spent more time in the greenfield part of a project, and very little time in the stage where you're extending existing code. We have a few places where we achieve something of a similar goal with data, e.g. defining schemas related to a graphql request, and allowing a small bit of code extract that. I think the scariest part of what you said was "[building] an ad-hoc engine", I don't even know where I'd start really. Reading through odin, I find terms like "unification" and "tabling" quite intimidating. I should perhaps purchase your series where you build odin to try and learn more about Karen and such.


Fwiw, I really like Odin so far. I'm trying to use it for querying REST apis. I've had to put a memoize in though (I want to replace this with a ctx cache), as Odin was "forking" (my term for the separate paths after doing an additional query) queries when I didn't expect it to, as there was no overlap in the inputs. I'm not sure if I'm using it properly, or even if I'm benefiting from it. I initially reached for datalog/datascript to do this, but being unable to make arbitrary rest queries made it difficult (having to commit to a db after doing part of the query I'm interested in probably doesn't give me much benefit.


I'm struggling now with something in odin, which is something to the effect of: - I've fetched all members from API - I've fetched all tasks from API - I want to find the assignee of a task - I want to find the owners of a task I seem to keep filtering all the members, instead of creating a list of members named "assignee". The closest way I came to achieving my goal was (o/and (= (:assignee_id ?task) (:id ?member)), which obviously doesn't involve any bindings. I wondered if I'd need to use o/== but I wasn't certain how I'd do that.


Maybe I should be "re-fetching" a list of members for assignees.