This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-10
Channels
- # beginners (35)
- # cider (165)
- # cljsrn (18)
- # clojars (1)
- # clojure (141)
- # clojure-greece (2)
- # clojure-italy (11)
- # clojure-nl (1)
- # clojure-spec (21)
- # clojure-uk (89)
- # clojurescript (56)
- # community-development (3)
- # cursive (3)
- # data-science (55)
- # datomic (13)
- # emacs (12)
- # fulcro (31)
- # graphql (6)
- # jobs-discuss (35)
- # lein-figwheel (10)
- # mount (2)
- # off-topic (3)
- # onyx (22)
- # parinfer (4)
- # portkey (7)
- # re-frame (29)
- # ring-swagger (4)
- # shadow-cljs (37)
- # specter (9)
- # sql (30)
- # tools-deps (15)
- # vim (2)
- # yada (17)
Just implemented Brainfuck interpreter in Clojure š Is there a smart way of doing ADTs other than dictionaries and creating multimethods which are dispatched by key? This is the way I did it :face_with_rolling_eyes: Challenge link: https://www.hackerrank.com/challenges/brainf-k-interpreter-fp/problem Code link: https://github.com/denis631/-Hackerrank-BrainF__k-interpreter/blob/master/src/brainfuck_interpreter/core.clj
@denisgrebennicov that's the usual way to do it
After a long time in the CLJS world, I need to jump back to Clojure to create a simple server (mostly a wrapper around large existing Java libs, to deliver a REST API). What's the current best-practice set of libraries for this?
Liberator maybe? http://clojure-liberator.github.io/liberator/
I've got a load of functions that take an optional first parameter, and then calls itself inside a binding. e.g.
(defn foo
([opts a b c] (binding [*options* opts] (foo a b c)))
([a b c] (+ a b c)))
but it's a bit... repetitive, so I decided to write a macro, to do the binding for me
(defmacro defn-opts [name args & body]
`(defn ~name
([options# ~@args] (with-options options# (~name ~@args)))
([~@args] ~@body)))
Works fine... Until my function has var-args.
e.g.
(defn-opts vargs [& args] (count args))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: & in this context, compiling:(/tmp/form-init835293560141170332.clj:1:1)
but I'm finding it impossible to google a solution, because &
isn't googleable.
Is there a solution to this? I imagine it is as defn is a macro.also, is there a way to support (optional?) docstrings in my macro like defn
would? or do I need to rewrite most of defn?
you would need to rewalk some of what defn does - both looking for and handling the special symbol & and determining whether the first arg is a string
one way to do this would be to write a spec for the args and conform the arg list against the spec - this gives you a data structure describing how the args conform
lucky for you, defn already has a spec!
defn is a macro and you can see itās spec with (doc defn)
https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L292
looks pretty functioney here?
or is the (. (var defn) (setMacro))
the special sauce?
macros are functions with a meta flag
that flag tells the compiler to expand rather than invoke
in this particular case, defn is being defined not with defmacro because defmacro has not yet been defined (as this is during bootstrap)
defmacro is itself ⦠a macro
anyhow, I digress
you can conform the args to a defn-like macro using the spec :clojure.core.specs.alpha/defn-args
user=> (s/conform :clojure.core.specs.alpha/defn-args '[vargs [& args] (count args)])
{:name vargs, :bs [:arity-1 {:args {:varargs {:amp &, :form [:sym args]}}, :body [:body [(count args)]]}]}
the defn spec knows how to parse the varargs out and break down the inputs. some code would be needed to decide how to transform this data structure into your macro output. That may or may not be harder than manipulating the incoming args directly (but keep in mind that the spec understand the full set of destructuring and nested destructuring thatās possible)
user=> (pprint (s/conform :clojure.core.specs.alpha/defn-args '[vargs [{:keys [foo bar]} [[a b & c]] & args] (count args)]))
{:name vargs,
:bs
[:arity-1
{:args
{:args
[[:map {:keys [foo bar]}]
[:seq
{:elems
[[:seq
{:elems [[:sym a] [:sym b]],
:rest {:amp &, :form [:sym c]}}]]}]],
:varargs {:amp &, :form [:sym args]}},
:body [:body [(count args)]]}]}
you should be able to actually manipulate that data structure too and then s/unform to get back to a āsignatureā but there is a known issue in this particular spec that the arg vectors will roundtrip back to lists instead
iām trying to write a clojurescript macro that expands to include a function call that is defined in my own code base. e.g.: iām writing a macro in my-project/macros
ns in a clojure file and i want the expanded macro to call a function in my-project/util
, which is a cljs file. do i just hard code the namespace?
Hm http://blog.fikesfarm.com/posts/2016-01-05-clojurescript-macros-calling-functions.html suggests thatās exactly what one does. mfikes to the rescue
Hello, I am a beginner in Clojure with async.core. I was wondering if knew of any good resources regarded async especially with 'channels'.
@lee.justin.m thank you!