This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # announcements (13)
- # atom-editor (3)
- # babashka (53)
- # beginners (28)
- # clojure (30)
- # clojure-australia (4)
- # clojure-europe (22)
- # clojure-germany (1)
- # clojure-italy (6)
- # clojure-nl (3)
- # clojure-norway (14)
- # clojure-spec (8)
- # clojure-uk (8)
- # clojurescript (19)
- # cursive (33)
- # datomic (4)
- # defnpodcast (6)
- # events (3)
- # fulcro (3)
- # graalvm (77)
- # jobs (2)
- # juxt (11)
- # podcasts (1)
- # reitit (1)
- # releases (2)
- # remote-jobs (2)
- # sci (1)
- # shadow-cljs (27)
- # sql (6)
- # testing (10)
- # tools-deps (36)
- # vim (4)
- # xtdb (15)
How can I make a repl using clojure? Any help? THis is my current code but doesn't work 😞
(defn print-prompt [s] (print (str "\n" s))) (defn get-user-input  (read-line)) (defn repl-loop  (while true (do (print "REPL>>") (print-prompt (get-user-input)))))
@sunchaesk This is how Clojure's REPL is implemented: https://github.com/clojure/clojure/blob/38524061dcb14c598c239be87184b3378ffc5bac/src/clj/clojure/main.clj#L456-L466 -- the source code can be an interesting read sometimes when you're trying to figure out how to do similar things.
@seancorfield sorry to bother, but could there be like a simple code that works? and maybe I will try improving from it? I just can't seem to make a simple one work
user=> (while true (println (eval (read-string (read-line))))) (+ 1 1) 2 (let [x 1] (+ x 2)) 3
What macros do which functions cannot do, I meant (when to use macros and when to use functions)
Macros allow you to capture expressions before you're evaluated.
fis a function, you'll have access to the value of x. If f is a macro, you'll be able to know that its name is x and not y.
(defn f-function [x] (+ x x)) (defmacro f-macro [x] (cond (= x 'x) :x (= x 'y) :y :else `(f-function ~x))) (f-function 10) ;; => 20 (f-macro 10) ;; => 20 (let [x 10] (f-function x)) ;; => 20 (let [x 10] (f-macro x)) ;; => :x
Does anyone know any good animations that show how `apply` works in Clojure? Something like this? https://jstutorial.medium.com/map-filter-and-reduce-animated-7fe391a35a47
I am not sure I have a good way to describe the different though, maybe apply is a control flow operation, it is function application reified. map, filter, reduce are dataflow operations.
https://norvig.com/lispy.html defines a little lisp in python, which doesn't have apply, but if you look at the definition of eval, it contains an implementation of apply
apply is probably one of those things that you will find when you really need it.
e.g. you have
[1 2 3] and
max, how do you call
max on that list (assume that you can't use
reduce for whatever reason)?
if you really wanted to you could go through and change all your function calls in your programs from
(f x y) to
(apply f [x y])
I don't care for the comparison there, because it suggests some relationship between reduce and apply
reduce is firmly a construct of the language, given a language without it, if you have first class functions you can write it. apply is the definition of your language bleeding into the language (sort of like eval, and in fact eval is a superset of apply).
https://github.com/borkdude/sci/blob/master/src/sci/impl/evaluator.cljc#L314 like here is apply in the eval for sci
This is really helpful, thank you