Fork me on GitHub
#clojure
<
2020-11-20
>
zhuxun218:11:58

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

dpsutton18:11:16

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

metal 3
Carlo19:11:50

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.

hiredman19:11:36

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

hiredman19:11:15

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

hiredman19:11:15

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

Carlo19:11:41

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

hiredman19:11:25

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

bronsa19:11:27

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

Carlo19:11:49

I see, thank you!

hiredman19:11:50

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

hiredman20:11:33

(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

hiredman20:11:05

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

hiredman20:11:31

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

Carlo20:11:51

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

Carlo20:11:32

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

borkdude20:11:20

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

Carlo20:11:36

oh it's the default, thanks!

borkdude20:11:06

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

Carlo20:11:04

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

wimomisterx23:11:09

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

wimomisterx23:11:12

ah that's it! thanks!