This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-18
Channels
- # announcements (1)
- # asami (17)
- # babashka (43)
- # beginners (36)
- # biff (2)
- # calva (24)
- # clerk (3)
- # clj-kondo (9)
- # cljdoc (18)
- # clojure (16)
- # clojure-berlin (1)
- # clojure-europe (7)
- # clojurescript (8)
- # clr (4)
- # conjure (3)
- # cursive (2)
- # emacs (15)
- # funcool (4)
- # humbleui (2)
- # hyperfiddle (118)
- # kaocha (3)
- # malli (5)
- # membrane (23)
- # off-topic (16)
- # pathom (3)
- # reitit (25)
- # releases (3)
- # shadow-cljs (13)
- # xtdb (6)
I just wrote my by biggest program in Clojure so far - a chess game. Amazing experience with the REPL, never was able to test ideas this fast before. I'm still learning the language. Yet I'm very uncomfortable with the lack of type checking, passing arguments in the wrong order was my most frequent mistake. I had to implement a parser for PGN ("portable game notation") files so I could exhaustively test as many games as I can, otherwise I'll have the feeling my program is brittle. I don't know what to ask exactly, just want to share this experience. If you people have any advice, it would be greatly appreciated anyway!
If you have multiple arguments, you might be better of accepting a map as an argument, then you can specify keys in the order you want :)
maps all the way down!
FYI Clojure 1.11 allows you to have definitions like this:
(defn my-fn [& {:keys [x y z] :as m}]
...)
and call it like this
(my-fn :x 1 :y 2 :z 3)
That's a good idea, but I'm still having problems with functions from the core library. I think it's a matter of muscle memory, and the functions follow a pattern too, like map, filter, reduce, get, get-in
Yeah, they do follow a pattern! A good rule of thumb to remember is that functions that operate on sequences take that argument last and functions that operate on maps have it as the first argument. Also most editors/IDEs have tools to help you, e.g. here's a screenshot from my emacs (and I'm sure whatever you're using offers integration with clj-kondo):
What editor are you using it? My editor shows the arg name for each arg as I’m typing when calling a function.
It’s also usually pretty easy to set up a shortcut that shows the documentation for functions as you use them.
I'm using Emacs+Cider!
One heuristic for core functions: Sequence functions take the sequence LAST Collection functions take the collection FIRST That saves so much headache
> Yet I'm very uncomfortable with the lack of type checking, passing arguments in the wrong order was my most frequent mistake.
Getting a proper setup for clojure-lsp
in my editor has relieved me of a lot of pain like this! It's possible to use both CIDER and Clojure-lsp at the same time.
More information on #CPABC1H61 or https://clojure-lsp.io/.
Regardless of editor support. I think it's great you added those tests. I'm not aware of any type system that let you encode chess logic 🙂 If you like the general affordances of having your data structures and function signatures validated while you're coding, then you might have a look at spec and instrumentation. You can encode a lot of dependencies and rules with it (it's just predicates and logic).

I wrote (most of) a chess engine in Clojure and tbh it does push the language in directions it doesn’t want to go - lots of type hints to ensure primitive math operations etc. But I came away from the experience appreciating just how pragmatic Clojure could be. I’ve got a pretty good grammar for clj-antlr to read PGN very fast (even with messy comments etc) if it’s of any use.

I used a simple assoc list pairing regexes and functions instead of a full blown grammar, I also converted the strings to vectors and then used destructuring to get the individual pieces of information of every move:
"I'm not aware of any type system that let you encode chess logic " John McCarthy strikes again: http://www.lispworks.com/documentation/lw71/CLHS/Body/m_deftp.htm