This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-26
Channels
- # aws (1)
- # beginners (15)
- # boot (30)
- # cider (13)
- # cljsrn (16)
- # clojure (458)
- # clojure-dev (15)
- # clojure-france (131)
- # clojure-greece (124)
- # clojure-korea (2)
- # clojure-spec (42)
- # clojure-uk (115)
- # clojure-ukraine (1)
- # clojurescript (103)
- # component (18)
- # cryogen (1)
- # datomic (4)
- # dirac (3)
- # figwheel (1)
- # funcool (13)
- # hoplon (60)
- # luminus (2)
- # off-topic (2)
- # om (28)
- # onyx (45)
- # parinfer (28)
- # pedestal (1)
- # proton (23)
- # re-frame (18)
- # reagent (36)
- # ring (1)
- # ring-swagger (5)
- # untangled (13)
- # vim (9)
@grant right (but maybe you should really be splitting things out more)
@sophiago don’t understand the question
i have a library as a dependency with gen-class in the ns and assumed it wasn't being required with everything else since i had a sort of similar issue with classes defined with defrecord that had to be explained to me
fwiw @sophiago from your gist yesterday I would ditch the gen-class and just make Number another defrecord
ahh.. well I would at least move it to its own ns. in my experience gen-class is pretty hard to get doing what you want (there's a lot to get your head around)
at this point i'm not sure it's responsible for my bug tho. i pasted over the gen-class code as well as the constructor into the ns of the main project and tested it in the repl and am still getting the bug related to its behavior in protocols when i run the code
for instance one thing I see you're doing is referring to the gen-class'd class directly, ie Number, in that ns. which last I checked you can't do, you need to :import it
i think you're confusing which ns is my checkout and which is the project i'm working on
i do think it's possible my issue could be solved by overriding the constructor with one that uses a qualified name Number
what's the issue you're seeing? as far as I can tell the Number defined in the gen-class in that namespace is not being used anywhere or doing anything. I could be wrong, because gen-class always confuses me, but that's what it looks like
it's meant to be a base type so that when rationals reduce they'll dispatch to appropriately rather than having to choose one java type
also, you're specifying a :constructors key without an :init key, which is counter to the gen-class
documentation
the error i'm seeing is "symbolic_algebra.core.Rational cannot be cast to java.lang.Number"
but it is correct in that it doesn't make sense to specify :constructors without :init
I need to filter hiccup notation to remove any [:br]
or series of them that immediately follows another tag, or that is immediately followed by another tag.
["foo" [:br] [:br] "bar" [:br] [:br] [:foo "foo]]
becomes ["foo" [:br] [:br] "bar" [:foo "foo"]]
@seylerius I'd probably filter with keep-indexed
and just index back into the original collection to check was previous and next are. seeing as with hiccup the structure is probably a vector
oh actually it sounds like what you're doing is more complicated than what I thought, my bad
Essentially, if a [:br]
is bounded on either side by something that isn't a [:br] or a string, it's tossed.
is there a best practice on how to create a stoppable and restartable core.async go-loop
?
i was thinking about close!
ing the channel (3), then re-executing the go-loop part to restart it (2) - is that correct?
(comment
;; 1
(def clockworker (let [time-channel (chan (async/sliding-buffer 1))]
(go-loop []
(let [_ (<! (timeout 2000))]
(>! time-channel (time/now))
(recur)))
time-channel))
;; 2
(go-loop [] (when-let [time (<! clockworker)]
(println "got time: " time)
(recur)))
;; 3
(close! clockworker))
Has anyone reported that the clojure syntax highlighting doesn't seem to work with ::
on http://clojure.org?
I’m aware of it, but haven’t spent any time looking at it
working on it
Lynn has been delayed as EuroClojure is under way atm
http://clojure.org uses highlight.js - looks like maybe the regexes there probably need to be tweaked
actually, looks like highlight.js fixed this in April and we’re using an older version, so I should be able to just include the latest
I’ve pushed the update for that, should be live in about 15-20 m
curious, why does ((fnil :key 0) {:key nil})
return nil
, I would’ve thought it returned 0
?
fnil is about what to do when the function is given a nil, not when it would return nil
Would anyone have any idea why extend-type
and extend-protocol
are so much more prevalent than just extend
? My first impression would be that extend
leads to more composable code. Are there any drawbacks to using extend
over the other two? Thanks!
hey folks, is there any lib that helps me reset my system ( system containing components ) when any file on my classpath changes ?
@trptcolin : thanks, I thought so. I was worried there was some obscure reason they were preferred other than convenience (like how it's more performant to implement protocols directly in records and types, rather than extending them). But I've never been able to find anything.
no reason that I’m aware of other than convenience
1. when requiring a library with gen-class and a constructor in it, what extra must one do to make that work?
2. i had assumed all along that clojure did not support operator overloading, but looking through the core source it seems it may be possible. could someone please clarify this? it would mean worlds of difference for me
1. require the library (to cause it to be loaded and the compiled class to be created). instantiate the class using the constructor via java interop.
2. from a general perspective, there are no operators, just functions and Clojure allows functions to be modified. Protocols and multimethods both support type-based dispatch. That said, things around math are a bit more complicated, especially due to a) inlining, b) Clojure’s approach to numerics and c) direct linking of core
I don’t get what that means re “pasted in separately"
if i don't copy the constructor directly into the ns i'm working in then it's not required with everything else
I think it would be helpful to be more precise with terms
“require” means “load the namespace"
and in particular create and load the Java classes that implement the functions
no worries, I’m not criticizing
just trying to be precise so these words mean good things in relation to other docs
so that’s really two parts - the require and the alias
require ultimately results in a call to load to load the code
but then i realized (separate issue, just pointing it out as part of my learning curve) i had to use a different syntax) for the defrecords
alias modifies a mapping of aliases to namespaces in your current namespace
when you say “constructor” what do you mean exactly?
ok, well for starters i was suspicious because of the defrecords needing a special syntax since they create java classes
they are really independent things, so let’s stick with gen-class
and then i have a class extended from an abstract java class using gen-class (this is actually what led to that patch i proposed on the dev list you responded to, if you remember)
i realized yesterday that without copying the constructor into the ns i'm working in i couldn't use it in the repl
do you mean a function named in the gen-class :init ?
and you are trying to instantiate the class created by gen-class?
if so, you should not use the function named by :init
you should use Java interop to instantiate the class (it’s just a Java class)
either way
you should instantiate the class using (Foo. …)
to do this, you will either need to import that class, or to use the fully-qualified class name when you invoke the constructor
so that’s incorrect
but the thing is it's only for dispatch so i don't actually need to do that anywhere in the code
you should not invoke (-Foo …) directly
oh wait, no sorry i was wrong about that (a month off of looking at code and i can forget things)
i'm having problems now that i'm requiring (hope correct terminology) it in another ns
you need to require the namespace to cause the class to be created
you need to import the generated class if you want to be able to invoke it via an unqualified class name in Java interop
you probably do NOT want to invoke functions in that namespace directly
you also need to compile the namespace, gen-class doesn't do anything unless you are aot compiling
well, I’m not sure that’s actually true anymore
there should be no point where i call this particular function we're discussing in the new ns
@alexmiller the doc string for gen-class still says it 🙂
it would be tremendously helpful to see a) the code, b) what you are trying to do, c) what you are getting and d) what you are expecting
@hiredman I am aware of that
records are totally different (and that is correct for records)
the fact that they generate Java classes is an implementation detail
whereas that is the explicit intended effect of gen-class
and on another tangent, it’s perfectly ok to write your Java concrete class extension in Java rather than in Clojure (and depending what you’re doing, this can be way easier)
well, mainly i'm experiencing type casting error and not certain whether it's due to the class created by gen-class since the point of it (as i describe in that post on the dev list) is to use as a base type for dispatch, not to call directly. so i tried instantiating it in the repl and it wasn't working. now, as stated above...this could be a syntax issue (but we already determined i'm using an odd syntax in my original ns compared to how you would do it: the gen-class docs are notoriously sparse) OR it could be that it's just not be required with everything else. so in response to that i just copied the function i use to instantiate it into my new ns and lo and behold it works in the repl. however, i still get an error when running the new code....just a different error
@alexmiller since you seem to be one of the few people who understand gen-class perhaps the best way to go about this is to see if you can correct my syntax with that? and then i can see if that allows me to instantiate that class in the repl using the ns i'm currently working in? that would rule out many issues i think
@sophiago if you are not a java programmer, I would strongly suggest not using gen-class. It exists pretty much entirely to interoperate with java where java requires a concrete class
@hiredman it's a little late for that... it was a long night of frustration, but i did get it working, if oddly working
anyway, the code is here and the gen-class and function and question is conveniently right at the top: https://github.com/Sophia-Gold/Symbolic-Algebra.clj/blob/master/src/symbolic_algebra/core.clj
line 2 - should be :gen-class, not gen-class (this won’t even compile in 1.9)
line 3 - should probably be a fully-qualified class name including the package you want
it is possible (but frowned upon) to create a class in the no-segment “default” package
I don’t think there’s any need for :constructors at all if you’re doing what you have there
Java classes exist in a hierarchical tree of packages
similar to Clojure namespaces (although the hierarchy is purely name-based in Clojure, not meaningful to Clojure)
by saying :name is Number you are literally creating a Java class whose fully-qualified name is “Number"
generally you want to give it a package like symbolic-algebra.Number
or symbolic-algebra.core.Number
that’s a fully-qualified Java class name
the / syntax is for Clojure vars
the -Number function is not doing anything useful in your gen-class afaik
you may want to check out https://github.com/clojure/math.numeric-tower
yeah, but that’s not how it works
it looks like you are trying to create a subclass whose constructor returns something of a type other than itself
which is not possible in Java
gen-class does not allow you to explicitly define a constructor method
rather you define an init method which acts as a bridge between subclass constructors and superclass constructors
the init method should respond to any constructor type signatures you define (in :constructors) and should return arguments that are used to invoke the super class
i looked at math.numeric-tower and that was specifically what i was trying to avoid in implementing it this way
in Java, the super class constructor is always called last (if not explicitly, than implicitly)
and the concrete type returned by a Java constructor is always the type of the class whose constructor you invoked (that’s not necessarily true in other langs like Ruby afaik)
@hiredman i'd like to please just focus on the implementation details because my brain can't absorb that and have two conversations at once
afaict, you’re not using your Number subclass at all
you’re just invoking the function -Number which calls num
but that’s just a function that happens to be named -Number - it has no link to your class
basically your gen-class stuff compiles, but is meaningless in the context of whatever you are trying to do
that is, you could remove the gen-class and replace the calls to -Number with num and it would be identical
which maybe is totally ok?
rationals are at the bottom of my numerica tower and i'd like them to dynamically dispatch to java's numeric types using clojure's builtin methods to do so rather than handrolling all that myself as the other example did
@alexmiller hmm if that would work i'm going to feel very stupid about this whole endeavor
no, those are fine
without that there would be no calls to coerce them upwards when the classes don't match
wait, I guess that’s not actually doing anything
I retract that, was reading it as extend-protocol
so that’s all fine
it's not all fine because i can't leave that extend-type without having the gen-class
sure you can
it’s not using your Number - it’s using java.lang.Number
it’s ok to extend a protocol (Algebra) to an abstract class (Number)
protocols do Java hierarchy type matching
you don’t need to - it’s already doing that
all classes in java.lang are automatically imported
(which is another reason that using bare classes in gen-class like Number can cause confusion)
I had a hard time wrapping my head around what it was actually doing and really replied to the intent :)
and tbh the real impetus for thinking it was a good idea for a patch rather than just my own code was that just weeks prior David Nolen (no offence, he's solely the reason i'm using clojure today) was giving a speech talking about how eventually there would be clojure in clojure for the jvm
well I wouldn’t say that will never happen
as never is a long time :)
even if it was the case, the dispatch in clojure.lang.Numbers being closed or open is a separate issue from clojure in clojure
there are a lot of reasons why clojure in clojure may not be something that anyone would actually want to use
yeah, it's an a rock and hard place for me given i frankly am not only not a java coder but really don't like java as a language yet i understand all the reasons for needing some parts to be low level like that even though it impacts writing libraries to some degree
anyway, before i take up too much time... @alexmiller can i clarify overriding core math functions as it would pertain to this library you just looked at?
well, that’s challenging :)
Clojure is not currently open for extension in that way - that was the intent of the other thread I pointed at in my response on the list
you could theoretically override the vars for things like +
however, those functions are inlined (which means that existing compiled code wouldn’t see it)
and Clojure core is AOT direct linked, which means other calls from within core to those functions would not see the changes
well, i do already have to rewrite some core math functions like mod and rem obviously
so you either need to define your own functions or fork Clojure
I have this abandoned (and unfinished( project that used rules about numeric types (written in core.logic) to generate a replacement for Numbers.java, the idea being that even though that is not runtime extendable it would be clearer than Numbers.java, and would be easier to generate your own custom Numbers.java that took in to account whatever types
@alexmiller when you say "define your own functions" you must mean far beyond what i've already had to do, correct?
no, that’s what I was referring to
where “other people’s code” includes core
yeah, maybe it wouldn’t affect much
well, i'm saying regardless of whether i override + or use add i need to redefine mod and rem anyway
yeah, I found that question confusing too
= and == :)
yeah, that person is just wrong :)
@sophiago What I've done in cases like this is implement my logic in one namespace, then in another import clojure excluding +, -, etc. Then define those functions. So you can do (:require [my.math as m]) and write (m/+ (m/- x y) 4) to call your special logic
@sophiago I do it with =, and, or in this namespace: https://github.com/halgari/odin/blob/master/src/com/tbaldridge/odin.clj#L28
@tbaldridge thanks! that's helpful. i'm already doing this in it's own ns anyway
but that zero is literally the empty set, kinda like how Church defined the integers to be certain functions
to be fair, Church’s language wasn’t winning in the Alioth perf rankings
sure, the monoid thing was just the objection that I thought of to defining (empty? 0) => true
or actually, wait @tbaldridge let me make sure i'm clear about how you're doing this...
i'm not sure. I'm just saying that since the empty set is empty, if 0 is the empty set (empty? 0)
could be argued to return true. but it would be a very bad way to set up a programming language environment
just whenever you import the ns where you override those functions you also import clojure excluding them?
@sophiago not sure if hiredman is replying to you or the other discussion, but I would say yes
@alexmiller p sure other discussion...
@sophiago no, any namespace that used your math namespace you'd assign it a name (like 'm') then call the functions via (m/+). In other words, don't try to replace Clojure math, make it clear when you're using your math instead of Clojure's
One choice:
(ns other
(:refer-clojure :exclude [- +])
(:require [my.algebra :refer [- +]]))
I like that one. If you really need to have local plain operation symbols, at least the top of the file makes it clear what's going on.
@tbaldridge ah ok, that's essentially what i'm doing right now except with + instead of add, etc.
Another choice:
(ns other
(:require [my.algebra :as m]))
… m/+ …
so do i want users to be clear they can't use all of core math functions with them since i won't rewrite all of them?
and sacrifice the legibility of a language with true operator overloading like haskell?
in actuality, the choice is not yours - it’s up to the user’s namespace that is pulling yours in
or in the first choice i'm actually replacing them and reminding the user (again, me) not to use functions that haven't been redefined?
well, the compiler will helpfully fail to compile code that references something that is undefined
at this point my math is all high level and i just need to replace the lower level functions
ok so i need to actually code all of this but i think the only thing i'm left unclear on is this:
Yes you wouldn't have to qualify them
Yeah, as a programmer I really don't like it when someone re-defines something that looks like a core function into something that's not
@tbaldridge did you mean to say "as a java programmer" 😉
Nah, if did x = 1 + 2 in C++ and got back a string I'd be just as unhappy.
And let's not forget that operator overloading abuse is also a thing in many languages.
Use a different namespace, and refer it as "m/" that's kindof what namespaces are for.
I'm just saying that if I saw that "refer-clojure...require" combination, quoted above, in a code review I'd probably say "let's find a better way to do this...".
from my point of view out of necessity i'm 7/12 of the way from having overwritten all of core math hence negating your issues with it
is it possible to define new clauses for the conditional reader macro? e.g. :node
, :phanatomjs
, etc.
if not, are there plans to extend the conditional reader macro in the future with this sort of feature?
@noprompt take a look at tools.reader, from what I can see it might offer that as a parameter
@tbaldridge thanks tim. will have a look.
https://github.com/clojure/tools.reader/blob/master/src/main/clojure/clojure/tools/reader.clj#L950
yeah, he didn't define what he was trying to do here
@hiredman supposing that you could configure that you're compiling against that environment ahead of time, no.
what i'd like to be able to say is that whenever i build this set of files i want the reader to honor, say, the :phantomjs
clause.
http://dev.clojure.org/display/design/Reader+Conditionals does list "Open feature set" as a future extension
ugh, all of that earlier and i'm still getting that error. qualified or not you do need to override core math functions and i ended up having a bug in rem that i just reverted to the built-in in the original version...not good enough anymore apparently according to the stack trace and i'm really not sure what it could be other than some type of rounding error with java math 😕
phew... think i fixed that. it was a mistake to look at the cljs code. too convoluted in attempting faithfulness to horrible js math
@sasha I have, do you have a question?
@tbaldridge: yep, i configurate number of threads, but result is constant.
@tbaldridge: and worker-name-prefix
not working too
@sasha have some example code?
@tbaldridge: i have (run-server app {:thread 9 :worked-name-prefix "project"})
check your spelling...
@tbaldridge: sorry i don't understand
:worked-name-prefix vs :worker-name-prefix
and I think the docs are wrong
:thread is probably :threads
doesn't make any sense as :thread
@tbaldridge: yep, sorry, worker offcorse
@tbaldridge maybe we have bug in docs has :thread
@tbaldridge: http://paste.ofcode.org/TNdVTFvx9NS6nf29RGTZPM - this is my example
yeah :thread is correct (looked at the source). IDK then, how do you know it's not working?
@tbaldridge: i see ps -T
@tbaldridge: ps -efT | grep java
and htop
, and i don't see name of thread
@sasha sorry, that's all I got. I haven't dug this deep into http-kit before. I stopped using it when I encountered some other things I didn't find acceptable.
@tbaldridge: Thanks, which server you are using?
I recommend Pedestal http://pedestal.io/ , but that depends a bit on what you are trying to build.
@tbaldridge: i have ring, compojure and need only http-server
@sasha have you considered ring-jetty then? That's probably the standard approach
@tbaldridge: thanks