Is there a way to give an alias to an imported symbol? I know (:require [my.lib :as lib])
is possible, but is there something equivalent to (:require [my.lib :refer [myfunc :as func]])
?
clojure.core/refer
([ns-sym & filters])
refers to all public vars of ns, subject to filters.
filters can include at most one each of:
:exclude list-of-symbols
:only list-of-symbols
:rename map-of-fromsymbol-tosymbol
I'd appreciate some insight on this core.logic
expression:
(logic/run* [what]
(logic/membero (:a :b what) '((:d :e :f))))
why in the world is this returning ((:d :e :f))
?
The equivalent prolog would be:
?- member([a, b, X], [[d, e, f]]).
false.
(:a :b what)
is invoke the keyword :a
with the arguments :b
and what
and because of how keywords work as a function, returning the lvar what
so you are actually running (logic/membero what '((:d :e :f)))
user=> (logic/run* [what] (logic/membero (list :a :b what) '((:d :e :f))))
()
user=>
thanks @hiredman! I was aware of the behavior (:a {:a 3}), but didn't know that I could pass multiple arguments to a keyword used as a function. Where should I read about that behavior? Besides, what's the difference between:
(logic/run* [what]
(logic/membero (list :d :e what) '((:d :e :f))))
and
(logic/run* [what]
(logic/membero '(:d :e what) '((:d :e :f))))
the first returns a solution, the second doesn'tyou are quoting the whole list, so 'what' is jsut the symbol 'what' instead of an lvar
first one has what
evaluated, second one has the 'what
symbol
I see, thank you!
if you are familiar with prolog, one of the big differences with a minikanran style dsl is how names are treated
(log/run* [q] (some-goal q))
in this expression 'q' is not a name in the logic language, it is a name in the language (clojure) that the logic language is embedded in
the "name" in the logic system is actually an lvar that is created and bound to q
so logic/run* expands into something like (let [q (new-lvar)] ...)
that's a very good explanation, thanks for pointing that out!
can someone point to the documentation of why (:a :b :c)
returns :c
?
@meditans as an expression in Clojure? :a
acts as a function looking itself up in an associative object. if it doesn't find anything, then the other argument is the default value.
oh it's the default, thanks!
(:a {:a :found} :default) ;;=> :found
yes, thank you! I knew that a keyboard could be used to search in a map, but didn't think about the need of default values
I linked the doc in thread above fyi https://clojure.org/reference/data_structures#Keywords
thanks!
is there a transducer that returns each individual element of a vector?
cat ?
ah that's it! thanks!