This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-18
Channels
- # aleph (12)
- # beginners (31)
- # boot (67)
- # cider (17)
- # cljs-dev (14)
- # clojure (111)
- # clojure-dev (1)
- # clojure-france (4)
- # clojure-gamedev (1)
- # clojure-italy (49)
- # clojure-nl (3)
- # clojure-poland (2)
- # clojure-russia (18)
- # clojure-spec (15)
- # clojure-uk (68)
- # clojurescript (33)
- # core-typed (1)
- # datomic (15)
- # emacs (3)
- # graphql (4)
- # hoplon (36)
- # leiningen (3)
- # lumo (44)
- # mount (2)
- # off-topic (46)
- # om (21)
- # onyx (47)
- # parinfer (22)
- # pedestal (21)
- # protorepl (4)
- # quil (4)
- # re-frame (15)
- # reagent (4)
- # ring-swagger (9)
- # rum (27)
- # spacemacs (11)
- # vim (7)
- # yada (8)
I want something which is okay to pass an unpure function to ... and returns a strict list
in good clojure style, do people do (map impure-function ...) or do we do something else when we have to run an impure function and also get its return value ?
@qqq usually you'll use doall
if you care about the return value
@qqq it is fine, as far as I know, to pass a function with side-effects to keep
, filter
, etc -- the gotcha is that you should not expect that your code will necessarily execute. So, your side effect may happen, and if you don't mind, great -- but, your side effect might not happen, and that's why it says to avoid impure functions (or in some cases, may get called more than once).
it also might happen 31 times more than you expected
right
@qqq not sure why you want a "strict list", but for eager evaluation with guaranteed "run-once", reduce
would be a decent candidate to build it:
(defn eager-map [f coll]
(reverse (reduce #(conj % (f %2)) () coll)))
Does anyone know how to specify a stylesheet for a scene in fn-fx (a clojure wrapper for JavaFx)? https://github.com/halgari/fn-fx
have you found your answers ?
@danielcompton , @joshjones : okay, so despite the docs saying "use pure function", it's actually okay to assume atmost once, in order semantics -- i.e. we don't know how far it will exec (due to evaling thunks at a time) -- but we can assume that: 1. thunks, if evaluated, at evaluated at most once 2. thunks, if evaluated, as "in order" so (head (map f '(1 2 3 4 5))) won't do (f 1) (f 3) eh, let's skip 3 & 4 ... and then just eval (f 5) just for kicks
@qqq in the case of the current implementation of map
, I'd say so -- with the obvious disclaimer that making assumptions like this and relying on these details is not safe, and that I wouldn't use an approach like this (I'd prefer the eager-map
function I put above, for example)
in the case of something like a comparator, it's quite likely that it will be executed more than once, per pair, so you would definitely not get any guarantees there.
I'm getting a bizarre repl error that occurs only when reloading a file:
2. Unhandled clojure.lang.Compiler$CompilerException
Error compiling *cider-repl webtools* at (1:1)
1. Caused by java.lang.RuntimeException
No such var: user/reset
The project compiles fine. The repl starts fine. The problem only happens if I use C-c C-x
to recompile. If I restart the whole repl, it compiles fine. It seems like it's trying to compile the repl buffer, but I can't for the life of me fathom WHY. Wondering if anyone has any ideas what would cause this. The only hit on stack overflow suggests that I must be requiring something :as user
, but I'm definitely not.Tried a lein clean
but that didn't make any difference.
@joshjones : thanks for the eager map; it seems surprising that 1. core has no eager-map 2. many are probaly using (doall (map ...)) to simulate it, but 3. this revolves around assumptions that map doesn't really guarantee
@tjscollins : what is user/reset ? is it part your lein config, part of cider, part of some reload package that you are using? [I use boot]
@qqq User is the init-ns for a repl in my project.clj (lein config).
@qqq so It seems that it expects a symbol user/reset
that suddenly doesn't exist. But looking over my changes from the past day I don't see anything that I would expect to affect that.
@qqq Well, restarting emacs finally seems to have fixed it.
How to avoid reflection warning in gen-class state. Specifically I looking for answer for this question https://stackoverflow.com/questions/6625104/adding-a-type-hint-to-a-clojure-gen-class-state-access
Hello fellow clojurians. I am eager to refactor part of my function that use destructuring but having some problem. Here is a code snippet that has sequence of files that are grouped based on hash, uploaded and flattened back.
(->>
items
(group-by :hash)
(pmap #(let
[[hash [{file :file}] :as group] %]
(if (upload hash file)
(second group))))
(flatten))))
Is there a better way to destructure group into key and values to avoid performing (second group)
?Ok, so the only thing I came up with is:
(->>
items
(group-by :hash)
(pmap #(let
[[hash values] %
[{file :file}] values]
(if (upload hash file)
values)))
(flatten))))
Is it any good?@andre.stylianos, @jahson is correct - values is vector. It may work if we update it:
(->>
items
(group-by :hash)
(pmap (fn [[hash values]]
(when (upload hash (:file (first values)))
values)))
flatten)))
Oops, my bad. Missed that
That should work
@jahson Correct. That is my initial version. But don’t you think destructured version is more readable?
@vadyalex Nope, imo — you need to parse destructuring and for that structure it is not very easy.
when using datomic, if I can freely choose between an SQL backed datomic or Cassandra backed datomic, which one is better?
@tkircsi It's because of the way transducers work. They are applied from right to left, but to the reducer fn.
@tkircsi https://clojure.org/reference/transducers checkout "defining Transformations with transducers"
I'm trying to do some Java interop using a Java library called Gurobi. I'm following their documentation which reads
import gurobi.*;
...creates outer class...
GRBEnv env = new GRBEnv("mip1.log");
GRBModel model = new GRBModel(env);
GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
I am trying to work with this library in clojure. It seems I cannot wildcard import in clojure so I have
(import '(gurobi GRB GRBCObj GRBCallback GRBColumn GRBConstr GRBEnv GRBExpr GRBException GRBGenConstr GRBLinExpr GRBQuadExpr GRBModel GRBSOS GRBVar GRBQConstr GurobiJni))
(def env (new GRBEnv "mip1.log"))
(def model (new GRBModel env))
(.addVar model 0 1 0 GRB.BINARY "x")
however I get the error on the final line "java.lang.ClassNotFoundException" caused by GRB.BINARY. I believe I have imported everything using the extensive import above.@michaellindon Might be a static member, so it could be GRB/BINARY
Or, an inner class, in which case you need to import GRB$BINARY
, but from the upper case spelling, I'd assume the former
How would I convert x.get(GRB.DoubleAttr.X)
to clojure
cider in emacs isnt giving me much help ><
so this worked (.get x gurobi.GRB$DoubleAttr/X)
but i feel like im in the dark
cider isn't giving me any autocompletion help so its hard to find the proper structure
cidre doesn't autocomplete gurobi.GRB$...
at all
it was GRB/BINARY
, thank you
can someone please help me convert x.get(GRB.DoubleAttr.X)
to clojure code
so turned out I needed this
(.get x gurobi.GRB$DoubleAttr/X)
But I'm confused as to why I needed gurobi.GRB$DoubleAttr instead of GRB$DoubleAttr as I already imported gurobi.GRB in my import statement
and when I begin to type gurobi.GRB$Dou....
cider doesn't autocomplete it
@michaellindon Yeah, nested classes are confusing to work with... You can probably directly import GRB$DoubleAttr as well, that should fix it
@qqq @joshjones clojure has eager-map built in, it’s called mapv, and it will perform better than that reduce example, even if you need to call seq on the result to get a list instead of vector
what's the best and most elegant way when using environ/env
to ensure that values that can be coerced end up being so. Right now everything it reads as strings. I'd like values like "true"/"false" and numbers to be converted to be of the right type
ag: Use aero
: https://github.com/juxt/aero
some edge-cases when the exceptions thrown, I end up with code that doesn't look elegant. Clojure has spoiled me: If code doesn't look elegant - I feel it's not worth adding it.
I wonder if plumatic/schema
coercers would help here…
> Coercion is like validation, except a schema-dependent transformation can be applied to the input data before validation.
@ag consider that you’ll read this at startup, and IMHO startup is the perfect time in the run of an app to reject input outright and exit with an error
so I’d say, don’t try/catch, even explicitly throw AssertionErrors if the data isn’t “perfect” - then whoever set up the run environment can use your clear descriptive error message to fix the input and try again
@noisesmith I'm not using schema in this particular project. Seems to be an overkill to have to use it to solve this particular problem
OK - but my point about asserting / throwing / bailing out still stands
RFC, all contributors to and users of nREPL: https://groups.google.com/forum/#!topic/clojure/6SX7q39lK90
@ag you can check out https://github.com/amperity/envoy too
is there a way to change the signature of a defmulti after you decide on which arguments to dispatch?
(defmulti foo (fn [a b] [a b]))
(defmethod foo [:foo :bar] [just-foo?] (do ...))
or does every defmethod have to have the signature [a b]
?so if you have a mutlimethod that can be invoked with differing numbers of arguments, and those differing numbers of arguments are dispatched differently, each method only needs to handle the arity it would be invoked with
if you invoke a multimethod with N arguments, whichever method it dispatches to will be invoked with N arguments, you can't change that
okay, that makes sense. I just didnt want to have to change my signature across a couple files that implement the defmethod *if at all possible
@ag @noisesmith I did exactly that using the json coercer that ships with schema
yeah- that’s what I was thinking of
I have a file in each of my projects that looks like
(def settings {:debug {:schema s/Bool :default false}
:db-uri {:schema sc/URI :default ""}
:redis-uri {:schema sc/URI :default ""}})
and then I have a component that takes a settings map in and can realize those at component/start time to be injected around to whomever needs them
although I have a vendoring of environ.core/env's underlying functions to allow me to invoke it in my start() and thus allow me to stop a system, change java properties, and then start the system again
This is the matcher I use: https://gist.github.com/8da9d6240679bbb57080682b20d4ba98
hello everyone, i have a giant regexp, is it possible to break it into various lines?
or build it from a string (which can be broken into various lines and assembled together with str
)
yes you can do that by calling (re-pattern (str the pieces of the regex))
or java.util.regex.Pattern/compile
@noisesmith @joshjones : I'm an idiot for forgetting mapv; for some reason, I always thought "this was for ppl who wanted vector instead of list" but never eager vs lazy or impure vs pure
So I’m looking at the honeysql README
and it has loads of example code
which is great
except sometimes it goes stale
and someone messages us to tell us that one of the examples in the README doesn’t run anymore
and I was thinking
we could set up a test case that actually parses the README
and finds the backtick-quoted sections and eval
s them
finds the forms preceded by => and treats them as expected results
I’m wondering if anyone’s done this before/if this is something that already exists off the shelf
because if not I could imagine throwing together a sort of ‘literate testing’ library to do this and sharing that
as I understand it, the python community has largely shied away from doctests (although I don't particularly know the reason; it might be an implementation issue)
@michaelblume it should be straightfoward to go the route you proposed using an augmented codeblock transformer with markdown-clj to grab the code blocks. See https://github.com/yogthos/markdown-clj#customizing-the-parser
(just thinking about it from the perspective of reusing existing stuff to avoid writing markdown grammar or some regexes)
@michaelblume Elixir has something like executable documentation that (I think) centers around iex.
Here you go: https://elixir-lang.org/getting-started/mix-otp/docs-tests-and-with.html#doctests
Playing around with spec-ing clojure.core/sort
and I came across something interesting:
(frequencies [Double/NaN Double/NaN])
=> {NaN 1, NaN 1}
@michaelblume If you're willing to live with Midje, there's a plugin to run the readme code as a set of tests -- clj-time uses it.
@seancorfield I’ve got clj-time checked out and I’m actually having a hard time figuring out how to see/run the readme tests
docs say to run lein test-readme but I’m getting no such task
and the test/readme.clj file that got generated looks really short
lein with-profile dev,default,midje test readme
-- only code annotated with clojure
(not clj
) is turned into Midje facts