This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-18
Channels
- # bangalore-clj (1)
- # beginners (36)
- # boot (119)
- # braid-chat (16)
- # cider (14)
- # cljs-dev (34)
- # cljsrn (7)
- # clojars (9)
- # clojure (91)
- # clojure-austin (1)
- # clojure-bangladesh (1)
- # clojure-dusseldorf (5)
- # clojure-israel (1)
- # clojure-russia (3)
- # clojure-spec (6)
- # clojure-uk (7)
- # clojurescript (11)
- # community-development (1)
- # core-async (5)
- # cursive (6)
- # datomic (11)
- # dirac (12)
- # funcool (24)
- # leiningen (5)
- # luminus (5)
- # off-topic (2)
- # om (69)
- # om-next (16)
- # overtone (4)
- # perun (19)
- # re-frame (23)
- # reagent (38)
- # specter (7)
- # uncomplicate (9)
- # yada (4)
has anyone else had the experience working with vim that commented out text doesn’t get highlighted as such?
hi. cant get it about java interop. this works
public static void main(String[] args) throws Exception {
Path path = Paths.get("test.txt");
Reader reader = Files.newBufferedReader(path);
}
(file not found). this (let [path (Paths/get "test.txt")
reader (Files/newBufferedReader path)])
does not work Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.net.URI, compiling:(/tmp/form-init6447706182627723560.clj:1:73
yes, call with URI is overloaded in Paths/get but for some reason Clojure version can't see the call with String
Paths.get is a java var args method, which at the jvm bytecode level means the arguments are passed as an array, so that is what you do from Clojure
Hii people!! I have this question about VMs and libraries. Why do we make separate clojure libraries with sane functionality as the native VM ( like strings etc) in clojure(script)? What exactly is the benefit that we gain from native implementation?
Hi, @abhi18av. A lot of people don’t like interop so there are a lot of wrappers out there around Java. Sometimes, however, it simply has to do with Clojure’s LISPness and how it does certain things for performance reasons.
Personally, I try to avoid bare metal Java as often as I can. For no real reason, really. I’m just not a huge fan of the interop syntax.
Guys, is there an equivalent to JavaScript's Promise.all()
in Clojure?
In JavaScript, if you have an async action, you have the function running the action return a Promise, you can then pass an array of Promise objects to Promise.all()
, and it then returns a Promise which resolves when all of the promises of the array have been resolved, with an array of values in the same order as the promises, or rejects when the first Promise rejects.
(Promises in JS are more similar to Clojure's future
type)
While I like the way of processes and channels, more often than not I just need a value from a function, and just wait until it's available, sometimes I just want something like
Oh, right. Thanks @akiva. Also, is there another benefit that we can by default parallelism the library internals while recreating the functionality as a Clojure lib?
There is some really serious optimization going on behind the scenes, if that’s what you mean.
@madarauchiha so in the snippet's case, you'd want a list of get-url
results?
No, I would like a single future
that would resolve with a collection of results
Assuming that get-url
performs a GET request on the URL and returns the body of the result
I would want future-all
to return a future for a collection of bodies, in the same order as the original urls
collection.
JavaScript also has future-race
which would return a future
that resolves with the result of the first future that resolves (`Promise.race()` in JS)
But that's less of an obvious usecase.
(The obvious usecase is a timeout Promise.race([doAction(), rejectAfter(2000)]);
Promise that resolves with the result of doAction()
but rejects after 2000ms if it hadn't resolved by then)
If it accepts a collection of futures and returns a future, I don't mind the internal details.
While an implementation of this would be a fun exercise and I would be happy to see what people can come up with
I was wondering if Clojure had something like it builtin
A delay is essentially a memoized function, I'm still struggling between the differences between promises and futures
But the idea is, while processes and communication channels are nice you often just need a function and a result.
A promise is initially a barren container for a value, and at some point a value will be deliver
ed to it
Without the full blown structure around it.
@kauko Right, so a promise is essentially a deferred
Which is less pretty than JavaScript's Promise
object
In JS, we used to have deferred values as well
You'd create a new $.Deferred()
then continue execution, then sometime in the future call d.resolve(value)
or d.reject(err)
At the end of the function, you immediately return d.promise()
The problems with that approach were primarily OO based, since this new $.Deferred()
constructor isn't really a constructor, because it doesn't bring the object to a usable state (you still need to call .reject()
or .resolve()
on it, as well as .promise()
to get anything meaningful out of it
The newer Promise
constructor accepts a function with two parameters, which are also functions, resolve
and reject
Essentially merging Clojure's future
and promise
into one
(Those were 180 seconds of JavaScript history, with MadaraUchiha)
In any case, one thing that no one ever seems to talk about (at least, the brave and true never mentions so far) is how to handle errors
If I have my get-url
function, and I encounter a server error
How do I handle it?
Anyone?
Spent hours on this and I can’t seem to cobble together enough macro understanding to get it done, I’m at an impasse.
http://paste.ubuntu.com/23198691/
Why exactly does evaluation explode when I give a variable instead of a value ? My hypothesis is that I’m still playing with the symbol myvar
instead of its actual value {:incr 1}
. Now that’s great and all but how would I remedy that ?
@madarauchiha the project I work on uses the promesa library for promises ( http://funcool.github.io/promesa/latest/ ) in that case, there’s a catch function in core. See the “Error handling” part of the link.
created this to turn java objects into printable map-like things https://gist.github.com/ghadishayban/c36368d1de6ce15ba2a782c3cfc9211a kind of like a more modern version of clojure.core/bean
@akiva, I see. Btw is there a way to use C/C++ libs in Clojure? No not the JNI but is there a port of the project towards this goal?
Question while I idle — is it a crime to use eval in clojure ? I have a macro problem (basically I can’t expand a variable into its value in a macro - and I need this structure because I’m trying to stuff it into a ‘case’ form which is also a macro). Anyway - long story short, I can build up a list representing the case form I’d like - but I may need to eval that list to eventually make it do something. So… is using ‘eval’ the worst thing possible, ever.. or is it legitimate in some instances
@pseud eval
is fine, as long as you’re aware of all that it does.
It’s unsafe, so sanitise your inputs, and often, but not always, there are more efficient/safer/simpler ways to achieve the same thing.
But sometimes eval
is necessary. Sometimes it’s useful to compile a declarative data structure into a function, for instance.
is there a swagger lib for generating client fns? I have a server serving swagger, and I want to auto-gen the fns to call the API using clj-http
kind of like https://github.com/oliyh/martian, but I want defns
@akiva Thanks)
There is no way to replace a variable by its value before going into a macro, right ? Essentially (mymacro x) => (mymacro {:k1 “v1”}) The reason I ask is that my macro needs to compute on the literal structure of the map and this (obviously) fails if I just get a symbol for a variable.
@pseud Correct, macros execute before code is actually evaluated. They transform code to code, there are no values at that point.
Generally, with macros, it’s best to write what you want as a function first, and then use the macro to create a layer of syntactic sugar over the function(s).
Unless you have a very specific syntax tweak and can write a simple macro straight off 🙂
likewise there’s no layering of lambdas and calls to macros within macros to be sneaky and coax out a value of an incoming var, right ?
@pseud not in general, you could use eval
in specific scenarios (basically re-reading your source and eval-ing interesting bits of it)
I’d love to, but (confession time), I was writing something in a clojure repl and have battled all day with splicing a body into a case-statement. Now in clojure, I finally tried out building a list and passing it to eval - that worked beautifully, but using that in clojurescript later on will be… problematic 😞
The evaluation context inside a macro is not going to be the same as for normal code so "Here Be Dragons!" on that...
Yeah, this is why macros (and eval
) should be considered a "last resort". Aside from portability issues to cljs, they don’t compose and your code won’t "scale" in complexity.
it is possible to go quite far if the things you feed into macro boil down to constants, or expressions of constants and limited set of clojure functions, here I was able to build a simple css cljs macro, which accepts clojure-code as input, to produce validated css strings as output: https://github.com/binaryage/cljs-devtools/blob/fd117bce8e777de190605e4c693604bc9074aa66/src/lib/devtools/defaults.cljs#L226 https://github.com/binaryage/cljs-devtools/blob/fd117bce8e777de190605e4c693604bc9074aa66/src/lib/devtools/defaults.clj#L79-L87
@darwin - x is a map of keywords -> fn’s I wanted then to make a macro which spat out a new function which had a case-form whose body is made from processing the aforementioned map such that I eventually get a structure like
[:incr ((fn [m a] …) m a)
:decr ((fn [m a] …) m a]
Because, with such a structure, it’s trivial to spit out a case-form and voilá, from a map of functions down to a case-form that I want.But naturally, the argument I get in a macro just evaluates to a symbol, now If I input an actual map of keywords -> fns everything is perfect.. But passing in a var will obviously explode as I’m then trying to rearrange the structure of a single symbol (in this case ‘x’)
so, x is known at compile time, why don’t you write a macro which will produce x (data), then expand it in place when passing it to mymacro?
The point of the variable is not that I have a def somewhere. The point is that I wanted to treat these initial maps as well.. maps. Be able to merge them etc before generating the resulting function holding the case form
if you need to work on those maps, you have to do it at compile-time, that means in functions called from macros or macros themselves
x
is not seen at runtime, unless you decide to export it as data, e.g. like this (def runtime-x (x))
I know it is a bit mind-bending, you have to know exactly “where you stand” when looking at code
Ah, so in that case, I’d have to do whatever processing I want (e.g. merge, rename-keys) etc in macros, too.
this is often the case, once you switch something to a macro, all inputs have to be turned into macros as well
because you are not working with runtime values anymore, you are working with compile-time values and doing fancy code-rewriting
@darwin Well, that’s certainly something to think about. I’ll have a go at it tomorrow. I think 8+ hours of battling macros today has turned my brain to mush. But thanks 🙂
@madarauchiha I think it is possible to employ pipeline-* stuff from core.async for this, though I'm not sure it would look super nice