This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-24
Channels
- # arachne (1)
- # bangalore-clj (11)
- # beginners (177)
- # boot (63)
- # business (1)
- # cljsjs (21)
- # cljsrn (3)
- # clojars (4)
- # clojure (116)
- # clojure-art (4)
- # clojure-belgium (3)
- # clojure-india (1)
- # clojure-italy (6)
- # clojure-russia (14)
- # clojure-spec (8)
- # clojure-uk (67)
- # clojurescript (51)
- # community-development (5)
- # cursive (13)
- # datascript (14)
- # datomic (29)
- # devcards (3)
- # emacs (3)
- # events (3)
- # funcool (4)
- # hoplon (29)
- # mount (6)
- # om (34)
- # om-next (5)
- # onyx (16)
- # perun (8)
- # planck (22)
- # re-frame (13)
- # reagent (5)
- # ring-swagger (21)
- # rum (3)
- # spacemacs (3)
- # specter (1)
- # untangled (39)
hey guys, I’m doing some clojure study and yesterday I faced a simple problem. the problem says: you have to save to a file the number of characters from a text below is my solution
(defn char-counter [filename text]
(spit filename
(reduce +
(map #(.length %)
(str/split text #"")))))
can you guys help me to make more idiomatic or a better way to do that ?the key insight here I guess is that strings in clojure work like sequences, so any function working on a sequence works on a string.
although, I’d consider the spit
part rather uninteresting if I wrote this code, I’d be more concerned with the count bit.
besides, when you decouple the logic from the side-effect, the logic tends to become more generic
@grav the docstring in =
is really misleading claiming "compares numbers ... in type-independent manner". In reality there are different rules, see https://github.com/jafingerhut/thalia/blob/master/doc/other-topics/equality.md
from http://clojure.org/reference/reader I find about keywords that "They cannot contain '.’ or name classes"
clj.user> (def my-map {:foo.bar "baz"})
;; => #'clj.user/my-map
clj.user> (:foo.bar my-map)
;; => "baz"
clj.user> :foo.bar
;; => :foo.bar
clj.user>
it’s also commonly used by hiccup and other templating tools that use hiccup-style formatting
the restriction makes sense for plain symbols since that clashes with package names, like foo.bar.baz
, but :foo.bar.baz
by itself has no special meaning in clojure
it's full of quirks, but I don't think it's considered important atm, maybe specs will come for this stuff but that's it
There has been (lots of) discussion on the clojure mailing list about non-readable keywords
right, I saw them a few times in other projects as well. I guess I'm asking my question in the wrong way. :)
You never have to use :: at all
It's just a tool for typing less to get qualified keywords
Using :: with an alias is pretty handy though to separate the namespaces in your code from the actual namespaces via indirection
If you say ::foo/whatever everywhere you can define what foo aliases exactly once at the top of the file (and easily change it)
It won't work - nrepl is a totally different protocol
You could write a server repl accept function that talked nrepl but afaik no one has done so
The default socket server repl is just stream based
If i have a library that depends on something that is not distributable, ie not on maven or anything, but the jar is available at a public url, is it possible for my library to download this jar from the url and add it to the classpath automatically whenever it's used? Or is this pretty much going to have to be up to the end user
you will have to first install it with mvn install-file
to your local .m2/repository. or with mvn deploy-file
to a locally accessible Maven repository
still struggling why the code below is considered bad
I’m using one global
symbol inside the save
function. everyone I showed the code said to use the save-explicit
but nobody gave me a real reason, only do not use globals
.
can anyone help me with that ? appreciate guys
(def filename “filename.txt”)
(defn save [text]
(spit filename text))
(save “hello”)
(defn save-explicit [file text]
(spit file text))
(save-explicit filename “hello”)
@oliv I think they mean to say that the save
function you have declared is way less reusable then the save-explicit
function. Globals are not all bad but most of the time it is better to make things an explicit argument to a function.
yeah ! agree that save
function is way less reusable. but on my domain , suppose I don’t need the reusable save
because all my use is to save on filename
. I’m super lazy, YAGNI is my middle name 😄
if functions rely on things other than the data they’re given as explicit inputs, they usually end up being harder to think about, maintain, and change over time
save-explicit
is better because it says: this is all the information i rely on in order to work, it’s all right here, look at it
but imagine the simple situation i will call dependency hell
suppose the function controller uses the save-explicit
(def file “hello.txt”)
(defn controller [request]
(save-explicit file (:body request))
@jrheard ^^ in the above situation, the controller must change the signature to receive the save-explicit
dependency
or, well, in the first example you gave your save-explicit
call uses the global file
variable so i don’t think that’s quite the same, but i see your point - yeah, there’s definitely a tension between “all functions should take all their data as explicit inputs” and “oh god now all of my functions take 300 inputs"
there’s no 100% right answer, your point is completely valid - do what makes the most sense in whatever situation you’re in imo
i’ve seen this specific topic debated in this channel many times and there are strong arguments on either side, it basically just kinda depends 🙂
yeah ! ok ! I’m very new on clojure and reading/watching talks a lot. it is unanimous that everyone advocates that ( explicit arguments/dependencies )
hm, i can’t actually think of a good one off the top of my head - if one comes to me later today, i’ll definitely link to it! sorry 😅
if you can find a good talk or article / etc on pure functions, they’re basically the same idea
save-explicit is pure because it only relies on its inputs, save is impure because it relies on something in addition to its input