Fork me on GitHub
#clojure
<
2015-10-22
>
mbertheau04:10:52

Any way to put (reduce-kv #(assoc %1 %2 (:shown? %3)) {} ui) more concisely?

camdez04:10:27

@mbertheau: You could do something like (zipmap (keys ui) (map :shown? (vals ui))), but whether or not that is more concise is open to debate. I typically just keep a map-vals function (`(fn [f m] (reduce-kv #(assoc %1 %2 (f %3)) {} m))`) in my utils.

camdez04:10:57

Then it’s just (map-vals :shown? ui).

camdez04:10:12

Another (more exotic) option would be (into {} (map #(update % 1 :shown?) ui)).

mbertheau04:10:09

map-vals looks very nice. What are the chances of something like it ending up in the standard library?

camdez04:10:44

mbektimirov: I’m probably not the right person to ask, but you’d be in good company using that name / signature (e.g. https://github.com/Prismatic/plumbing/blob/master/src/plumbing/core.cljx#L68).

camdez04:10:35

mbertheau: Whoops, sorry, I meant to tag you.

mbertheau05:10:09

Mmm, ?> and ?>> would come in handy as well.

mbertheau05:10:22

Although these operators start to look like perl.

camdez05:10:54

Yeah, there’s a lot of a good stuff in there. And those functions look crazy, but (1) they’re hard to name, and (2) you come to appreciate the fact that the names are terse and that they nicely parallel the other threading functions.

cfleming09:10:54

I’m getting some bug reports with namespaces referring to mranderson045 - does anyone know what that is?

cfleming09:10:13

Is that the jar packaging tool that CIDER uses?

Alex Miller (Clojure team)12:10:31

@mbertheau: I would like map-keys and map-vals to end up in the stdlib. The other ops are unlikely though.

calvin9112:10:34

Hello everyone !!!

calvin9112:10:51

I'm currently using pedestal but I don't know how to post params in it. I'm using the response-for function from io.pedestal.test. How suppose I test the routes in repl ?

mbertheau13:10:12

@alexmiller Then do you know of a concise way to construct a seq or vec out of several parts, where parts may be omitted or have different lengths? Right now I'm using a (-> [] (conj foo) (into (when cond? [bar baz]))) style.

mbertheau13:10:04

And (-> [] (conj foo) (into (when cond? [bar baz])) seq) if I need a seq, which works but isn't very readable.

Michael Griffiths13:10:26

@mbertheau: how about

(cond-> [foo]
  cond? (conj bar baz))

mbertheau13:10:47

@cichli Interesting! Didn't know about that one.

bronsa13:10:43

a pattern I've been using in t.e.jvm is `[foo ~@(when cond? [bar baz])]

bronsa13:10:01

which is rather concise but not really performant

tyler14:10:41

Are there any lein templates that have pedestal set up for code reloading from the get go?

camdez14:10:04

mbertheau: From what you described it sounds like you could just use concat: (concat [foo] (when cond? [bar baz])). If you really want a vector, call vec on the result.

mbertheau15:10:54

camdez: That should be it! Now I just wonder if I tried that and discarded it for some reason I don't remember now or if I missed it. Anyway, thanks! Very simple simple_smile

camdez15:10:29

mbertheau: No problem! The thing you may run into is that the arguments must be sequences, not scalar values. So make sure to wrap individual items into sequences.

Alex Miller (Clojure team)15:10:38

or the transducer version of that :) (into [] cat [[foo] (when cond? [bar baz])])

Pablo Fernandez15:10:03

I’m using environ, is there a way to get the name of the environment being used?

sveri16:10:46

Hi, When I define a prismatic schema in a .cljc file, it does not get recognized from clojure side. Any ideas why that may be the case? Is this supported at all?

blandinw16:10:10

re ?> and ?>>, we stopped using them when cond-> and cond->> came out

Alex Miller (Clojure team)16:10:19

@sveri: are you using Clojure 1.7+?

camdez16:10:34

pupeno: A couple months back I was waaaaay down this path and could have told you with certainty, but IIRC, the answer is no. But it might be worth pointing out that using knowledge of the environment (that is, ‘dev’, ‘prod’, etc.) is against the spirit of 12 Factor, environment based configuration.

camdez16:10:14

(Though often convenient.)

Pablo Fernandez16:10:49

camdez: but without knowledge of the environment, how will my exception tracker now whether to put the exceptions in the production or staging buckets?

camdez16:10:47

pupeno: The environment’s configuration will have an EXCEPTION_BUCKET value (or similar) rather than using ENV to make the decision within the app.

camdez16:10:13

pupeno: Does that make sense? The word “environment” is a bit overloaded here.

Pablo Fernandez16:10:11

Yes, it does make sense, but I think it can get a bit verbose. I’ll just add a :name to my environments and use that.

camdez16:10:50

pupeno: I agree that it can get verbose. But it makes things less “magical” (i.e. easier to reason about), and you usually appreciate the fact that you did these things when you want to (say) set up a second staging environment and not have the two instances clobbering each other’s external data.

Pablo Fernandez16:10:49

camdez: the couple of times I set up another staging environment, it was not called staging.

sveri16:10:19

@alexmiller: When I restart the REPL the changes are recognized. However, resetting everything using tools.namespace they are not

Pablo Fernandez16:10:47

camdez: I see your point. I just don’t want to end with 5 keys all pointing to :dev when they are more or less equivalent.

Alex Miller (Clojure team)16:10:00

@sveri: sounds like it's past my realm of experience then - support for cljc was added to tools.namespace. Maybe you have an older version though?

camdez16:10:26

pupeno: I certainly understand where you’re coming from, just presenting other considerations. To me, it feels a bit like using the convenience of global state assuming you’ll only ever have one of something. And often you get there and wish you’d used Component or something, ya know?

Pablo Fernandez16:10:49

Yes, I know what you mean.

camdez16:10:55

Gah, sorry I can’t type today. Typo city.

Alex Miller (Clojure team)16:10:03

@pupeno I think Immuconf is a much more flexible config choice and it's not well known enough

Pablo Fernandez16:10:31

alexmiller: does it integrate with environment variables?

Alex Miller (Clojure team)16:10:00

it's really focused on storing your config in edn maps

Alex Miller (Clojure team)16:10:11

one or more edn files that is

Pablo Fernandez16:10:21

Sounds not very Heroku friendly.

Alex Miller (Clojure team)16:10:25

but you can use environment vars to pick the ones used

roberto16:10:02

yeah, I set the env vars in the uberjar profile using System/getenv

roberto16:10:13

works fine in heroku

roberto16:10:49

example:

:uberjar-env-vars {:mongo-uri      (System/getenv "MONGO_URI")
                                :db             (System/getenv "DB")
                                :default-admin  (System/getenv "DEFAULT_ADMIN")
                                :client-id      (System/getenv "CLIENT_ID")
                                :client-secret  (System/getenv "CLIENT_SECRET")
                                :oauth-callback (System/getenv "OAUTH_CALLBACK")
                                :auth-url       (System/getenv "AUTH_URL")
                                :token-url      (System/getenv "TOKEN_URL")
                                :profile-url    (System/getenv "PROFILE_URL")}
             :uberjar          [:uberjar-common :uberjar-env-vars]})

camdez16:10:58

alexmiller: Thanks for sharing that! I’ve never quite loved how environ did things, but I’d never heard of Immuconf.

Pablo Fernandez16:10:04

Well, in this case, environ picks up the variables automatically. I like that environ does that. Other than that, I don’t know enough to compare.

roberto16:10:39

@camdez: it is mentioned in his book also.: Clojure Applied

camdez16:10:17

roberto: Cool, thanks!

Alex Miller (Clojure team)17:10:40

yeah, Clojure Applied talks about both Environ and Immuconf and compares them

gary17:10:16

Clojure Applied is now on Safari Books Online 😄 so now I get to read it at work.

Alex Miller (Clojure team)17:10:36

yay :) I think somehow I'm supposed to get a cut of that but who even understands royalties

gary17:10:43

Yeah I always wondered how that works. I'll send you some stats about how many times I access it so you can try to correlate it 😃

Alex Miller (Clojure team)17:10:30

correlate it with what? it's not like I have any data. :)

Alex Miller (Clojure team)17:10:13

every 3 mon a random amount of money flows into my acct, who even knows

gary17:10:45

interesting

Alex Miller (Clojure team)17:10:53

actually, pragmatic gives us very good real-time data about direct purchases on their site

Alex Miller (Clojure team)17:10:17

it's all the other stuff that is ambiguous and received at a great lag

Alex Miller (Clojure team)17:10:01

I'm going to pragmatically assume it's enough to buy a tesla though

Alex Miller (Clojure team)17:10:37

(probably closer to enough to buy a nice bike)

gary17:10:32

haha well I'll definitely buy a couple extra copies directly from prag to raffle off at my next clojure meetup. especially if part of the proceeds will be going toward the purchase of a tesla =p

gjnoonan17:10:07

If I buy a tesla, can I have a free book? 😛

christianromney17:10:13

+1 for immuconf. I have a single envvar now to choose which files to load

Alex Miller (Clojure team)17:10:09

@gjnoonan: if you buy me a tesla, you can have a free book :)

sveri18:10:36

@alexmiller: Thanks, I explicitly added the latest tools.namespace version and it seems to be working now

Pablo Fernandez18:10:42

I have a middleware to force redirect to SSL in compojure. This should be the outermost middleware, right? to redirect as soon as possible.

tranchis20:10:27

has anyone encountered this error? Caused by: java.io.FileNotFoundException: Could not locate clojure/tools/reader/impl/ExceptionInfo__init.class or clojure/tools/reader/impl/ExceptionInfo.clj on classpath.

tranchis20:10:21

this is weird, it comes from a dependency that does not appear in lein deps :tree or :plugin-tree but gets automatically downloaded by lein repl/run

camdez21:10:28

I kinda just wish if required an /else/ form.

bronsa21:10:36

@tranchis: haven't seen that one in a long time

tranchis21:10:00

I think I’ve fixed it

bronsa21:10:03

it's usually caused by libs pulling in older version of tools.reader than the one required

tranchis21:10:20

lein clean + commenting all dependencies and adding them one by one taking care of the exclusions suggested

sashton23:10:09

just wanted to get some feedback on something. is it reasonable to put all database queries behind a protocol, then implement that protocol for sql queries. then also reify the protocol in tests to have total control over the query responses, so that they can be effectively mocked? Example here:

; all the db queries go here
(defprotocol DB
  (create-user [x username])
  (find-user [x username]))

; sql implmentation
(defrecord SQL-DB [conn]
  DB
  (create-user [x username]
    (jdbc/insert! conn :users {:name username}))
  (find-user [x username]
    (jdbc/query conn ["select * from users where username = ?" username])))

; use in production code
(let [db (map->SQL-DB {:conn (get-conn)})]
  (create-user db "steve"))

; use in test code
(let [db (reify DB
           (create-user [x username] {:username username :id 1}))]
  (is (= {:username "steve" :id 1} (create-user db "steve"))))

bfabry23:10:21

that looks quite a lot like @stuartsierra 's component usage @sashton, so I'd say yeah it's pretty standard

sashton23:10:51

yeah, that's where i was going to go next with it, if there aren't any problems with it

bfabry23:10:14

that's exactly how we abstract our db server anyway, only we implement component/ILifecycle in the record as well