Fork me on GitHub
#clojure-uk
<
2018-08-29
>
Rachel Westmacott07:08:33

loving yesterday’s history lesson 🧡

otfrom07:08:46

@peterwestmacott I always feel the remarkable thing is when we actually talk about clojure on this channel. 😉

Rachel Westmacott07:08:08

this morning I want to talk theatre!

Rachel Westmacott07:08:09

And I’m super excited about it! But obviously we can talk Clojure too!

dominicm07:08:27

plays sound better

otfrom07:08:21

what is it like directing a play?

Rachel Westmacott07:08:31

@otfrom variable. Depends hugely on the cast.

Rachel Westmacott07:08:45

In this case, lots of fun. It's a smallish cast, and they're all excellent, so we can really sink our teeth into the text.

Rachel Westmacott07:08:21

At least half the battle is getting the right people to audition in the first place.

alexlynham08:08:30

morrrrrrrning

alexlynham08:08:23

@dominicm I feel like I need to say - you know our partial vs anon fn conversation of a few weeks ago? I was doing some data structure wrangling at the weekend, reached for a partial, decided to do it your way, and it's actually probably more readable, so fair play 😉

alexlynham08:08:41

turns out I may have been doing it wrong for oh, years lol

dominicm08:08:29

@alex.lynham It's good to hear my experience can be replicated 🙂

dominicm08:08:38

Sometimes I worry I live in too much of a bubble.

alexlynham08:08:38

I think if I'm comping then maybe I'd still use partial

alexlynham08:08:09

but for shifting the argument order in a thread, just defining an anon (and then refactoring if it's useful elsewhere) is actually super readable

alexlynham08:08:18

good shout 👍

otfrom08:08:54

what was the upshot of the conversation? (I missed it) When to use (fn ...) and (partial...)?

dominicm08:08:34

@otfrom tl;dr never use partial, always use #() or (fn)

cddr08:08:46

Morning 🙂 Is there a way to "unwrap" an instance of a java numeric type (e.g. java.lang.Double) when the numeric type might be a variety of different things (but guaranteed to be number?).

dominicm08:08:47

@cddr I don't know what unwrap means here?

cddr08:08:58

Oh. get the primitive number

otfrom08:08:34

Aww... I like partial in some instances, esp when doing higher order functions that have lookups and other things passed in

alexlynham10:08:54

yep 100% ... I think I'm won over in the case of threading (i.e. not using partial to shift argument order) but there are cases I'll still use it

otfrom11:08:41

yeah, I tend to avoid it in threading as well, but I like it in a map/filter/etc

dominicm08:08:23

@cddr I suspect you're into rolling your own function here, I've not encountered this.

dominicm08:08:55

@otfrom it's not as readable though, because the call interface isn't very well defined.

Conor08:08:44

If it extends java.lang.Number it should have shortValue(), intValue() etc. methods available, I think

dominicm08:08:33

@conor.p.farrell I'm guessing that he wants to use the one appropriate for each type, automatically.

cddr08:08:26

Yeah background is I'm trying to pass a value returned from one API that returns an object, to the other which has a method overload for each supported primitive type.

Rachel Westmacott08:08:03

I think you’ll need a code path for each primitive type you’re using. Even if it’s just a branch in a cond or something.

👍 4
Conor08:08:45

Having to care about the data type (to me) usually means something is slightly wrong

Rachel Westmacott08:08:57

something is slightly wrong

Rachel Westmacott08:08:16

but in my opinion, it’s the scar across the Java language that is primitives vs. objects

Conor08:08:56

The past cannot be changed, I've learned this from many science fiction stories

Rachel Westmacott08:08:56

many Java APIs have one method for objects and eight almost identical methods for primitives

Rachel Westmacott08:08:31

you could hide it a bit with a multimethod or something but I’m not sure that’s any better than an obvious / explicit branch

Rachel Westmacott08:08:00

if something is slightly wrong it’s usually better to make subsequent maintainers aware of it I feel

jasonbell10:08:42

morning

🌊 4
jasonbell10:08:51

I've not been contributing much because I've been wading through season 3 of Twin Peaks.

❤️ 8
bronsa10:08:35

did you make it through it yet?

jasonbell10:08:16

ep 10, ep 8 was the best bit of television I've seen in a long time.

bronsa10:08:15

and you still got 8 episodes to go, you're in for a treat :)

bronsa10:08:17

it just keeps getting better

danielneal13:08:24

oh god twin peaks 3

danielneal13:08:35

that reminds me I still have fwwm to watch

cddr15:08:49

Regarding the question above... here's what I ended up with. Also replaces +-inf with +-max-value because I've seen influxdb (the eventual target) choke on infinity before.

(defn bridged-metric
  "Bridges the kafka metric API with the dropwizard API by taking the supplied `kafka-metric`
   and returning an object that satisfies both interfaces. This allows the underlying
   kafka metrics to participate in dropwizard's registry.

   The additional information provided in the `MetricName` is thus made available to the
   reporter for event tagging purposes"
  [kafka-metric]
  (let [metric-name (.metricName kafka-metric)
        value (fn [x]
                (let [-inf? (fn [n]
                              (= n (Float/NEGATIVE_INFINITY)))
                      +inf? (fn [n]
                              (= n (Float/POSITIVE_INFINITY)))]
                  (condp instance? x
                    Integer  (.intValue x)
                    Double   (cond
                               (-inf? x) (Double/NEGATIVE_INFINITY)
                               (+inf? x) (Double/POSITIVE_INFINITY)
                               :else (.doubleValue x))
                    Float    (cond
                               (-inf? x) (Float/MIN_VALUE)
                               (+inf? x) (Float/MAX_VALUE)
                               :else (.floatValue x)))))]
    (reify
      org.apache.kafka.common.Metric
      (metricName [this]
        (.metricName kafka-metric))

      (metricValue [this]
        (value (.metricValue kafka-metric)))

      com.codahale.metrics.Gauge
      (getValue [this]
        (value (.metricValue kafka-metric))))))

Rachel Westmacott07:08:18

Does the -inf function defined with Float/NEGATIVE_INFINITY work for doubles? (it looks a bit odd, but I genuinely don’t know)