This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-28
Channels
- # announcements (2)
- # aws (21)
- # babashka (4)
- # babashka-sci-dev (17)
- # beginners (3)
- # cider (16)
- # clj-kondo (61)
- # clojure (122)
- # clojure-europe (11)
- # clojure-spec (3)
- # clojurescript (2)
- # conjure (19)
- # helix (9)
- # hyperfiddle (2)
- # inf-clojure (18)
- # lsp (12)
- # off-topic (15)
- # react (2)
- # releases (1)
- # shadow-cljs (40)
- # spacemacs (4)
- # tools-deps (5)
- # vim (15)
Is there a common naming convention for patterns? Something like the convention of using a ?
for boolean fns (e.g. string?
)
Do you mean for symbols bound to regular expression patterns?
Yeah for the bindings themselves. E.g. (def alphanumeric-pattern #"[A-Za-z\d]")
I like that. Simple and clear
Looking at our codebase, it seems we mostly use a -regex
suffix.
I like that better. I read -re as "in relation to".
#naming-things is a channel I just started for discussions like these - one of my favorite kinds of channels from other programming language chats.
anyone have an example of how to copy the contents of a ZipEntry
to a string? i cannot figure this out for the life of me - the java docs are quite confusing for input/output streams
i figured it out... you have to navigate to the appropriate entry and then read from the underlying ZipInputStream
- not the ZipEntry
itself. :man-shrugging:
I'd like to understand better the mechanism behind clojure.repl/source
. Here's a snippet:
(ns my-ns
(:require [clojure.repl :as repl]))
(defn foo [a] (inc a))
(repl/source omicron.core/foo)
This prints
Source not found
I'd like to understand why this happens (isn't my file on the classpath too?) and what's the standard solution to get the source of the functions in your own project.And by the way, why doesn't clojure put the source code in the metadata for the function? There has to be a tradeoff there, but I'm unsure where!
The source
function finds the .clj(c) file on the classpath, then reads until the line that is in the metadata and reads the entire form
@U04V15CAJ I made a mistake in copying; but this doesn't work either:
(ns omicron.core
(:require [clojure.repl :as repl]))
(defn foo [a] (inc a))
(repl/source omicron.core/foo)
src
is automatically on the classpath, but perhaps if you restart your REPL it will work
unfortunately it doesn't even restarting. I can make a repro example, but it's just the snipped I pasted. I'm using:
;; CIDER 1.3.0 (Ukraine), nREPL 0.9.0
;; Clojure 1.11.0, Java 11.0.12
you don't need CIDER to test this. could you just go into a terminal with clj
and then (require '[omicron.core])
and then (clojure.repl/source omicron.core/foo)
?
Interestingly, clj
errors out when I try the require:
user=> (require '[omicron.core])
Execution error (FileNotFoundException) at user/eval2 (REPL:1).
Could not locate omicron/core__init.class, omicron/core.clj or omicron/core.cljc on classpath.
But the file is definitely called src/omicron/core.clj
and I'm invoking clj
from the root of the project
/home/carlo/code/clojure/omicron λ tree .
.
├── deps.edn
└── src
└── omicron
├── core.clj
└── new.clj
That was dumb on my part, sorry; anyway, now the code works in clj
but still not in cider
the classpath is not updated dynamically, so you have to start a new CIDER REPL. if it doesn't work, perhaps you didn't restart it hard enough ;)
What does (System/getProperty "java.class.path")
say? It should list src
as one of the first things
maybe this https://clojureverse.org/t/using-doc-and-source-in-cider-vs-lein-repl/1077 is relevant, reading
thank you @U04V15CAJ! I appreciate the help in understanding it has to be a cider issue 🙂
Hey folks, wondering what you'd recommend to an experienced dev to get a deep understanding of clojure. Can be a book, blog series, video course, anything really
I presume you mean a dev who is experienced in other programming languages but not Clojure. In that case, I'd recommend just writing your own Clojure code and reading the sources of libraries or Clojure itself. Depending on which other languages the experience is in, the biggest challenge is likely to be unlearning previous habits and learning to think in Clojure. Some more advanced books might be useful too. A good starting point is https://clojure.org/community/resources
Joy of Clojure is my fav clojure textbook
maybe a bit dated now
I guess I should expand a bit more on what I'm looking for. I've been writing clojure for a few years now but I never really invested time in learning it thoroughly. So I want something that will give me the most comprehensive/deep understanding of the language
oh, probs just read the source code then
Solve an interesting advent of code (or similar) problem in your language of choice, then solve it in clojure, then see how a pro clojurian did it, then gain insight, then repeat.
I started reading Programming Clojure. Any chance you read that @UA7E6DU04? I wonder how you would contrast the two
Joy of Clojure is a fantastic book - it's not a super easy read but if you have been doing clojure for a few years I really recommend it
http://clojure.org has a lot of articles explaining Clojure
Thanks! Yeah I have gone through some of those in the past. I think I'd prefer to have a one stop shop explaining and going into depth. A book seems like maybe the best fit for this?
Reading the source is good All the books with Alex Miller as an author You can also read Bagwell's paper on HAMTs And Rich's talks are also an excellent resource
There’s also Clojure, The Essential Reference by Renzo Borgatti, that provides detailed explanation of every concept. It’s one of those available while still being written from Manning. It’s only 1172 pages so far.😆
I second Clojure, The Essential Reference too. Probably not an introductory text, but wonderful to read after an introduction
I recently noticed a nice trick that is actively used in clojure.core eg core_print
: code is in a https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core_print.clj from core and it uses in-ns
at the top of the file to be included in the clojure.core
ns. I haven't seen this in the wild. Is it cos I haven't seen enough code bases or because it's not preferred by the community for some reason?
clojure.core
is a very unusual piece of Clojure code -- and it's a massive namespace -- so splitting it up is, I suspect, just a way to make it a bit more manageable when working on it. I can't imagine any real world production code ending up looking like that...
It's not preferred because it confuses tooling and isn't always supported by tooling, even if it's totally fine in the language itself
yeah, I was thinking that it might mean that the tooling is weak / incomplete.
I don't think that's the conclusion I would draw from it 🙂
I find it a bit weird that https://clojure.org/reference/namespaces says
At the Repl it’s best to use in-ns, in which case the new namespace will contain mappings only for the classnames in java.lang. In order to access the names from the clojure.core namespace you must execute (clojure.core/refer 'clojure.core). The user namespace at the Repl has already done this.
I see beginners get confused by in-ns
in the REPL quite a bit.Fortunately, in https://clojure.org/guides/learn/namespaces#_loading it says (after explaining the mapping from namespaces to files)
Due to this loading convention, most Clojure is structured with a 1-to-1 mapping of namespaces to files, stored in hierarchical fashion that maps to the namespace structure.
And the official guide about namespaces clarifies the in-ns
thing https://clojure.org/guides/repl/navigating_namespaces#_switching_to_an_existing_namespace_with_in_ns
What happens if you in-ns to a namespace that has never been created? You will see strange things happening.
Strange, indeed 🙂ok, so weird historical edge case
that persists cos it's working
so I guess it's important for tooling to understand that files and ns are not the same thing, though they tend to be
And because ns
uses in-ns
as well as several other low-level namespace-related functions...
It's very rare for files and nses not to map 1-to-1. I can't think of anything outside clojure.core
that I've seen that does that...?
no - me neither hence the question
I want to support database rather than file based tooling so anything that is unusual in this way is good to know.
I appreciate your comments @U04V70XH6
We have a handful of in-ns
calls but they're all in RCFs and they're like this:
(require ' :reload)
(in-ns ')
...
(all from my teammate -- I don't use in-ns
because my editor/REPL combo always switches to the right ns to eval code automatically)I know until recently you also had to load
the namespace in CLJS but that was fixed
"recently" being a few years ago 🙂
RCFs?
(comment ..)
with dev-only code in it (generally). Stu Halloway coined the term because Rich uses them quite a bit 🙂
I don't know, I think you could argue the tooling is incomplete, but also the idioms people ended up with tend to not split a namespace across files, and so the tooling never had to take on that additional complexity. in-ns
is also harder to work with for tooling, because it doesn't have dependencies, instead you would have other calls to refer or require, so it's a lot harder to manage for tooling.
in-ns
is the special form and it mirrors Common Lisp more, it's more primitive and less useful.
Even at the Repl, I personally use ns
to switch even to existing namespaces.
Though it looks like in-ns
and ns
actually both start out as the same thing, both special forms, and then ns is rebound to the core ns
clojure.core
does a lot of weird stuff as it is bootstrapping the language across that whole namespace...
@U04V5V0V4 how about breaking the REPL into its components? Read analyzes and inserts to the db, eval takes an id, reifies and evals
@UK0810AQ2 it's not the way I'm going but it sounds like an interesting approach!
@U04V5V0V4 are you trying to go the Unison way where terms that are alpha equivalent are semantically equivalent?
I don't know what that means
In Unison things like (fn [x] (inc x))
and (fn [y] (inc y))
are equivalent and saved/linked in the same hash (db entry)
I was wondering if this was the main thrust of your question wrt saving code in a database
ah no, it's not that smart. I'm only tracking named vars per ns.
Those kind of similarities could be detected based on the data but that's downstream
the level of ambition is small: a fancier, better informed editing experience for Clojure
spec/conform is my weapon of choice
how would in-ns
work back then?
ah ok - yes, I see now that the first check-in of clojure.core
also used in-ns
The issue is that the ns
macro is defined in clojure.core, so you can't use the ns
macro before it has been defined obviously, that's why clojure.core has to use in-ns
Actually, it seems like ns
is first available as a special form that's similar too in-ns
but works as a macro, and then it gets replaced by clojure.core's ns
What's the best way to render user-provided Markdown with hiccup2 (that respects escaping) :thinking_face:
Markdown is often rendered into HTML for WIKI web pages. This exists but I haven't used it https://github.com/mpcarolin/markdown-to-hiccup
@U04V15CAJ I figured it out, I guess it was too late yesterday 😅
According to Markdown spec it doesn't escape anything. And hiccup2 does, everything by default. And it's a bad idea to render as raw
. So what I have to do is to escape the input first, render to Markdown, and then use raw
@U030521UZ1N can you explain how hiccup escapes everything? As an aside, the terminology "escaped" is really weird to me given what it's about, i never thought about that description very much.
It's just a way to have reserved functions. like & has special meaning in a url, it's not just a string.
but what's escaping? getting free? running away?
i guess if your the character "&" your escaping the pool of reserved words/symbols.
> but what's escaping? getting free? running away? I'm not native speaker, but there is no need to be sarcastic. But here you go: https://github.com/weavejester/hiccup/blob/master/src/hiccup/util.clj#L80-L88
Calling this escaping is common CS terminology. You're replacing special characters with escape sequences. https://en.wikipedia.org/wiki/Escape_sequence
'Escape character', Wikipedia: "Not to be confused with control character, escape sequence, or Escape key." :-/
For me, it's easier to think of them as reserved keys/chars/function. I can't get the & table the restaurant, i can get the &foo table though and tell my friends a lie about the name.
Idk. Communication is hard.
I think the escape sequence was called that because the escape key was the original character used. I was just looking up why the key was called that, when I came across that helpful warning.