This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-02
Channels
- # aleph (5)
- # beginners (112)
- # boot (137)
- # cider (10)
- # cljs-dev (36)
- # cljsrn (2)
- # clojure (118)
- # clojure-argentina (1)
- # clojure-berlin (1)
- # clojure-brasil (3)
- # clojure-dev (4)
- # clojure-italy (2)
- # clojure-nl (13)
- # clojure-russia (23)
- # clojure-spec (5)
- # clojure-uk (53)
- # clojurescript (344)
- # clojutre (1)
- # core-async (65)
- # cursive (9)
- # datascript (7)
- # datomic (28)
- # devops (1)
- # emacs (16)
- # events (1)
- # jobs (5)
- # keechma (18)
- # lumo (56)
- # off-topic (7)
- # om (3)
- # onyx (14)
- # protorepl (21)
- # re-frame (3)
- # reagent (20)
- # ring (12)
- # ring-swagger (9)
- # specter (17)
- # unrepl (14)
- # vim (14)
- # yada (22)
well 100 numbers is definitely more than 120 wide (or is it counting “items” rather than string length?)
good morning! core.cached question:
(if (cache/has? C :c)
(cache/hit C :c)
(cache/miss C :c 42))
how is it possible with this code that :c
will definitely survive in between /has?
and /hit
? would think they’d have to be wrapped together in some way (macro?) for that to happenor does /has?
under the hood flag for holding until /hit
(require '[clojure.core.cache :as cache])
(def C (cache/fifo-cache-factory {:a 1, :b 2}))
(if (cache/has? C :c)
(cache/hit C :c)
(cache/miss C :c 42))
C looks kinda atom-esque
the java api is not that bad actually (that lib only makes it edn-configurable and saves type hinting here and there)
@micahasmith The way core.cache is used in practice isn’t well documented IMO
Typically you have something like:
(defn with-cache [cache key value-fn]
(-> cache
(swap! #(cache/through value-fn % key)
(cache/lookup key)))
through
uses that has?
/`hit`/`miss` logic
It’s just for updating the cache
If you put the cache in an atom, it can be updated using swap!
, which conveniently returns the immutable cache as a value.
This return value can then be used to lookup the key.
is there a version of the EDN reader available as a standalone library, that I could read EDN files with namespaced maps in clojure 1.8?
A standalone library instead of the clojure.edn namespace?
right
I'm not sure I understand the second half of the sentence - what's wrong with using clojure.edn?
if I use clojure 1.8 in my project.clj, then I get the 1.8 version of clojure.edn, which doesn’t understand namespaced maps
I produced a bunch of data from another project using 1.9, and it’s full of namespaced maps
is that an EDN reader, or a clojure code reader?
I’ll check it out, thanks
Hi guys! Clojure noob here so please be gentle:D I’m playing with Ring/Compojure-api to try and get some understanding how one would build an API in clojure. The question I have is this: I have a function in a db namespace called db/find-user. This function depends on external database (datomic in this instance if it matters). When consuming this function at the service/handler level (or in other places) what would be the most clojure way to deal with this? Should I pass the db/find-user function as argument into functions that need to consume it (end upwards on the stack) or do I just take the hit and use it as a “hidden” dependency? Example of what I mean:
(ns example.core
(:require [example.db :as db])
;;1. hidden dependency way?
(defn vaild-user [credentials]
(let [user (db/find-user credentials)]
(do-more-with-user user)))
(defn function-that-uses-valid-user [credentials]
(valid-user [credentials]))
;;2. passing dependency as argument
(defn valid-user-2 [find-user credentials]
(let [user (find-user credentials)]
(do-more-with-user user)))
;;this seems to have a disadvantage as I have to move db/find-user upp the stack
(defn function-that-uses-valid-user [credentials] ;;and this potentially needs find-user as an argument if it gets called upwards on the stack
(valid-user-2 [db/find-user credentials]))
Sorry if it’s a stupid question 😞What we tend to do is have the system configuration in a Component and have middleware that adds that to the Ring request.
Ok. So you do move the dependency to the uppermost level on the stak and pass it down the levels.
So handlers can get (:my-app/config req)
and inside that there’s a :my-db
which is a pooled datasource.
The alternative would be packaging your “data tier” as a Component and passing that around (easier to mock perhaps for testing).
But, yeah, pass everything in via the request and set it up in middleware would be my preferred approach. @weavejester you were about to suggest something? Would love to hear your recommendation as the creator of Ring.
I was typing something, but got distracted 🙂
@weavejester has a recent blog post which covers this with Duct i believe
@nadejde by db/find-user
, do you mean the function itself or the database connection?
Or a closure wrapping find-user
with the database connection?
And by credentials
you mean the user’s username and password?
So this is a user authentication?
yes. in this case it would be. It can be anything else. I just picked this as an example
In the basic case I’d write something like:
(defn user-endpoint [db]
(GET "/user/:id" [id]
(find-user db id)))
So I have a function that returns a handler/route
By using a closure we can pass the db connection in once, and also perform any setup we need.
I’ve also taken to writing protocols around my I/O boundaries
(defprotocol Users
(find-user [db id]))
(extend-protocol Users
foo.component.Database
(find-user [{:keys [spec]} id] ...))
This loosens the coupling between the handler and the database. When testing I can replace the real database with a stubbed or mocked one.
It’s also useful when communicating with external APIs. During development you can subtitute in a local API that mocks out a cloud service.
This approach works well with Component or Integrant.
@robert-stuttaford mentioned Duct, which is currently in alpha, but I’ll release a beta today. That’s a more opinionated framework built on top of Integrant, but follows the same basic ideas of passing dependencies as arguments, and using closures.
Thank you very much for you help and suggestions! I will now go and research about what you guys said for a couple of weeks:) I’ve not used Component or Integrant before. But I hope I can figure it out! Thank you again!
@nadejde The information on the Duct page is currently for the soon-to-be-outdated stable version. You might want to look at https://www.booleanknot.com/blog/2017/05/09/advancing-duct.html and https://www.booleanknot.com/blog/2017/05/29/building-web-services-with-duct.html if you’re interested.
will do this @weavejester thank you again
One more if you might please? For learning my way into this what would you recommend? Start low level with Ring and try to understand how to use all the different pieces together or through something like Duct and work my way down?
@nadejde I’m a fan of working my way from the bottom up if understanding is your goal.
Sometimes that’s not the most expedient way to get started
But it does lead to the greatest understanding
On the other hand, if you’re not doing something practical, it can be boring, and when things are interesting people learn faster.
Thank you. Will try and see how it goes with the buttom up. When I get bored look into a framework:) cheers!
@weavejester Thanks. I hadn’t thought of functions-that-return-routes — because we have a lot of routes that would need this, but it’s an interesting approach for small apps.
@seancorfield It works for larger apps too, but it tends to require something like Component or Integrant to manage the dependencies.
We have a lot of dependencies (and we’re using Component) and a lot of routes, so having the middleware as the closure-over-system and injecting it into the Ring request seems simpler than having a function-per-route?
My colleague decided to use Bidi instead of Compojure in the app he’s working on and that seems to decouple handlers more completely but I haven’t had a chance to dig into that yet.
@mping Built a realtime chat with aleph the other day
It's great!
I’m trying to remember the name of a clojure debugging tool that would basically print intermediate values in an expression; ISTR it was even in the stdlib, but I can’t find it anymore
I have a function that has stuff like (not= some-expr some-other-expr)
and it just tells me “true” which is useful but I’d love to know what the exprs were 🙂
@joelsanchez source code or it didn't happen...
I kinda want taoensso’s spy I guess, but I don’t want it to go to stdout; I want it as data
@john Of course! Making a gist
Don't expect it to be perfect or full of features, it basically ensures that the message gets read by the target
Holds the messages until they are read
Nothing more
I made this to implement chat for a cordova app
It also communicates with PHP through zeromq, in another namespace, but who cares
Honestly most of the code you see is copied from aleph's documentation
I was considering several JS options (socketcluster, deepstream, plain old socket.io...), using ejabberd, learning Elixir like a real man, or doing it with Clojure
You know what I picked
I'm writing a DSL for writing C code in Clojure. Long story. What's a good way to represent array access and dereference in Clojure? i.e. a[20] and *b
use deref (@) to replace *
you can extend dereffable