beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Chris K 2021-06-07T03:18:40.367700Z

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

dpsutton 2021-06-07T03:29:39.368Z

you need to invoke the reader and then eval.

dpsutton 2021-06-07T03:30:48.368800Z

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

seancorfield 2021-06-07T03:33:57.369800Z

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

Chris K 2021-06-07T03:35:39.370400Z

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

Chris K 2021-06-07T03:59:49.370500Z

@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

solf 2021-06-07T04:04:20.370700Z

@dpsutton wrote a working code in the comment just above

dpsutton 2021-06-07T04:08:48.371200Z

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

Chris K 2021-06-07T04:34:38.371400Z

oh shoot didn't see that

Chris K 2021-06-07T04:34:42.371600Z

thank you so much

popeye 2021-06-07T07:46:31.372600Z

What macros do which functions cannot do, I meant (when to use macros and when to use functions)

teodorlu 2021-06-07T10:38:24.373100Z

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.

teodorlu 2021-06-07T10:43:21.373300Z

(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

2021-06-07T15:25:28.381500Z

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 https://github.com/accordproject/cicero 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 https://github.com/accordproject/aws-qldb-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

2021-06-07T21:24:34.382500Z

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

2021-06-07T21:28:56.384800Z

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

2021-06-07T21:33:12.386800Z

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.

2021-06-07T21:37:22.389800Z

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

borkdude 2021-06-07T21:38:22.390800Z

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

2021-06-07T21:39:55.391800Z

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

2021-06-08T14:58:44.413800Z

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

2021-06-07T21:43:53.392700Z

I don't care for the comparison there, because it suggests some relationship between reduce and apply

2021-06-07T21:47:10.394400Z

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

2021-06-07T21:50:43.394800Z

https://github.com/borkdude/sci/blob/master/src/sci/impl/evaluator.cljc#L314 like here is apply in the eval for sci