This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-25
Channels
- # babashka (108)
- # beginners (40)
- # calva (78)
- # clj-kondo (4)
- # cljs-dev (2)
- # clojure (26)
- # clojure-dev (1)
- # clojure-europe (1)
- # clojurescript (53)
- # conjure (16)
- # cursive (7)
- # data-oriented-programming (2)
- # datomic (11)
- # emacs (1)
- # fulcro (45)
- # leiningen (22)
- # lsp (30)
- # meander (9)
- # off-topic (14)
- # polylith (13)
- # proletarian (1)
- # reagent (1)
- # reitit (8)
- # releases (1)
- # reveal (4)
- # rewrite-clj (2)
- # sci (12)
- # shadow-cljs (3)
- # sql (2)
- # vrac (1)
is it idiomatic to create a record with no fields as the equivalent of what I'd use a python (implicitly static) class for - a quasi-namespace grouping related functionality?
this question seems closely related: https://stackoverflow.com/questions/5024211/clojure-adding-functions-to-defrecord-without-defining-a-new-protocol
in interactive development, often around manipulating data like data munging, in python my workflow is like (mess with in repl) => (end up with lots of functions) => (throw those functions into static python classes grouped by area) => (refactor into some static classes, some normal classes, some loose functions)
so right now I'm messing with a git diff and I'd probably throw the functions I've some up with into a couple of static classes if I was in python, split by "upstream" and "downstream" processing
being a clojure noob I don't get the idiomatic equivalent, from the linked stackoverflow it seems that what I'd normally do is funky since it would require creating protocols for no reason, and I need to use like a raw map
No need for extra wrapping, just defn them in the namespace
because the unit of grouping and formality is lower, each namespace should be in it's own file? I could do that but it's a bit more ceremony and slightly more complication than static classes would be
If you want to subdivide functions within a namespace, some people use conventions like
(defn foo:bar1 [])
(defn foo:bar2 [])
ok sounds like my three options: just use ordering/naming conventions, do something clever with maps/macros, just use namespaces
The feature in Clojure for grouping and naming functions is namespaces
I guess one question to ask about the practice you have developed in Python is: for what reason do you want to collect multiple Python functions in a Python 'static class'? Why did you do that, vs. put those Python functions at the top level in your source file? Clojure functions at the top level in a namespace seem to correspond closely with Python functions defined at the top level, certainly. There isn't a direct analog in Clojure of creating a static class that is there for nothing but containing methods, but it naturally leads to the question of "why bother creating such a class?"
@zimablue one thing that can be useful is to design functions such that they are meant to be used with a namespace alias
(ns your-project.stack)
(defn stack-create []
'())
(defn stack-pop [stack]
[(first stack) (rest stack)])
(defn stack-push [stack item]
(cons item q))
(ns other-ns
(:require [your-project.stack :refer [stack-create stack-pop stack-push]]))
(let [stack1 (-> (stack-create)
(stack-push 1)
(stack-push 2))
[head stack2] (stack-pop stack1)]
...)
(ns your-project.stack)
(defn create []
'())
(defn pop [stack]
[(first stack) (rest stack)])
(defn push [stack item]
(cons item q))
(ns other-ns
(:require [your-project.stack :as stack]))
(let [stack1 (-> (stack/create)
(stack/push 1)
(stack/push 2))
[head stack2] (stack/pop stack1)]
...)