This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-03-29
Channels
- # beginners (14)
- # boot (83)
- # cider (13)
- # cljsrn (4)
- # clojure (240)
- # clojure-argentina (1)
- # clojure-berlin (2)
- # clojure-canada (1)
- # clojure-dusseldorf (1)
- # clojure-greece (2)
- # clojure-india (2)
- # clojure-japan (2)
- # clojure-russia (23)
- # clojure-taiwan (2)
- # clojure-uk (12)
- # clojurescript (138)
- # cursive (6)
- # datomic (36)
- # hoplon (245)
- # jobs-discuss (35)
- # lein-figwheel (4)
- # melbourne (2)
- # off-topic (1)
- # om (26)
- # om-next (2)
- # onyx (23)
- # proton (8)
- # quil (1)
- # re-frame (9)
- # ring-swagger (2)
- # untangled (10)
- # yada (6)
Don’t know how to phrase the question to ask google but I’m curious if Clojure has a way of stepping through the changes of a persistent data structure.
persistent data structures do not change by definition, maybe that is why google does not understand you .-)
lol totally, but I’ve seen demos in clojurescript of ‘undo’ behavior because of the underlying data structure
you can use (add-watch your-atom …)
and record all changes into another data structure, thanks to structural sharing, that history recording will be cheap
this might be relevant: http://swannodette.github.io/2013/12/31/time-travel/
ah so I could I could conj the value of every swap! of the atom
the swap! the index of whatever history I wanted into the current state
no problem, google for “undo” and “add-watch” and you should get some relevant blog posts as a result
yup this is the right path 😃
Is it possible to change things in the clojure.core namespace before running my code? (I was thinking about changing clojure.core/assert to have more information in the error message.)
Perhaps a hook which would (load-file "patch-assert.clj")
Okay, found lein :injections which seems about right.
Anyone can explain this?
user=> (loop [i (int 0) _ (println (type i))])
java.lang.Long
nil
user=> (let [i (int 0) _ (println (type i))])
java.lang.Integer
nil
I’m looking for articles dealing with JavaScript -> Clojure(Script). Specifically interested in hearing what worked/didn’t, rather than code conversion examples. Either my Monday morning Google-Fu isn’t up to the task or there articles that must be out there are poorly keyworded 😁 Would appreciate links/nudges!
@reborg: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L6270-L6282 this explains how that happens, as to why.. I could not figure it out. I suspect there's no good reason
thanks @bronsa. So my int cast is thrown away in the loop. I have a code snippet I’m trying to optimise and that’s one of the thing I couldn’t understand.
(defn find-index-1 [f coll]
(loop [n (int 0)
s coll]
(if (f (first s))
n
(recur (unchecked-inc-int n) (rest s)))))
(defn find-index-2 [f coll]
(loop [i 0
s coll]
(if (f (first s))
i
(recur (inc i) (rest s)))))
I was expecting find-index-1 to be faster, because of the (int) treatment. It’s not, it performs roughly the same as find-index-2
I think your best option is to use i 0
and unchecked-inc
. Although I'd assume the seq traversal overhead will dominate those primitive ops
@bronsa thanks for the hint. I was able to see the difference with the following:
(defn inc1 [n]
(let [n (int n)]
(loop [i 0]
(if (< i n)
(recur (inc i))
i))))
(defn inc2 [n]
(let [n (int n)]
(loop [i 0]
(if (< i n)
(recur (unchecked-inc i))
i))))
unchecked-inc-int makes things worse in this benchmark. Also the (int 0) in the loop doesn’t make any difference.
@reborg: had another quick look on that over lunchtime, I'm reasonably convinced that should be considered a bug. Care to open a ticket?
@bronsa sure. I cannot judge on the priority or impact, so expecting you to comment on that when I’m done
What was the lein-exec alternative that came out recently? It had a really bad pun for a name...
is there somethink like this: (take-nth [a b c d e f] 0 1 3) ==> [a b d]
in the stdlib?
Not Clojure-specific, but I'm thinking someone here will know What is the name of a position in x/y space along with an angle? Is there a term for it?
A vector describes a direction and length (or, alternatively, from-and-to coordinates). Is that what you meant?
@dominicm: you might be thinking about inlein https://github.com/hyPiRion/inlein
Does anyone know if it is possible to dispatch a multimethod on functions or funcallables? I come from CL and this is pretty straightforward but I have no idea how it might work in clj. Feels like CLOS with all of its limbs chopped off.
@tacosundae: When you defmulti
you define a function for dispatching on. Or am I misunderstanding?
Yes, but how to get at things like the arity of a function or the name if it is in a var and dispatch on that
@tacosundae: For functions defined with defn
, you can inspect the metadata on the var.
user=> (defn foo [bar] (identity bar))
#'user/foo
user=> (meta #'foo)
{:arglists ([bar]), :line 1, :column 1, :file "/tmp/form-init8119837949873121332.clj", :name foo, :ns #object[clojure.lang.Namespace 0x444a7e76 "user"]}
Thats perfect!
For a raw function, you can probably get something with Java reflection.
It might be worth specifying the interface as being via a symbol, instead of the function value.
hehe, no real use case. I'm new to clojure and exploring the landscape. Thanks for the help.
Can anyone explain when I should use lazy sequences with map/filter and when I should use transducers?
lazy sequences are an iteration interface, and transducers are a way of specifying reducing behaviors, so they are not entirely apples to apples
I have an existing transformation of a lazy sequence with several maps and filters. What can I achieve with transducers?
when you have a sequence of maps and filters, unless you build that using tranducers, you have intermediate lazy seqs between each operation
not sure I understand it... I thought lazy-seq consumes elements one by one for all the processing steps
transducers, instead of transforming data, transform the function you reduce with, so you end up with a single function so no intermediate sequences between each step
the lazy seq library (map, filter, etc) are really single step transformations, plumbed together via intermediate lazy sequences, where as transducers are a nice way to specify what you want in a way that fuses those steps together in to a big step.
but you can build a transformation using transducers and still go lazy-seq in and lazy-seq out if you want
because the transducers work has sorted of elevated the usefulness of reduce
there are now some interfaces and protocols that let you define "collections", "iterations", "views", or whatever you want to cal them, similar to lazy-seqs, but using reduce instead of first/rest
Interesting... And if I create core.async channels, I can pass a transducer to a channel creation function, right? But what to do with exceptions in transducer transformations?
channels can take an exception handler a long with a transform, but I would suggest making your transform total
Is there a name for the operation (zipmap coll (map f coll))
? Like group-by
, but the items in coll
become the keys instead of the values
actually it looks like the way the channel error handler works has the same effect as making your transform total
What about core.async/pipeline function? As I see I can create a couple of channels and use pipeline
to transform data with several processor cores. Is it work so?
yes, but there on several distinct flavors of pipeline depending on what you are doing, so be sure to read the docs
and pipeline-async is really odd, so if you do use that, be prepared for it to act in ways that you don't expect
Like this: I have several producers and several consumers. I would like to parallelize processing (produce item -> process item -> consume item) and consuming.
Am I right that producer and consumer doesn't know about transformation which I call with pipeline fn?
pipelines do need to produce output, so if you have a terminal stage that won't work as a pipeline
Channels, producers and consumers are decoupled from a transformation in pipeline fn, right?
btw, Is it good to build CPU consuming programs with core.async? I could use thread, <!!, >!! and pipeline for cpu bounded operations and go, pipeline-blocking for async operations.
no no, cpu bound operations go in go blocks and, blocking operations like io go in pipeline-blocking
if want the best performance for your cpu bound work, you cannot just launch hundred of thousands of threads to do it, you need a pool
if you are familiar with continuations, inside a go macro, if you use <! you can think of it as meaning: take the continuation, give it to the channel, and the channel will schedule the continuation to be run if anything is put on the channel, and the current thread then is returned to the threadpool
Does clojure itself provide any facilities for generating a valid edn representation of a user-defined record that was defined with defrecord
? I had thought that pr
and friends did that, but now that I'm looking at the matter it seems like they do not.
Okay. But edn is extensible. So say I want to extend it to encode/decode a defrecord I have created. This seems quite likely and obvious. Is there an easy way to do this out of the box?
so you can change pr to print whatever you want, to print whatever you want, for any type
doing that looks sort of like https://github.com/hiredman/bytes/blob/master/src/com/thelastcitadel/bytes.clj#L7-L14
then, depending on how you are reading the edn, you'll need to define a reader for that tagged element
This seems like something I will want to do a lot. I want to use clojure.edn/read
just so that I'm reading forms safely. But that means I need to read/write edn, rather than clojure's ordinary serialization format (which read-string
/ pr
provides). However, if I need to write custom encoder/decoders for every defrecord just in order to use edn, then that's a lot less convenient..
In the Clojure cookbook in chapter 4, recipe 16 they cover Emitting Records as edn Values. the next recipe also talks about how to handle unknown Tag values.
edn is itself rich (it has sets, maps, etc) so, at least in my experience, I don't end up tagging everything like that
Hope that helps @alexisgallagher
@staypufd: Thanks. good to see.
That seems to confirm my conclusion, alas. I had hoped edn was better integrated into the language so that I could handle defrecords without having to bring in yet another third-party library. 😕
for example, the #inst
means it is instance data in a certain serialized format, but that doesn't tell you want the deserialized format will be
I really just want to round trip defrecords with safe reading. That's it for now. I had thought that clojure.edn/read
was the recommended way to go about this but I'm guessing maybe it's not.
Well, if you take into account the reason Records are in the language then it makes sense that edn doesn’t have them. Records were added as a more efficient alternative to structs. They actually get made into Java class objects and have all the strengths and weaknesses of them. But b/c of that edn goes with the more general struct for it’s representation.
alexisgallagher: https://github.com/miner/tagged may help
If all you want to do is round-trip then doing structs is better for you and then have a Record that you fill in from the struct (via same key names) for when you need to use the Record type for interop or speed
So you're suggesting at de/serialization time I go from MyRecord -> struct -> edn ?
Not really. I mean if you don’t need Records for interop and the speed they give, then use structs.
I believe I do need records for various other reasons.
I know this is going to sound weird, but you could potentially use the Java RMI since your using Records
too much mental overhead. I've forgotten everything I once knew about Java RMI and would like to keep it that way.
That code there doesn’t do it for a Record but since defRecord creates a class you’d be able to use the objects for RMI
sounds like the easy options are to go with the miner library or just stick with pr
/ read-string
and forget edn for now. Going to go with option 2.
better unsafe reading than another dependency at the mo.
more easy than simple. I have unsafe reading now. c'est la vie.
@cky @wamaral @mping : I'm thinking of a point and an angle, not a length and an angle.
@reefersleep: Yep, that can still be a vector. (Presumably angle is relative to x-axis.)
No length.
Just coordinates for an origin.
Thanks for the attempt!
I'm not sure there is a term for it.
I'm just trying to name stuff in my code, so it's no biggie, just a minor annoyance... I do like to be precise.
Is EDN
still a popular format for passing around data from server to client? I read somewhere it's deprecated, and just curious what the best practice is for a REST API to communicate with CLJS on the front
transit is more efficient
Though less readable, but more extensible.
you mean less "human readable", right? sounds good. And is EDN in general still alive and well?
Human readable
I personally use transit. I think the options are Fressian, Edn or Transit. I'm not very familiar with fressian.
@wamaral: relative to the x axis
@taylor.sando: Is the best way to use transit with compojure ring-transit? : https://github.com/jalehman/ring-transit
But the x/y position is meant as the "current" position, and the angle is the direction that a line is going to be drawn the next time. Only these two pieces of data are coupled - the length is unknown and not necessary at this point.
I am generating a lazy list from a db. The db driver is using korma raw.
(defn lazy-df-db
([] (lazy-df-db 20000))
([l]
(map parse-rec (take-while (complement empty?)
(mapcat #(get-record-block %1 l)
(reductions + 0 (repeat l)))))))
This is so slow it never really returns. I can hook it into a text file with the same data and it returns in seconds.
Any ideas, about how to speed up db operations? I'm having it query a materialized view in sorted order, the query runs super fast in psql, but again slow here.@reefersleep: maybe you want the current position and an unit vector which you could scale at will
@josh.freckleton: If you're using ring it would work. I use pedestal which has its own middleware for handling it.
@reefersleep: an angle needs 3 points in space, just 2 (current position and x axis) is not enough to form one
@josh.freckleton: I mean compojure would work with it.
@taylor.sando: gotcha, just trying to keep in touch with best practices. From a cursory look, Pedestal seems a little involved, and unless you'd suggest I learn it, I'll probably keep using compojure for now. Thanks for the help, there are a lot of descisions to navigate in this world of CLJ
Ya pedestal is pretty involved. I learned it awhile ago. Not sure it's significantly better than the other options.
base698: it is hard to tell from that what you are trying to do. your code + korma is almost certainly pulling all the data in your process's memory and operating on it there, without the benefit of any data layout and indexing optimizations that the sql database does
that whole function sort of strikes me as odd, like, reductions with + is going to result in a sequence of numbers, in which case why not use range or iterate
@hiredman I don't know how many records there and don't want to query with count to find out
I would double check your running against a file comparison to make sure it is doing the same thinig
if you split mapcat in to map and apply concat, and put the take-while between you should be fine
i just realized that if you use an empty vector with :keys in destructuring
you should google unfold
which doesn't exist in clojure.core but is very useful for exactly this sort of thing
you can accept arbitrary (or no) elements
https://gist.github.com/hiredman/4d8bf007ba7897f11594 is unfold for CollReduce, but somewhere out there I know people have implemented it and things like it for lazy-seqs too
@wamaral: When I say "angle", I really mean a radian. Maybe "angle" is not the proper term.
korma wouldn't even return running a normal query "select *" looks like this is going to return. anyone know the defaults of Xmx for lein? and how to increase it.
@base698 i have used korma on a project, but i don't like much this kind of DB manipulation it uses (like rails, django, etc..) today I'm using yesql, i have wrote a few queries, and it looks like good for me :)
Is there a way to setup a build target to compile one clojure file into js via cljsbuild?
@base698: you might be able to accomplish that with modules