Fork me on GitHub
peter hull07:04:49

What's the neatest way to look for keys in a hashmap in order, running a particular function on the first one found. e.g. I've got a thing with an optional :display-name key, optional :id key and further keys. If it has a display name I want to format that, otherwise if it has an id I want to format that in a different way, otherwise ... and so on. I can use a nested if-let but it's a bit messy and condp doesn't quite work.


Plain cond should do it, no?


(defn sel [x]
    (:a x) (f x)
    (:b x) (g x)
    :else "nothing"))

peter hull08:04:13

OK, but ideally I'd want f to take an arg of whatever (:a x) is


edited ๐Ÿ™‚

peter hull08:04:55

 (:a x) (f (:a x))
 (:b x) (g (:b x))
 :else "nothing")

peter hull08:04:02

without repeating the get

peter hull08:04:21

obviously that's fine but I always suspect there's a neater way in clojure.


There have been discussions around better-cond some time ago, it seems a lot of Clojure shops are building some flavor of their own


This is a nice exercise for writing your own macro, I think. Mostly to expand something like

(foo x
  :a f
  :b g
to that cond above ๐Ÿ™‚


Now, coming up with a name for that foo is the truly hard part.

Steven Katz19:04:36

Nested (if-let)

peter hull08:04:32

better-cond might do it, looks maybe a sledgehammer to crack a nut! I'll have a go at a macro, it's good practice anyway

peter hull08:04:40

Thanks orestis

๐Ÿ™ 4

Yeah I get it โ€” but sometimes adding a library like that to your dependencies early on saves you for having to judge cost/benefit for every little bit tiny use case.


If you have it, might as well use it in many places, I mean.

Roberto Rodriguez Jr08:04:51

hey, looking to make friends. dont have any

๐Ÿ˜‚ 4

Hey, does anyone have any other video/blogs that talk about Clojure at scale? like this or Rich Hickey's Language of the System.

๐Ÿ‘ 4
Frank Henard14:04:03

Good question. I would also like to hear more about this. The most difficult thing for me to figure out as my Clojure program grows and grows in complexity, is how to structure it, so it's easier to come back to. Blog posts, videos, and books (I would pay) would be welcome!


In terms of code-size scale, we're at 82,000 lines these days and we're sort of constantly evolving how we organize that much code.


We have a monorepo with about 30 subprojects now. All managed with clj and a bunch of deps.edn files (we have a "base" subproject that has the overall deps.edn file, and that is where our CLJ_CONFIG var points).

Ivan Koz10:04:38

what am i missing here?

(match '("M" "125.4,69.7" "rest1" "rest2") 
       [(["M" x & rest] :seq)] x
       :else :no-match)
=> :no-match


(match '("M" "125.4,69.7" "rest1" "rest2") 
         (["M" x & rest] :seq) x
         :else :no-match)


since the first pattern is [...] you'll want the value to be, say, ['("M" "125.4,69.7" "rest1" "rest2")]


or as @U04V4KLKC says ๐Ÿ˜Ž

Ivan Koz10:04:07

oh thanks, that was simple enough


Hi , my compjoure route is (GET "/foo/:a/:b/:c" [a b c] (some-controller/controller-function a b c)) it is not working when my url is like localhost:8080/foo/1-2-3.html#/foo/bar


the # introduces a URI fragment, which isn't part of the path component of the URI. compojure is only destructuring the path component, which is /foo/1-2-3.html (no :b or :c available)


for example one that should work would be: localhost:8080/foo/1-2-3/bar/baz


if youโ€™re trying to send a url through a url (ergo โ€œ`1-2-3.html#`โ€œ) youโ€™re going to have a hard time.


actually i m converting existing site into clojure ....and trying to use as it is url from existing one .... so in compojure i couldn't use # in between url


ye, makes sense. Good luck.


I'm struggling to get spec/fdef to fire anything useful.

(require '[clojure.spec.alpha :as s])
(s/def ::name string?)
(s/def ::score int?)
(defn generate-new-player
  {::name name
   ::score 0})

(s/fdef generate-new-player
        :args (s/cat :name ::name))

(s/explain ::name 42)
; => 42 - failed: string? spec: :user/name
(generate-new-player 42)
; => {:name 42, :score 0}
Edited: because I suck at remembering alt-enter


were you expecting some kind of exception/warning for (generate-new-player 42)? keep in mind specs don't apply at runtime


Ahh...I was. I just found that in the testing that only for testing then?


you'll have to instrument the function to throw exceptions for specs



arbscht13:04:57 lists all the ways specs can be useful


Ahh! That's a great link.


So I could use instrument for generate-new-player or add a if (s/valid? ::name name) ( stuff...) (throw...)


could abbreviate the if/throw to assert


but that's the general idea with spec


Quick follow up question. I was planning on spec-ing some large data structures my applications was going to pass around and checking it in and out of of the data functions. Is that a terrible idea?


not at all, depends on the nature of the data and fns but I don't see why not


Is the best way to instrument all those functions? Or add those asserts?


also depends on your fns and program. add asserts if you have some kind of error handling strategy. instrument in unit testing either way


or just instrument at the repl if you're not automating tests


Just getting started with Clojure, so unit tests are being planned and coming. I'll take a look. I was just trying to make sure that the name had to adding some error handling there seems to be useful.


Thank you for the help. Its nice having ya'll around as I learn in a vacuum.


I'd go with asserts then, which will force you to build up the error handling aspect of the program ๐Ÿ™‚


I notice there is a spec.alpha/assert that something I should look at?

๐Ÿ‘ 4

it's useful since you can toggle the assert check


That seems useful.


the main reason specs don't apply at runtime by default is not to penalize perf for correct programs, so toggle-able asserts is one way to give more flexibility


but if you're not worried about perf (yet), you can stick with ordinary assert or valid?/conform checks too


I'll do the s/assert so I can toggle later in life. I had kinda planned on a user entering name...that's where the hard assert was coming from.


so if that's a hard requirement then you might not want to be toggling it ever. I'd actually consider opting out of s/assert for those cases


but experiment and see what works best for you, I don't know your program ๐Ÿ˜Ž


nice thing is there are many options

Frank Henard14:04:03

Good question. I would also like to hear more about this. The most difficult thing for me to figure out as my Clojure program grows and grows in complexity, is how to structure it, so it's easier to come back to. Blog posts, videos, and books (I would pay) would be welcome!

Scott Starkey14:04:31

Hi there, dear Clojurians: Can you tell me how is next different than rest?

Scott Starkey14:04:36

Duh - I should have just typed the query into google! :-?


I said to myself, "Duh...I know this" then ran (next [1 2 3]) and (rest [1 2 3]) and they were the same...time to google this for me too!

Scott Starkey14:04:08

Apparently, what Iโ€™m able to gather, doing a next on an empty sequence returns nil. Doing a rest on an empty sequence returns ( ).

Scott Starkey14:04:42

This would be important if youโ€™re wanting to do a exit condition on a empty collection.


Yeah...`rest` is one of the primary seq workhorses according to So rest is more sequence based and lazier than next is my understanding.

Ivan Koz14:04:20

as i understand it, next will evaluate element of lazy sequence to check if it has any value while rest just returns next LazySeq which maybe empty


That seems right to me.


Style question. When destructuring complex data structures, my linter (`joker`) gives a warning for unused binding when I use the :as <complex data structure>, but to me (and examples I've seen) it seems good practice to include that to let folks know what data structure the function is looking for.



(defn index-from-name
  [{:keys [::players] :as game} name]
Where a game map is given but never used, I really only want the players from within the game map.


I guess I never actually asked a question. Question: what is the preferred style? I don't see anything in the style guide.


I found that joker ignores the warning if you precede the binding name with an underscore: eg. _game


At least in Haskell / ML languages it's a common practice to denote unused bindings that way, not sure if that's the case here in Clojure


Style guide does say to use _ for unused variables, so that seems like a good compromise.


alternatively, this check can be turned off by setting unused-as to false:

๐Ÿ‘ 4

Is passing function calls as arguments bad practice?


it depends on how much you like reading Lisp ๐Ÿ˜„ Another option (as well as let) is threading macros


can you put an example?


(fn1 some-arg (fn2 args)) where fn2 returns some data


if you were to let bind all of those it would get ugly. this is a "salt to taste" situation.


sometimes makes sense sometimes not


an easy example to see is in arithmetic. (+ (+ 2 3) (* 3 4) (/ 9 3)) would look silly if each of the intermediate steps was in a let binding


good example, yeah I'll put more common sense to it ๐Ÿ˜‰


its a good question. but the answer is to just make it legible

โœ… 8
Jp Soares19:04:04

Would be a problem to create tests outside the test/ folder in a lein project? I guess is not a problem in clojurescript as I define what's the entry point and there's the closure compiler for not used functions, but I don't know if similar things happen in the JVM.


the test/ folder is just a classpath entry that is automatically used by leiningen (and other tools) when you ask it to run tests, any other directory can be used if you properly make your tool use it when running tests


there's a leiningen config for folders to include when running tests

;;; Filesystem Paths
  ;; If you'd rather use a different directory structure, you can set these.
  ;; Paths that contain "inputs" are string vectors, "outputs" are strings.
  :source-paths ["src" "src/main/clojure"]
  :java-source-paths ["src/main/java"]  ; Java source is stored separately.
  :test-paths ["test" "src/test/clojure"]
  :resource-paths ["src/main/resource"] ; Non-code files included in classpath/jar.


(from lein help sample)


Any number to currency formatters out there for clojure?

Alex Miller (Clojure team)19:04:46

Java has a whole internationalized system for this in the jdk

Alex Miller (Clojure team)19:04:05

So look up how to do it in Java and use interop

Jp Soares19:04:47

:tests-paths seems to work


I'm having trouble getting connected to ms sql server:

Caused by: java.lang.Exception: Unsupported URI:  please, submit an issue request and we'll try to add it. Pull requests also welcome


the docs here: make it look correct. i'm just wondering if this URI really isn't supported.


@bill.h.tucker I suspect that wikibooks page is horribly out of date...


Oh gosh, yes, it's completely different to how you use that library these days ๐Ÿ˜ž


What version of are you trying to use?


More up to date documentation is here (and that is linked to from the GitHub repo for the project )


You're better off using the :dbtype hash map form of db-spec -- it's the preferred way and it's much better supported that raw URI strings.


for your case it would be

{:dbtype "sqlserver" :dbname "DDDDD" :host "HOSTNAME" :user "UUUU" :password "XXXXX"}


Assuming you have a modern version of (0.7.9 is current) and you also have the Microsoft SQL Server JDBC driver in your dependencies.


In project.clj, that would be [ "6.2.2.jre8"]


Also, feel free to ask any detailed questions about or database stuff in #sql (it's lower traffic and I'm more likely to see it there) @bill.h.tucker


Ah, at least it looks like someone updated that wikibooks page to warn people the docs are out of date For the latest, most up-to-date community-managed documentation for the library, consult Using java.jdbc on Clojure Documentation. This WikiBooks page is written around a very old version of the library and most of the examples here will not work with newer versions. ๐Ÿ™‚


thanks for your help. I've been struggling with this for weeks.

voortuck23:04:39 and on.


Sorry to hear you've been struggling, especially for weeks ๐Ÿ˜ž


If it's not obvious, I'm the maintainer of (and the upcoming alternative for it, next.jdbc), and I test it against ten databases, including MS SQL Server although my heaviest use of it is for MySQL (Percona 5.7) at work.


thanks for your help.