clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
zhuxun2 2020-11-20T18:14:58.443700Z

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

dpsutton 2020-11-20T18:16:16.443900Z

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

🤘 1
william 2020-11-20T19:18:50.446Z

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.

2020-11-20T19:48:36.447100Z

(: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

2020-11-20T19:49:15.447700Z

so you are actually running (logic/membero what '((:d :e :f)))

2020-11-20T19:51:15.448100Z

user=> (logic/run* [what] (logic/membero (list :a :b what) '((:d :e :f))))
()
user=>

william 2020-11-20T19:56:41.450800Z

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't

2020-11-20T19:57:25.451700Z

you are quoting the whole list, so 'what' is jsut the symbol 'what' instead of an lvar

bronsa 2020-11-20T19:57:27.451800Z

first one has what evaluated, second one has the 'what symbol

william 2020-11-20T19:58:49.453100Z

I see, thank you!

2020-11-20T19:59:50.453700Z

if you are familiar with prolog, one of the big differences with a minikanran style dsl is how names are treated

2020-11-20T20:01:33.455500Z

(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

2020-11-20T20:02:05.456200Z

the "name" in the logic system is actually an lvar that is created and bound to q

alexmiller 2020-11-20T20:02:09.456400Z

https://clojure.org/reference/data_structures#Keywords

2020-11-20T20:02:31.456900Z

so logic/run* expands into something like (let [q (new-lvar)] ...)

william 2020-11-20T20:02:51.457300Z

that's a very good explanation, thanks for pointing that out!

william 2020-11-20T20:09:32.458100Z

can someone point to the documentation of why (:a :b :c) returns :c ?

borkdude 2020-11-20T20:10:20.459Z

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

william 2020-11-20T20:10:36.459300Z

oh it's the default, thanks!

borkdude 2020-11-20T20:11:06.459600Z

(:a {:a :found} :default) ;;=> :found 

william 2020-11-20T20:12:04.460500Z

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

alexmiller 2020-11-20T20:17:53.460800Z

I linked the doc in thread above fyi https://clojure.org/reference/data_structures#Keywords

🙌 1
william 2020-11-20T20:19:53.461100Z

thanks!

wimomisterx 2020-11-20T23:13:09.463400Z

is there a transducer that returns each individual element of a vector?

alexmiller 2020-11-20T23:19:25.463700Z

cat ?

wimomisterx 2020-11-20T23:23:12.464500Z

ah that's it! thanks!