This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-04-26
Channels
- # aleph (5)
- # announcements (9)
- # beginners (115)
- # boot (36)
- # calva (13)
- # cider (4)
- # clara (7)
- # cljs-dev (27)
- # cljsrn (20)
- # clojure (182)
- # clojure-conj (3)
- # clojure-dev (4)
- # clojure-europe (3)
- # clojure-italy (2)
- # clojure-nl (4)
- # clojure-uk (34)
- # clojurebridge (3)
- # clojurescript (19)
- # clojureverse-ops (3)
- # core-typed (1)
- # cursive (12)
- # data-science (3)
- # datomic (16)
- # emacs (9)
- # events (5)
- # figwheel-main (11)
- # fulcro (14)
- # graphql (7)
- # jobs (10)
- # jobs-discuss (6)
- # lein-figwheel (8)
- # leiningen (2)
- # lumo (22)
- # mount (1)
- # nrepl (7)
- # off-topic (69)
- # overtone (17)
- # pathom (3)
- # quil (1)
- # re-frame (5)
- # reagent (23)
- # reitit (6)
- # remote-jobs (1)
- # rewrite-clj (4)
- # ring (38)
- # shadow-cljs (54)
- # sql (9)
- # uncomplicate (5)
- # xtdb (1)
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.
OK, but ideally I'd want f to take an arg of whatever (:a x) is
(cond
(:a x) (f (:a x))
(:b x) (g (:b x))
:else "nothing")
without repeating the get
obviously that's fine but I always suspect there's a neater way in clojure.
Not sure if https://github.com/Engelberg/better-cond fits the use case
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
"nothing")
to that cond above πNested (if-let)
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
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.
Hey, does anyone have any other video/blogs that talk about Clojure at scale? like this https://www.youtube.com/watch?v=av9Xi6CNqq4 or Rich Hickey's Language of the System.
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).
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 π
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)
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
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 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-enterwere 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 section...is that only for testing then?
https://clojure.org/about/spec#_using_specs 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) (...do stuff...) (throw...)
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?
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
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 match...so 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 π
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
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!
Hi there, dear Clojurians: Can you tell me how is next
different than rest
?
https://stackoverflow.com/questions/4288476/clojure-rest-vs-next has a good couple of answers
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!
Apparently, what Iβm able to gather, doing a next
on an empty sequence returns nil
. Doing a rest
on an empty sequence returns ( )
.
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 https://www.braveclojure.com/core-functions-in-depth/#first__rest__and_cons. So rest
is more sequence based and lazier than next
is my understanding.
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.
Example
(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
: https://github.com/candid82/joker#optional-rules
it depends on how much you like reading Lisp π
Another option (as well as let
) is threading macros https://clojure.org/guides/threading_macros
if you were to let
bind all of those it would get ugly. this is a "salt to taste" situation.
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
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.
Java has a whole internationalized system for this in the jdk
So look up how to do it in Java and use interop
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: https://en.wikibooks.org/wiki/Clojure_Programming/Examples/JDBC_Examples#Microsoft_SQL_Server 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 clojure.java.jdbc
are you trying to use?
More up to date documentation is here http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html (and that is linked to from the GitHub repo for the project https://github.com/clojure/java.jdbc/ )
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 clojure.java.jdbc
(0.7.9 is current) and you also have the Microsoft SQL Server JDBC driver in your dependencies.
Example (for deps.edn
): https://github.com/clojure/java.jdbc/blob/master/deps.edn#L18-L19
In project.clj
, that would be [com.microsoft.sqlserver/mssql-jdbc "6.2.2.jre8"]
Also, feel free to ask any detailed questions about clojure.java.jdbc
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 clojure.java.jdbc 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. π
Sorry to hear you've been struggling, especially for weeks π
If it's not obvious, I'm the maintainer of clojure.java.jdbc
(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.