This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-01-08
Channels
- # admin-announcements (19)
- # announcements (1)
- # beginners (70)
- # boot (192)
- # braid-chat (55)
- # cider (3)
- # cljs-dev (42)
- # cljsjs (2)
- # cljsrn (25)
- # clojars (23)
- # clojure (162)
- # clojure-brasil (3)
- # clojure-czech (18)
- # clojure-russia (10)
- # clojurecup (3)
- # clojurescript (63)
- # code-reviews (6)
- # community-development (340)
- # core-async (7)
- # cursive (4)
- # datomic (20)
- # events (2)
- # funcool (3)
- # hoplon (20)
- # jobs (33)
- # ldnclj (11)
- # lein-figwheel (9)
- # leiningen (6)
- # off-topic (1)
- # om (79)
- # proton (4)
- # re-frame (39)
- # ring-swagger (4)
- # slack-help (2)
- # yada (2)
Hi hivemind, I wanna [1 1 1 1 10 1 1] to be [ (1 1) (1 1) (10 0) (1 1)], is there any good way to do?
@lambeta: I would use replace
to turn every 10
into a 10 0
pair, cat that together, then use partition
for the final result
there are two challenges: 1. what code will do the job 2. what code communicates the best (ignoring performance requirements)
if it weren't then you'd end up with a (1 10) (0 1)
combo of pairs in your result (with the code I posted)
So, still my question remains-- how is an or
(a branch), a map construction, and a lookup within that map better than just an if
(a branch)?
I made the assumption that there was more to this question than meets the eye and responded with how I think about transforming data.
ahh, yea. the skeleton is there either way-- which road you take could vary depending on the particular data involved.
Well, you can take the question at face value (though yes it is a bit ambiguous) and within that fixed structure, then you can compare codes for 1) readability and 2) performance. I would sacrifice performance for readability but in my opinion, the code you posted is worse on both axes (in absence of any extra requirements which your framing of the solution would help with)
There are patterns (ala the gang of four patterns and christopher alexander) even in functional design
Right. So what I did was look at my real code to see why I was thinking about the problem the way I was and this is what I found:
(defn rewrite
"Returns a successor, which must be a vector or a function. If no match is
found in the rules mapping, the original module is return within a vector."
[rules m]
(or (rules m) [m]))
(defn rewriting
"Returns a rewriting transducer."
[axiom rules generation]
(map (partial rewrite (get-rewriting-rules axiom rules generation))))
Yea, if I had those functions in my toolbag, I'd certainly use them. I'm a fan of bottom-up design (which I think most LISPers adhere to).
hi all, first time to #C03S1KBA2… i’m dipping my toes into the clojure waters and i’ve got a question about the style guide at https://github.com/bbatsov/clojure-style-guide, specifically about indenting… the guide suggests indenting by 2 spaces the bodies of any forms that have body parameters… I understand why a defn/defmacro form has a body parameter, but what is a body parameter when calling a macro? e.g. the good when
example at https://github.com/bbatsov/clojure-style-guide#body-indentation
@j1mr10rd4n: welcome
@j1mr10rd4n: ^ read that
then click on the "source" link off to the right of the function definition on that page
(defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
{:added "1.0"}
[test & body]
(list 'if test (cons 'do body)))
well, any argument(s) you pass after the first one are bound to the symbol “body”, is it that simple or am i missing something?
What editor are you using? One like Cursive/IntelliJ does nice indenting/formatting for you.
but that would imply, as per my understanding of the style guide, that had the macro author decided on a different name for their arguments “predicate” and “when-true” say, any code calling the macro should be differently indented - or is there a convention to say when a macro argument should be called “body”… i guess i’m trying to ask if there is a more concrete definition of what is a “body-parameter” than a parameter that the author decided to name “body”
<- vim
I think you are reading too much into this, but these are good questions. There is the notion of the "body" and the convention of naming the parameter body
.
probably reading too much into it - it was spurred by the thought of “how do i tell vim about this rule?"
and trying to explain it to myself first
editors don’t bake themselves 😉
I prefer the Cursive plugin for IntelliJ IDEA myself. Lots of emacs users here too. It's all good.
but the indentation is slightly different from the style guide
there is an #C050AN6QW channel here where you might find vim people who could offer tips
excellent, thank you
@j1mr10rd4n: check options g:clojure_fuzzy_indent and local lispwords. Here is what I use: https://github.com/Deraen/dotfiles/blob/master/.vim/after/ftplugin/clojure.vim
I wonder what the best way to generate a class that can be accessed from java would be. I need to create a .war file that contains a class which implements a certain interface and is instantiated from a Java application. It seems the :gen-class and AOT compilation would make the most sense, but maybe I can also place a reify expression on the top level of a file and AOT that. Does anyone have advice?
@hans http://clojure.github.io/clojure/javadoc/clojure/java/api/package-summary.html
clojure 1.6 added niceties for this
2nd answer on this SO post has some guidance too http://stackoverflow.com/questions/2181774/calling-clojure-from-java
Right, I've seen that SO post and as there were multiple answers claiming that they would be "best", I thought I'd ask here to confuse me more
No, but really - The clojure.java.api method seems to require that I need to be called first and then compile the required classes, or am I wrong?
start with clojure.java.api and see where that takes you; it’s the newest capability the Clojure language offers
i’m not sure. i’m kind of winging this answer because i’ve not done this myself
i jumped in mostly because i knew that 1.6 added stuff for this. good luck
@hans: not sure if I understood your question correctly, but I have had some success with a pattern like this for interacting with a Clojure library (included as a standalone JAR) from a Java application: https://gist.github.com/jstaffans/747504f0150c8ada0e1d
@jstaffans: Thanks, exactly that was what I was looking for!
How much of the way that libraries like medley
and plumbing
or useful
lay do transducers go?
@mbertheau: Can you rephrase that question? I don't understand it.
I’m running a long-running task in background from my REPL. It does println during its lifetime and that breaks my REPL prompt. I have control over those println calls. Is there a way how to do println, but restore REPL prompt somehow?
@meow: I mean that these libraries exist and provide functions for manipulating maps like filter-vals and others, which could reasonably expected to be in core. To implement the corresponding functionality with core functions would mean verbose and rather unreadable code. Now concerning transducers I only watched Rich's talk about them, but never used them in practice. I wonder if transducers make many of the functions in these utility libraries obsolete, in the sense that with transducers you can implement their functionality succinctly and readably.
@mbertheau: Gotcha. I've used transducers a fair amount. I don't think they take away the need for those libraries.
Transducers really only apply when you are transforming data in a single pass and you want to apply multiple small transformations to that data.
Transducers provide a nice mechanism to compose those transforming functions and apply them to collections without concern for the type of collection.
There are plenty of times where I need to manipulate data in a way that doesn't match what I just described and so I don't use transducers there because they don't apply, and so I still might have a need for the kinds of functions that exist in those libraries you mentioned.
@meow: Hmm, ok, thanks. I should try out transducers anyway, so I know when they're useful.
I highly recommend taking a look at the into
function which can take an optional transducer.
@cddr Yes, single stream of commands from web tier to Kafka topic pending-commands
. Then business logic processes that stream and writes to either accepted-commands
or failed-commands
topic. You could certainly have different jobs on different stream consumers for each type of command, and rely on filtering to only present the desired command types to a given unit of business logic.
@meow: If you have a little time, do you have any insight into how I could make this function more readable? https://gist.github.com/mbertheau/ebdf995dc3c635e1f55e
Do you have enough throughput that you need to partition the command log? If so what do you use for the partition key?
@cddr: total ordering in Kafka is only guaranteed within a partition, so a given command log would have to be single partition
throughput per partition is quite high in Kafka (see various benchmarks on the web), but you could give orthogonal command types separate command streams if throughput becomes a problem
Hey, gang, is it better to do database access wrapped in a with-open
binding or just keep a connection open at all times bound to a var? The database is small and hosted on the same machine as the web server.
How can I get (partition 2 1) as a transducer? partition-all is a transducer, but that’s not what I want.
lvh: nothing in core does it, but https://github.com/cgrand/xforms does
@mbertheau: Not sure I'll be able to do that. Lots going on.
@mbertheau: there is also #C053PTJE6 channel
@ghadi, thanks, by the way; I didn’t even think about handling the connection via Component.
you wouldn't even have to handle the connection/pool with component, just the spec for the connection/pool. You'd still with-open
for scoped usages
Anyone have any suggestions for automated benchmarks in a leinengen project? I’ve been playing around with criterium (https://github.com/hugoduncan/criterium) and perforate (https://github.com/davidsantiago/perforate) which work hand in hand, but it seems perforate hasn’t had any active development for more than a year so I’m hesitant to start relying on it too heavily. Is there a more prominent library out there for this? or how do others usually solve this?
1) don't use leiningen while benchmarking if possible. If not possible 2) Always use :jvm-opts ^:replace [...blah]
the way we benchmark clojure itself is usually java -cp ... clojure.main -m thenamespace
I have a general question, I believe it applies to both Clojure and ClojureScript. You, experienced guys, when you start a project do you have to deal with a lot of boilerplate? I'm coming from js/nodejs world which is getting more complex every day, what is you feeling about cl/cljs?
@esnunes: I find it much, much simpler to navigate the start of a project than I would in the JS world. Still can be difficult, and there is a pretty significant amount to learn at the start, but it doesn't seem to have the crazy explosion of extra choices and complexity as the JS world does to me.
thanks @shaun-mahood , I don't mind learn a lot of things in order to develop something, what bothers me is the amount of source-code I have do create (or copy n paste) to bootstrap a project
@esnunes: It feels like very little to me. For cljs, it's worth starting with the figwheel/devcards templates. In clj, Luminus is a popular template but I prefer starting from a bare project and adding things as needed.
@ghadi: whats the purpose of the jvm-opts update if using leinengen? is this just due to leinengen defaults that aren’t optimal to benchmarking and production code? If we already are altering the jvm-opts to better match our production config in our profiles, is that sufficient? Or am I missing something?
lein disables hotspot, basically. And you need the ^:replace metadata to avoid that flag.
jarredlhumphrey: yeah you hit it. if you don't specify ^:replace
your opts are merged with lein's opts. You should always control things explicitly
ghadi: awesome thanks man
and without hotspot, your profiling won't reflect an optimum server runtime
noisesmith: gotcha.. I’ll have to dive more deeply into the default jvm-opts lein uses. thx
jarredlhumphrey: you shouldn't be running lein at all on a prod machine anyway, that's what uberjars are for
cool, jps is a trick I'll need to remember
lots of the jvm management things are great. jstack
when stuff hangs/deadlocks, jmap
jdump
when you want to snapshot heaps
noisesmith: yea we def dont run lein on our prod servers 😉
ghadi: awesome I’ll dig into those tools
@jarredlhumphrey: you'd be surprised how many people do, so I try to make a point of mentioning it
I have a sequence of strings ("line 1" "line 2" "line 3") and I want to add "line 4" to the end of it - whats the most pragmatic way
@sharms: I believe that if you have a vector
, conj
will add to the end of it. If you have a list
, it’ll add to the front of that. That’s how these data structures are optimized
just didn't know if there was a better way - I created the seq by using line-seq from a BufferedReader
@esnunes: as far as boilerplate goes I think that you will find that clojure has little to no boilerplate at all, depending on the type of app you are developing. When you start using various libraries and frameworks you might have more or less, but on the whole it is less that I have ever experienced in about 30 years of programming
Am I missing some core function that takes a value and a function calls the function for side-effects on the value then returns the original value?
seems like that's a thing that would exist and I want it regularly… easy enough to write (if I could figure out what the heck to call it)
an lovely idiom i picked up from a former coworker:
(doto 'my.application require in-ns)
anybody know of a macro that's a mix of cond
and a bunch of if-let
? something of the form
(cond-let
[var1 (some-test)] (blah var1)
[foo (test-bar)] (do-something foo)
etc)
where it finds the first binding that's not nil and returns result of the corresponding clause.... is this making any sense? is there a concise way to do this using existing macros?