This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-02
Channels
- # announcements (1)
- # babashka (6)
- # beginners (53)
- # cider (2)
- # circleci (2)
- # clj-kondo (1)
- # cljdoc (1)
- # clojars (2)
- # clojure-berlin (1)
- # clojure-europe (23)
- # clojure-serbia (1)
- # clojure-spec (2)
- # clojure-uk (9)
- # clojurescript (12)
- # datomic (34)
- # emacs (1)
- # fulcro (46)
- # leiningen (15)
- # planck (3)
- # re-frame (46)
- # reitit (17)
- # remote-jobs (4)
- # shadow-cljs (17)
- # sql (3)
- # tools-deps (2)
Hey! I’m going through http://pedestal.io/guides/your-first-api and I’m at the user=> (require 'main)
part but I’m receiving an error of:
Syntax error (FileNotFoundException) compiling at (main.clj:1:1).
Could not locate io/pedastal/http__init.class, io/pedastal/http.clj or io/pedastal/http.cljc on classpath.
I’ve triple-checked and I have my deps.edn
and src/main.clj
correct.
I’ve also just gone through https://tomekw.com/clojure-deps-edn-a-basic-guide/ without any problems, leading me to think that my environment is alrightcheck your spelling on that namespace @jesse.claven
I knew it would be something silly, haha. Thank you @ghadi. This is what I get for starting a tutorial at 1am on a Saturday
Hi Clojurians,
I come from javascript/python world. I am trying to understand how the recursion works in clojure.
((fn foo [x]
(when (> x 0)
(conj (foo (dec x)) x)))
5)
here is how I would write in javascript:
function anon(x) {
if (x >0) {
return [x].concat(anon(x - 1));
}
return [];
}
In JavaScript, I do write the case for when x === 0, which is my terminating case. Why does clojure return ‘(5 4 3 2 1) and not ’(5 4 3 2 1 nil)?
@munichlinux Also consider using the sequence abstractions/library, and try to move away from thinking in terms of loops
(reverse (range 1 6)) ;; sequence functions for working with/iterating over numbers
(take-while pos? (iterate dec 5)) ;; a minimal example of all the logic in your function
@deleted-user Thanks, that makes sense. I was trying https://clojurecademy.com/, the example was part of that question.
Be aware that for very hot loops, loop/recur
can be significantly faster as it does not have the overhead of creating sequences. Normally won't matter though.
Is it possible to decorate all functions in a namespace and redefining them in place? For debugging I would like to log/print the input/output of some function in a defined namespace in a running app. I thought alter-var-root might work but I am unsure.
Thanks a lot!!
@software.dev0218 I now added a Windows build for jet: https://github.com/borkdude/jet/releases/tag/v0.0.11
It can now also be installed using scoop: https://github.com/borkdude/jet#windows
@software.dev0218 I now also added clojure.edn
to the npm package of sci:
> evalString("(require '[clojure.edn :as edn]) (-> \"{:a 1}\" edn/read-string clj->js)")
{ a: 1 }
(so you now have three ways of converting EDN to JSON: jet, babashka via cli or sci as an npm lib)
TIL; There is something called http://exercism.io, and there is a Clojure track: https://exercism.io/my/tracks/clojure
You can peek at someone else's solution. Here's mine: https://exercism.io/tracks/clojure/exercises/armstrong-numbers/solutions/ebeeddf661bd4aca9884c2750fccf392
Cool solution! I just got mentored to find the problem with my solution. Plus some other nice pointers. Such an awesome service. My solution: https://exercism.io/tracks/clojure/exercises/armstrong-numbers/solutions/585ac5a4d0594cd7a2dd36679f8c4494
If I may, Character/digit is a better way of converting char to digit, which I also did not do.
Hello! I am trying to traverse a vector with map, and convert any string found to a key: This is the function I am trying to use with map:
(defn cs2k
[v]
(if (= v str)
(keyword v)
v))
This is my vector I am trying it on: [“string” 3 5]@georghagen I believe you were then looking for (string? v)
instead of (= v str)
yea, (= v str)
is asking if v
is itself the function str
, rather than asking if v
is a string
No problem asking in the #beginners channel about such things, and not trying to discourage you from practice, but str
in Clojure is a function you can call, usually to create a string that represents the arguments you pass to it. The expression (= v str)
determines "Are the values of v and str equal to each other?", i.e. "Is v equal to the function that is the value of str?"
There are a lot of functions in Clojure, and it isn't always clear which one to use when you are new to it. The Clojure cheatsheet might be helpful for you, because it at least tries to divide up the list of Clojure's built-in functions and macros into logically related groups: http://jafingerhut.github.io
Hey! I’m super new to clojure, and am working through
I like it a lot so far, but I’ve a bit stuck and am not sure how to proceed.
There’s an exercise that asks me to create a function mapset
that takes a function and vector as params and does the same thing that map
does but returns a set instead. This is what I tried
(defn mapset
[fn v]
(loop [remaining v
result-set (hash-set)]
(if (empty? remaining)
result-set
(let [[head & tail] remaining]
(recur remaining
(into result-set
(set [fn head])))))))
and I feel I’m “almost” there but it doesn’t work. Any help appreciated!If I'm reading this right, the first error I see is recurring on remaining
each time, instead of tail
, since the latter would be the 'rest' of the vector you have yet to have worked on (and remaining
refers to your beginning state with the full vector, so if you keep recurring on it, you never are moving forward). The second is [fn head]
instead of [(fn head)]
where the latter will call the function on head, and the former will be adding fn itself into the set
so the fix would be
(defn mapset
[fn v]
(loop [remaining v
result-set (hash-set)]
(if (empty? remaining)
result-set
(let [[head & tail] remaining]
(recur tail
(into result-set
(set [(fn head)])))))))
I believeSo I was almost there lol
Stupid mistake, thanks @zdot101!
Just to confirm that I understand it right. The recur tail
only runs within the let [[head & tail]…
block? Or does it re-run whole mapset
?
That makes sense. Thank you.
other small things: I may be missing something, but I don't think you need to call (hash-set)
, and can instead use the first class syntax for sets (ie, writing #{}
in its place). Also, instead of (into result-set (set [(fn head)]))
, you should be able to just call (conj result-set (fn head))
, where the latter is flat out just saying "add (fn head) to set" and the former is saying something like "create a vector containing just (fn head)
, then immediately turn it back into a set, and conjoin these two sets"
Awesome, good feedback, thank you very much.
I still find lisp’s syntax a bit confusing 😛