Fork me on GitHub
Chris K03:06:40

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

(defn repl-loop
  (while true
      (print "REPL>>")
      (print-prompt (get-user-input)))))


you need to invoke the reader and then eval.


(println (eval (read-string (get-user-input)))) ReadEvalPrintLoop


@sunchaesk This is how Clojure's REPL is implemented: -- the source code can be an interesting read sometimes when you're trying to figure out how to do similar things.

Chris K03:06:49

@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


@U11BV7MTK wrote a working code in the comment just above


user=> (while true (println (eval (read-string (read-line)))))
(+ 1 1)
(let [x 1] (+ x 2))

Chris K04:06:38

oh shoot didn't see that

Chris K04:06:42

thank you so much

Chris K03:06:39

@seancorfield Thank you so much I never really thought about it 😄


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.

(f x)
If f is 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


This may be an overly general question but I am starting on a project where I want to wrap a Javascript library and use it in Clojurescript. The Library (libraries - since there are sub libraries) is It is available on NPM-- It is basically a parsing library that parses text and returns json - I want to use the json in my Clojure/Clojurescript project - There is a version that has been adopted for Lambda but I want to basically create a Clojurscript SDK - possibly to publish as a library - and use it in my project — I know I have a long way to go but is there a tutorial? or an example I could go by for doing something like this - I have gone through the Shadow-cljs documentation but I wanted something more specific to wrapping an existing library — I know there are several examples with java

Rob Haisfield21:06:34

Does anyone know any good animations that show how `apply` works in Clojure? Something like this?


apply is fundamentally a different sort of thing from map, filter, and reduce


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.

hiredman21:06:22 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])

Rob Haisfield14:06:42

This is really helpful, thank you

Rob Haisfield14:06:44

This seems like the sort of tool I need to explore a lot


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).