This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-02-22
Channels
- # beginners (55)
- # cider (22)
- # cljs-dev (123)
- # cljsrn (75)
- # clojars (1)
- # clojure (92)
- # clojure-europe (2)
- # clojure-italy (16)
- # clojure-nl (6)
- # clojure-spec (17)
- # clojure-uk (77)
- # clojured (2)
- # clojurescript (39)
- # core-async (8)
- # cursive (4)
- # data-science (1)
- # datomic (22)
- # duct (4)
- # editors (21)
- # emacs (10)
- # events (4)
- # fulcro (116)
- # graphql (8)
- # immutant (3)
- # jackdaw (1)
- # juxt (3)
- # kaocha (4)
- # luminus (1)
- # mount (1)
- # nrepl (32)
- # off-topic (34)
- # other-languages (5)
- # pedestal (32)
- # reagent (1)
- # ring (6)
- # ring-swagger (7)
- # shadow-cljs (5)
- # spacemacs (3)
- # specter (1)
- # sql (1)
- # vim (21)
If I have an arbitary number of functions I want to run just for effects, is there a handy function for that? So rather than
(do
(f1)
(f2)
(f3))
just
(dofn f1 f2 f3)
@mkeller02 look at run!
and juxt
.
With run!
you'd have to do something like this: (run! (fn [f] (f)) [(fn [] (println "yo")) (fn [] (println "ya"))])
. juxt
is probably better for what you want.
So... anyone seen this before?
Exception in thread "main" java.lang.ClassCastException: cm_metrics.main$_main$fn__8935 cannot be cast to java.base/java.lang.Thread
at cm_metrics.main$_main.invokeStatic(main.clj:30)
at cm_metrics.main$_main.doInvoke(main.clj:21)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.AFn.applyToHelper(AFn.java:152)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at cm_metrics.main.main(Unknown Source)
it's from an uberjar
happens when the -main
function calls (Runtime/getRuntime)
(I think)
@gypsydave5 i dont know what your code looks like but i’m guessing you’re trying to add a shutdown hook? if so, the addShutdownHook method wants a Thread object not a function: (Thread. (fn [] ...))
You're obviously a mind reader 😄
Thanks!
I'm trying to get a clojure cli tool I wrote a while back to compile and run under java 6. So far I've downgraded leiningen and the clojure version and I'm now in the process of trying to find a permutation of the dependencies which would actually compile under java 6 (currently playing with tools.cli) . Just figured I would ping this channel to see if this is even worth it...i.e. is this doomed to fail from start or is there a chance this might actually work in the end?
@alexmiller I'm trying to get the tool to run in a controlled server environment which only has java 6
Has anyone used a recent core.memoize with a cache backend out of process (like leveldb or redis where you need to serialize the objects)? Since I last used it, it seems to make heavier use of custom delay objects and I wondered if anyone else has encountered issues
@mbjarland that's what I was asking. I don't think it's hopeless (but might not be fun)
@alexmiller ok thanks! ...yeah, not fun which is why I figured I would ask first before banging my head against the wall
hey everyone, this might be more of a java question but i’m looking to store a hex number, for example 1ff as a byte-array
casting to and from bigintegers results in truncating the 1, and i’m not quite sure how to get around that
i guess the super hacky route would be to mod the number of hex characters against 8 and supplement zeroes..
you might look at java.util.BitSet
not sure if it's exactly what you want as an api but prob good code to build on
0x00 0x01 0xFF isn't a "hex number" it is 3 bytes, so when you try to store it as 1 byte it is truncated to 0xff
3 bytes is kind of an odd size to work with, usually it will be some power of two, 2 or 4 would fit, and match the sizes of some primitives on the jvm (short and int)
if you want a byte array with the contents describing some Long / Int / whatever you can use ByteBuffer https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#putInt(int)
bigintegers will add an extra byte to track the sign sometimes, where if you are mostly concerned with bytes but sometimes with numbers you often want to assume unsigned and not have the extra byte
(cmd)user=> (def bb (ByteBuffer/allocate 16))
#'user/bb
(ins)user=> (seq (.array bb))
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
(ins)user=> (.putLong bb 42)
#object[java.nio.HeapByteBuffer 0x640f11a1 "java.nio.HeapByteBuffer[pos=8 lim=16 cap=16]"]
(cmd)user=> (seq (.array bb))
(0 0 0 0 0 0 0 42 0 0 0 0 0 0 0 0)
(ins)user=> (.putLong bb Long/MAX_VALUE)
#object[java.nio.HeapByteBuffer 0x640f11a1 "java.nio.HeapByteBuffer[pos=16 lim=16 cap=16]"]
(cmd)user=> (seq (.array bb))
(0 0 0 0 0 0 0 42 127 -1 -1 -1 -1 -1 -1 -1)
this is the most straightforward way I've found for converting between the internal representation of primitive types and the actual values (for every putFoo method there's a getFoo method)
@gtzogana were you trying to use the toByteArray method? https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toByteArray()
so what was your actual task? turn a byte array into a number?
no, i was trying to store a series of bits that may or may not fit neatly into bytes in a byte array
OK - you can write bits (or any other type of course...) one by one with ByteBuffer, then read out whatever types you like
Why isn't group-by
"transducable"? Is it because it needs to scan the entire coll?
you can do something similar as a transformation of a step function if you have input sorted by your grouping key and a transducer version of partition-by
it can't produce any intermediate results, so not really any point
OK, so my group-by is in the middle of a long chain of transformations
my only option, it seems, is to have 2 transducers
I could imagine a hypothetical case where a groups-by function might be useful, and that could be a transducer (reductions : reduce :: groups-by : group-by)
reductions gives a lazy seq of all accumulator values for a given reducing function and input, so groups-by could give all the intermediate groups as you consume inputs - but there's few places that would be useful, if any
have you considered what it means to have a group-by in the middle of a long chain of transformations?
https://github.com/aphyr/tesser has a group-by operation with slightly different semantics that is very interesting
@alexmiller My feeling is that it's not a single sequence of transformations. Rather, two, unrelated map-reduce piplines
@hiredman Thanks. My understanding of how transducers work is still very basic, so I'll have to read more about this
that seems healthy :)
tesser has a slightly different model of folds then transducers does, but is very useful for inspiration, and speaking of map/reduce tesser can run the folds as hadoop jobs
(defn groupy-by-transducer [key-fun]
(fn [f]
(fn
([] (f))
([x] (into {} (map (fn [[k v]] [k (f v)])) x))
([accum value]
(let [k (key-fun value)]
(assoc accum k (f (get accum k (f)) value)))))))
(transduce
(comp
(groupy-by-transducer :type)
(map :mass))
+
{}
[{:name :electron, :type :lepton, :mass 0.51}
{:name :muon, :type :lepton, :mass 105.65}
{:name :up, :type :quark, :mass 1.5}
{:name :down, :type :quark, :mass 3.5}])
;;=> {:lepton 106.16000000000001, :quark 5.0}
that kind of works, but I think it might run afoul of the internal mutations that some transducers do
I'm trying to add metadata to a function, but it's annoying because it erases the function name in the print-out:
binja.util> (fn hey ([x] x))
#function[binja.util/eval9096/hey--9097]
binja.util> (with-meta (fn hey ([x] x)) {:x "x"})
#function[clojure.lang.AFunction/1]
I recommend not putting metadata on functions because they have reference equality but value metadata
you can implement IFn / IMeta and have any toString you like via reify, deftype, whatever
or fork clojure, but implementing IFn and IMeta is a lot less work
adding metadata to a function results in wrapping it with an instance of another function with that metadata
which is part of why (= x (with-meta x {}))
which holds true for other types with-meta works on fails for functions
hmm well that's disappointing. maybe i can figure out how to store the metadata inside the function and fetch it somehow when I need it
cmd)user=> (def f (reify clojure.lang.IFn (invoke [this] :a) (applyTo [this _] :b) Object (toString [this] "f")))
#'user/f
(ins)user=> (def g (with-meta f {:foo :bar}))
#'user/g
(ins)user=> (meta g)
{:foo :bar}
(ins)user=> (meta f)
{:line 53, :column 8}
(ins)user=> (f)
:a
(ins)user=> (g)
:a
(ins)user=> [f, g]
[#object[user$reify__233 0x36676c1a "f"] #object[user$reify__233 0x5b408dc3 "f"]]
(reify is already IMeta so no need to implement)
thanks @noisesmith! But I mostly need it for stack trace errors so it needs to show up as a function, not just a reify
but, again, metadata on functions is kind of silly, metadata is more or less extra data you want to attach to an object without changing the equality of that object
b.util > (def f (reify clojure.lang.IFn (invoke [this] :a) (applyTo [this _] :b) Object (toString [this] "f")))
#'b.util/f
b.util> (f 3)
Execution error (AbstractMethodError) at b.util/eval9131 (form-init14739800243932352027.clj:2461).
Receiver class b.util$reify__9129 does not define or inherit an implementation of the resolved method abstract invoke(Ljava/lang/Object;)Ljava/lang/Object; of interface clojure.lang.IFn.
you need to define the arity you call
you only defined the 0 arg applyto invoke
well i'm storing the type information of the args and return value for a function, which I can check with a spec, so I just need some way to get that out of a function. i could hide it in the function so if you call it with the magic incantation it returns the type