This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-22
Channels
- # alda (1)
- # aws-lambda (23)
- # beginners (27)
- # boot (156)
- # business (2)
- # carry (4)
- # cider (1)
- # cljsjs (2)
- # cljsrn (29)
- # clojure (170)
- # clojure-austin (35)
- # clojure-czech (8)
- # clojure-dusseldorf (5)
- # clojure-italy (4)
- # clojure-nl (1)
- # clojure-quebec (2)
- # clojure-russia (45)
- # clojure-spec (49)
- # clojure-uk (12)
- # clojurescript (81)
- # component (5)
- # datomic (24)
- # devcards (26)
- # emacs (4)
- # hoplon (4)
- # jobs (1)
- # juxt (5)
- # leiningen (6)
- # luminus (14)
- # mount (26)
- # om (27)
- # om-next (2)
- # onyx (22)
- # pedestal (2)
- # planck (3)
- # proton (5)
- # re-frame (19)
- # reagent (2)
- # ring-swagger (60)
- # spacemacs (12)
- # specter (8)
- # untangled (119)
- # vim (61)
- # yada (36)
Thanks @jeremyraines
I am pretty confident that the author was referring to the style of my example above, especially if he’s referring to functional programming, but like @idiomancy says we can’t tell for sure without more context
I’m having a hard time getting used to component (and duct). So my whole system is started when the application boots up, and Duct is glueing components with endpoints. So I have a “processor” namespace that do some queries on the database… how should i inject the db there?
or better explained, do i have to then, in my endpoint, call the process function passing on the db :spec and then keep repassing to a lower function within the other namespace?
I'm not sure what you mean by an endpoint, but a database connection is a component/resource that other parts of the system depend on.
@grzm yeah… it didn’t make sense for me to create a component for my processor, as that would make me put on the system map on boot time, and i might be wrong, but then it looks like i have one instance of a processor record on my process...
How are the functions in your "processor" namespace being invoked? (i.e., where from?)
From inside an endpoint function? Which will have access to the component that contains the database connection, yes?
very simple, as my endpoint is doing something as simple as: (future (processor/process))
In my database component I have the queries that utilize the connection. Then the processor depends on the database component and calls the queries on the component.
So when you call (processor/process)
pass in either the component containing your db connection (so processor
is parameterized on a Component) or pass in the db connection itself (so processor
is parameterized on a db-spec).
(processor/process (:db system))
or (process/process (:db-spec (:db system)))
Yeah, the processor would be a component, too. One that depends on the database component
The processor does not need to be a Component, but its functions need to be passed a Component.
if I create a component for the processor, that means one instance of Processor will be there right? are there any problems with race conditions on that?
I wouldn’t do that.
I would just write the processor
functions to expect a (database) Component as one of their arguments — or a db-spec which the caller gets from the (database) Component it already has.
If the processor
namespace has no "state" of its own that it needs, I wouldn’t make it a Component.
@seancorfield it doesn’t.. but let’s suppose it does.. then i create a component..
what happens when 2 different threads call (process {:processor system}) at the same time??
At World Singles, our database Component has all our db connections in it (and our MongoDB connections) so we pass the whole database Component into functions that might need to access a database.
@fenak I’m not sure what you’re asking...
(which would have a dependency on a Database Component yes?)
(assuming Processor is truly a singleton in your system)
Are you concerned about mutable state within the Processor record? An atom
in there?
that’s what i wanted to ask then.. if it’s not a singleton, it should not be a component..?
Right. Component = singleton in essence.
hmm… i don’t think i’d have any mutable state.. i mean, just a function that runs a query and chuck the result in S3.
Otherwise you’d just create a hash map (or record) on the fly and pass it around.
So Processor
could exist as a Component that has dependencies on Database
and S3Storage
— Database
would start/stop/manage your database connection pool; S3Storage
would get the S3 configuration details at startup perhaps. Does that sound reasonable?
Then your endpoints (handlers) would need to pass the Processor
component into functions in that namespace: (processor/process (:processor system))
And you’d have an overall Application
Component (`system`) containing :processor
, :database
, :s3
, etc that you build at startup and let Duct weave into the endpoints…
my processor ns functions right now doesn’t have s3 storage as a component.. it just calls a function on files.clj that calls s3/put-object.. and that handles it..
it’s not a component, but it’s opening a connection and running stuff directly from the files/send-to-s3 function..
Then you could test your processor independent of your endpoints by having your tests create Processor
and pass it into functions as needed.
I’d probably have an S3 Component to encapsulate the configuration so nothing is hardcoded
So you could then reuse that S3 files code with different credentials etc more easily.
Then maybe your env
should be a Component?
(we have our "environment" as a Component at World Singles so it can populates its configuration at startup)
I assume that’s a library that "wraps" environment variables?
Think about testability tho’ — do you want to have your tests around the S3 stuff rely on environment variables? Better to encapsulate that in a Component that your tests could provide perhaps?
in ruby or java i’d just mock/stub the s3 bits.. but in clojure i don’t know yet what i’d do.
i have a function like that that i find very hard to think on how to test.. that’s maybe too poorly designed
(defn content-from-s3
([]
(files-from-s3 s3-bucket rules-dir))
([bucket dir]
(let [s3-objects (->> (s3/list-objects bucket dir)
:object-summaries
(filter #(re-matches #".+\.json$" (:key %)))
(map #(s3/get-object bucket (:key %)))
(map :input-stream))]
(map load-content s3-objects))))
I'd say that's doing too much in one function and I'd refactor it into several smaller functions.
You can follow a TDD workflow with Clojure... which should stop you creating untestable functions, just like should in Ruby / Java with OOP...
Does that help at all @fenak ?
@seancorfield yeah, definitely..
Look at with-redefs
.
@fenak maybe this is not Clojure related per se. We're doing a lot of S3 related stuff with Clojure. Instead of mocking those s3 calls using
with-redefs
we typically use a fake server https://github.com/jamhall/s3rverThis is how you can use it:
(doto (AmazonS3Client.)
(.withEndpoint custom-end-point)
(.setS3ClientOptions (-> (S3ClientOptions.)
(.withPathStyleAccess true))))
hi guys
is there clojure equivalent to re.sub python ?
@abdullahibra not sure the semantics are exactly the same, but have you checked out clojure.string/replace
?
val_waeselynck what i want to do exactly is: apply method which will work on lower-case words then return another lower case words but i want to preserve the original format
I think I'll need an example 🙂
as example: input "Hello;world!" get-words -> ["Hello", "world"] lower -> ["hello"," world"] apply_X_method -> [hell@, w@rld] final -> "Hell@;w@rld!"
that's one example what i need is general solution
the format is changeable
@abdullahibra ok, so basically split your string into an interleaving of word/non-word substrings, and apply a modification only to the word substrings
is that correct ?
oh and there's the lower-case constraint too
@abdullahibra don't know how to deal with lower-case, but clojure.string/replace-by
seems close to what you're trying to achieve
val_waeselynck there is no replace-by
@abdullahibra I'm on clojure 1.8, you ?
oh 1.7, will update
is there docs for it in clojure ?
replace-by
is not public
I was looking for it here, but can't find it
But it sounds like what you want to be done can be constructed easily. Maybe. I just don't understand your example. Does it show what happens inside the function you desire?
clojure.string/replace
can take a fn as replacement though
@abdullahibra I find the part about operating on lower-case strings but returning (possibly) differently cased strings a bit weird. What if X_method affects casing?
Here's one implementation (concocted in and to be pasted into a REPL, though it'd work fine in a file if you fix the require
) :
(require '[clojure.string :as str])
(defn rb [input f]
(let [delimiter ";" ,
words (str/split input (re-pattern delimiter)) ,
lower (mapv str/lower-case words) ,
after-fn (mapv f lower) ,
final (apply str (vec (interpose delimiter after-fn)))]
final))
... which does not return the original casing 🙂
called as such: (rb "Hello;world!" some-fn)
@reefersleep checkout as->
to reduce let
bindings 🙂
Cheers @mac 🙂 I thought I'd make it verbose and stick to "simple" functions in order to aid @abdullahibra , who may or may not be new to Clojure. But I can always use a reminder about handy core fns, I tend to forget them!
@wopeers there’s #liberator and #yada btw.
https://github.com/funcool/catacumba is also an option 😉
@niwinz I was just starting to compare catacumba, aleph etc. I might have some questions for you later :)
@niwinz What do you think about a model where you consume requests from a stream and put responses on a stream, instead of having to return something from within the route function?
@niwinz I want my handling of the request to go forward and deeper into the callstack instead of having to return a value right there in the handling function. I'm not talking specifically about catacumba.
maybe a handler get take a deferred or a stream as a parameter and pass it along, instead of creating its own and returning it.
At this moment you have the ring approach (composition of functions) and catacumba/pedestal (managed pipeline/chain/stream of handlers)
you want something like the catacumba/pedestal approach but handle the stream yourself
in this case and maybe I'm wrong, handling yourself the stream of the request will add additional boilerplate, that something like catacumba/pedestal approaches in more simple approach
Unfortunately I have to go right now, if you have a concrete questions about catacumba, feel free to post them to me directly or in #funcool channel 😉
Anyone know of a clojure/clojurescript compatible markup language or some such that could be used for marshalling one structure to another?
I'm effectively trying to expose the functionality of map
to users for custom mutations of data, ideally with a drag-drop editor, for example turning {:user_email "
into {:email_address "
without the user having to know how to code, and without having them write functions that we'd have to eval
at runtime.
Possibly something along the lines of https://github.com/joelvh/json2json
Nevermind, answered my own question when I found json2json. https://github.com/shaunlebron/hammock
I am trying "interactive development" with Clojure (on Emacs and CIDER). But I think I don't get it. My idea is to write a function, eval it, then call it from the REPL. Correct it, eval it and call it again. But there is no single command for this. There is one for evaluation, one for switching to REPL, then open last call and execute it. Then switch back to editor. This seams not right. Do I have a wrong idea about interactive development or is there a command like "eval buffer + send to REPL + execute last REPL expression"? Or should i write my functions directly in the REPL and copy them to my .clj file when it works? Is there any tutorial on how the interactive development workflow is done right?
if you’re using cider, there’s an eval from buffer command, which is what I tend to use
C-c C-c to eval it. ;; editing this. C-c C-p to print your expression in a separate bufer
@dhruv1 but if you’re writing a function, you’ll need some sample call to get a result
@bolivier and @witek if i understood it right, you want to define a function, compile it, and run that function in the repl with some arguments and then see if it’s correct
I think what @witek is saying is that the process for interactively writing a function isn’t smooth enough
eval function -> switch to repl buffer -> write test case for function in repl -> switch back to clj buffer and edit
that wouldn’t litter the clj buffer with junk (which I’m sometimes guilty of anyway)
right.
then what i use is:
C-c C-c
;; C-c C-c runs the command cider-eval-defun-at-point, which is an
;; interactive compiled Lisp function in `cider-interaction.el’.
write the function with it’s arguments in a clj buffer.
C-c C-p
;;C-c C-p runs the command cider-pprint-eval-last-sexp, which is an
;; interactive compiled Lisp function in `cider-interaction.el'.
if i still haven’t understood the question properly and the approach i’ve defined isn’t what you were looking for, i’ll shut up 😛
@bolivier explaind exactly my problem. Usually I edit a .clj file with functions calling helper-functions, etc. Then I have to test the main function. Since I jump in the editor between main function and helper functions I need to eval the whole buffer. Then I need to call the main function with some args. I can not realy imagine an other workflow which would work. So why is there no command for evaluating the whole buffer and run the last executed function call again? Or is there?
@witek I usually have a comment block at the bottom of my file that contains the tests I’m working with. So I can eval the file, then play around with the statements in the comment block by evaluating them. That way I always stay in one buffer.
@jcsims Of course, but I am completely new to Clojure and Emacs. And therefor I think my idea about the interactive development workflow must be wrong. Everybody should have the same problem, right?
@jcmoore Do you uncomment your tests when testing and comment them when done? Or is there a way to execute the commented tests?
I guess I normally work from the inside out - smaller, independent functions are iterated on, and then work out from there
But to execute the tests inside (comment ...) do I have to move to the bottom of the file? Or is there a command which executes it from anywhere? What is the command name? I am using Spacemacs, so I have different key bindings...
I think if you want to execute them from anywhere, you probably need to put them in a (deftest …) and eval the tests in that ns
There is a run tests for current namespace http://cider.readthedocs.io/en/latest/interactive_programming/
but I find that I’ll write a function, then I want to play with different inputs to observe it, so moving to the bottom of the file provides that context switch for me
another option, if you have a “main” that you want to evaluate after your buffer is loaded, you could make that call the last statement in your file
@witek When I first started with Clojure, I followed the blog post, Clojure for the Brave and the True, now a book. It has a good Emac and Cider primer. http://www.braveclojure.com/clojure-for-the-brave-and-true/.
I use the midje autotest functionality to have my (fast) tests run every time I change anything, from within the repl. I’m sure there’s some similar functionality for clojure.test
.
Autotest for Clojure.test: https://github.com/jakemcc/lein-test-refresh https://github.com/metosin/boot-alt-test