Fork me on GitHub
#beginners
<
2017-01-02
>
pwalsh18:01:38

How does one expose a single namespace for code spread across several files? eg, in python:

types/__init__.py # import public stuff from nested modules, and expose with __all__
types/integer.py
types/boolean.py
types/string.py
…etc...
So I just want a core.types namespace as part of the public API for my package, not core.types.integer and so on.... Is there an idiomatic way to do this in Clojure?

notanon19:01:04

there isn't an idiomatic way, since the idiom is to keep one file per namespace, but you can always call the (in-ns ...) function from any file, which I believe will let you then def your vars, etc in the namespace you want.

notanon19:01:40

might want to try it out... it's not something i've ever tried to do or have seen anyone do... so not entirely sure if it works.

jgh19:01:03

You can also use (load "file-name-without-extension") at the end of your files + (in-ns 'namespace). Works pretty well for me.

jgh19:01:01

e.g. fancy-pants.clj:

(ns fancy.pants)
(defonce something { :hi "hi" })
(load "fancy-feast")
fancy-feast.clj:
(in-ns 'fancy.pants)
(defonce something-else {:food "sogood"})

pwalsh20:01:40

@notanon it is probably better not to do this, if it is unusual in clojure, right? it seems to me it must get really unwieldy with large codebases, having a 1-1 relation between namespaces and files? @jgh got it, thanks.

notanon20:01:44

@pwalsh I definitely wouldn't do it. you'll probably find that clojure is so succinct that it would take an absolute boat load of functionality to overwhelm a namespace. but you can always create a wrapper namespace if you're really worried about it. create a bunch of small namespaces like mystuff.mystuff-a, mystuff-b mystuff-c and then create the mystuff namespace and require in mystuff-a/b/c/etc and then expose only the 'publics' you wish to expose. i still find it hard to picture a use case to justify any these options though πŸ™‚ i'd recommend sticking to 1 file/1 namespace, and if you feel it has gotten overwhelming, post the code and let us look at the real example. best of luck.

pwalsh20:01:55

@notanon great advice, thanks

sb22:01:54

Hello, I would like to create a friend authentication with Redis.

sb23:01:54

I can get the KEYS and single data. My question, what is the shorter/ nicer version to get the right format for friend authentication?

notanon23:01:14

what does the result of singleuser look like

sb23:01:23

one moment

notanon23:01:28

so for each element in the "results: " vector above, you need to call singleuser query and get back the details

notanon23:01:44

and then put the results of each singleuser into one map called users?

sb23:01:55

yes, that is ok. I try to do this query.

notanon23:01:59

if i am understanding what you want, then you'd need to map call-to-single over a results-of-all-users call

notanon23:01:07

and then probably reduce it into the format you want

sb23:01:33

Yes, I will do it. Just I created a few version and every was bad.

notanon23:01:54

sometimes starting from scratch helps πŸ™‚

sb23:01:02

Thanks πŸ™‚

sb23:01:15

(I need to go now, but I will back..)

jgh23:01:28

Is there a way I can concatenate an earlier value in a hashmap in a later one? For example, something like { :baseUrl "" :absUrl (str :baseUrl "/test2") }

jgh23:01:04

or would I just have to set the :baseUrl constant in its own function

adamkowalski23:01:26

yeah i don’t think you can just reference it like that

adamkowalski23:01:32

you could wrap it in a let binding

adamkowalski23:01:07

(let [base-url β€œhttp://test/”] {:base-url base-url :abs-url (str base-url β€œtest2”)})

adamkowalski23:01:34

another option is to pass it through assoc

adamkowalski23:01:37

(-> {:base-url β€œhttp://test”} (fn [hash-map] (assoc hash-map :abs-url (str (:base-url hash-map) β€œtest2”))))

jgh23:01:39

the first way seems more straightforward heh, that works though thank you.