This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-31
Channels
- # admin-announcements (1)
- # beginners (1)
- # boot (91)
- # cljs-dev (11)
- # cljsjs (8)
- # cljsrn (38)
- # clojure (89)
- # clojure-dusseldorf (1)
- # clojure-russia (6)
- # clojure-spec (14)
- # clojure-uk (4)
- # clojurescript (25)
- # data-science (1)
- # datascript (3)
- # datavis (4)
- # emacs (25)
- # events (1)
- # jobs-rus (3)
- # off-topic (1)
- # proton (3)
- # re-frame (19)
- # reagent (5)
- # specter (10)
- # yada (1)
how would i wait for an i/o operation in clojure? i’m used to javascript, where we’d use a promise or callback. not sure how to proceed in this case.
@ezmiller: which io operation? Do you use any library for that? Do you want your program to run other calculations while io is running or can you just block everything until it returns?
pretty simple case here. i want to import a rather large csv. once it is imported i will do some manipulations, and then return the result in fn. the io problem is that this is a server sending content to a client, so i don’t want to return until the data is prepared.
use a promsie? https://clojuredocs.org/clojure.core/promise
I’m using the http://clojure.java.io and clojure.data.csv libraries.
@ezmiller: without getting into which server you're using and how long your operation takes, I can only say that these IO calls are blocking (unlike node.js) so code lines will be executed synchronously in order. You don't have to use promises or async libraries
Fyi, I’m working with the preconfigured server that comes with the reagent base project.
All pretty new to me, so I’m not sure what packages that means, but it seems to rely on libraries called ring and compojure.
@ezmiller: depending on your you case, you may be able to just slurp
a url or file
hi ethan by the way 🙂
It s probably in the latest compojure, if not it s possible to explicitly depend on it
But that doesnt make csv parsing async, either your lib supports it or you need to wrap the op in a future like context
people in the clj world generally eschew asynchronicity unless there's a compelling reason for it
or rather JS (& friends) are the only ones with async everywhere because they only have 1 thread so blocking calls usually isn't an option.
Asynchrony can be a powerful force for helping you not hide some I/O in the middle of a function that would be much better pure, though.
As an extreme example, Haskell (sans unsafePerformIO
) forces you to do that with its type system. Whether or not that’s as a whole desirable is a different question, but it definitely produces the same pressure.
I think that’s interesting because it raises a point about languages where the type system does not enforce that, like Clojure; is synchronicity only easier because you get to hide IO in places? (Consequently, is not hiding IO in places intrinsically better?)
(Sorry, I buried the lede there a little bit: that is interesting because you don’t have to care if the IO is performed synchronously or asynchronously in Haskell.)
@lvh: i think that your assessment is correct, and this is a tradeoff that is made. haskell is more strict, and allows you to enforce a lot of things at compile time, while clojure mostly relies on runtime checks. honestly, as a fellow async nerd, i think the async situation in clojure needs to be solved, because it is simply too easy for people to make mistakes. but i don’t see a good solution either, that doesn’t introduce additional complexity.
I like manifold a lot, but it definitely has the same problem as Twisted did in Python, that is, there are people who don’t use it
well there's no magic bullet. imho clj async situation is just fine, and I don't think it's discouraged in general, it's more that it's misused very often
ex: ppl using the async part of a web server to later do blocking io in handler via some db driver
@lvh: for what it’s worth, juxt’s new web library yada
uses aleph (which is based on manifold), and it’s slowly starting to move into the right direction
there is no async-first mentality, which makes sense for the majority of the situations
the fact that everything is signals between processes yes, but it's a different thing imho
well you can get away with sync behavior in those languages, but what i mean is, that there is no way in clojure to tell “this function must be implemented async"
I happen to work with a stack that's mostly async for the rest backend part, and very happy with it
we also have bits in erlang actually, but not really because of to the async "situation" in clojure, mostly because it's a better fit for some of our needs
yes you are right about that, when used by a team of disciplined developers, it’s easy to pull it off in clojure, but you have to start using non-standard libraries
there's actually a new ring version with async handlers in the works (it's in beta right now I think)
but yeah, the adoption by the "standards" like ring is slow, but core.async on the other hand has been here for a while for other contexts
jetty9 for instance has had async from the beginning, it was just a bit the wild west until now, many adapters, many mistakes etc. But manifold/aleph should be solid, it is/was used very heavily by some serious players
well, that’s the thing, rather than enforcing certain behavior from the standard, a new standard is added
in haskell it seems to be much more common to just break things and let the community fix it
Any suggestions on how to coerce one nested data structure to another? I can't for the life of me understand Prismatic Schema coercion, so maybe a good tutorial? Can it be done with pattern matching?
@yonatanel: can you be more specific?
Clojure web apps: how are people sanitising/coercing input values into correct types, e.g. numbers/dates? prismatic/schema?
@petrus: input coming from html forms?
Compojure :params
or from API calls
I do it manually :)
(defn parse-long [s]
(try
(Long/parseLong s)
(catch NumberFormatException e
nil)))
if you do it a lot, it may be worth using something like https://github.com/metosin/compojure-api
I used prismatic/schema coercions for a while
they're good for what they try to do
@petrus, I do what @pesterhazy did and wrote my functions manually.
Is there an easy way to get a sub-vector of a primitive array?
(subvec (vec "Hello") 1 4) => [\e \l \l]
(??? (char-array "Hello”) 1 4) => [\e \l \l]
Or should I just cast to a PersistentVector
and back? (This is in the context of reify
ing a java.io.Writer
.)I'd be surprised if you can
JVM doesn't support that afaik
You can use System.arrayCopy() though
I’ve got a function:
(defn get-col-as-vec
[col-key]
(mapv #(get-cell-val % col-key) (range (count data))))
The data in question is 80000 rows about. It takes about a minutes or so to run. Anyone know how I can speed this up?something like this maybe? https://stackoverflow.com/questions/35173713/clojure-read-csv-and-split-the-columns-into-several-vectors
yes, the
(apply map vector coll)
should transpose your data so that you can get at the columns easily. not sure if it's faster though@ezmiller: If you’re trying to do huge matrices fast, you should probably be using something like https://github.com/mikera/core.matrix. Pure Clojure vectors are really slow.