Nullable Double Type-Hint - clojure

I'm writing a neural net, and have the following definition of a Node:
(defrecord Node [^double input-sum ^double last-output])
input-sum is the running sum of it's input; pre-activation. last-output is it's activated value.
My original thought was to create an initial node like (->Node 0 nil). My rational was it didn't make any sense to give the last-output a real value before it had ever been activated.
Unfortunately, creating the above Node yields a NPE; apparently because it's attempting to cast the nil as a double:
(->Node 0 nil)
NullPointerException clojure.lang.RT.doubleCast (RT.java:1298)
If I remove the type-hint on last-output, it works fine.
I'm trying to get into the habit of type-hinting what I can. Is there a way to provide the type-hint on last-output, but also indicate that nil is an acceptable value?

If you really need to typehint with a primitive type, and using object type Double is not an option (like for performance related reason), the common practice (as far as i know) is to create your own custom constructor function:
user> (defrecord Node [^double input-sum ^double last-output])
user.Node
user> (defn make-node [^Double input-sum ^Double last-output]
(->Node (or input-sum 0) (or last-output 0)))
#'user/make-node
user> (make-node nil 0)
#user.Node{:input-sum 0.0, :last-output 0.0}
user> (make-node 10 nil)
#user.Node{:input-sum 10.0, :last-output 0.0}
user> (make-node 1 2)
#user.Node{:input-sum 1.0, :last-output 2.0}
the approach is used for all the cases, where you need some sophisticated logic for the entity construction.
update
If you really need both a primitive value and the way to distinguish value from "no value", you could use NaN for that:
user> (defn make-node [^Double input-sum ^Double last-output]
(->Node (or input-sum Double/NaN) (or last-output Double/NaN)))
#'user/make-node
user> (make-node 1 nil)
#user.Node{:input-sum 1.0, :last-output NaN}
so, it is value, and also is the primitive analogue of nil.

^double is type hint for Java primitive double type. Try ^Double.

Related

Why use (*')'s implicit 1 over an explicit 1?

From the clojure docs, the function *':
Returns the product of nums. (*') returns 1. Supports arbitrary
precision. See also: *
I understand the use cases for arbitrary precision as explained in the example provided:
;; great so it gives the same results as *.
;; not quite check this out
(* 1234567890 9876543210)
;; ArithmeticException integer overflow
(*' 1234567890 9876543210)
;;=> 12193263111263526900N
However, the (*') returns 1 does not seem to have any use at all since you can just specify the explicit value. In the same example provided:
;; there is an implicit 1
(*')
;;=> 1
;; the implicit 1 comes into play
(*' 6)
;;=> 6
I had thought that perhaps it would be useful if the second argument is not defined, perhaps nil but:
(*' 6 nil)
Throws a NullPointerException.
Why would you use (*' 6) over 6 and (*') over 1?
* and *' are equivalent in this regard:
first of all - how would you otherwise handle the [] and [x] cases in *?
The source as it is written makes the most sense and AFAIK is mathematicly correct (identity value and such).
user=> (source *)
(defn *
"Returns the product of nums. (*) returns 1. Does not auto-promote
longs, will throw on overflow. See also: *'"
{:inline (nary-inline 'multiply 'unchecked_multiply)
:inline-arities >1?
:added "1.2"}
([] 1)
([x] (cast Number x))
([x y] (. clojure.lang.Numbers (multiply x y)))
([x y & more]
(reduce1 * (* x y) more)))
second, it makes it much more robust in cases other then manually writing (* 2 4). I wouldn't write (*) to mean 1.
Like the following - see the single element and emtpy vector on pos 3 and 4.
user=> (map (partial apply *) [[2 2] [2 3 2] [6] []])
(4 12 6 1)
Usually the 0-arity of a function is useful for a reduction or for applying to lists. For example:
(apply *' (range 5))
The multiplicative identity is one.
In terms of code and clojure, birdspider and Alejandro C. both answered about the (apply * []) case, in which it is indeed useful for (*) to equal 1.
However, I want to point out another case where (*) is useful. When passed to transduce as a reducing function.
transduce docstring:
clojure.core/transduce
[xform f coll]
[xform f init coll]
Added in 1.7
reduce with a transformation of f (xf). If init is not
supplied, (f) will be called to produce it. f should be a reducing
step function that accepts both 1 and 2 arguments, if it accepts
only 2 you can add the arity-1 with 'completing'. Returns the result
of applying (the transformed) xf to init and the first item in coll,
then applying xf to that result and the 2nd item, etc. If coll
contains no items, returns init and f is not called. Note that
certain transforms may inject or skip items.
The relevant part here is "If init is not supplied, (f) will be called to produce it.".
So using * as the rf without an init value will use an init value of 1. Which is probably what you want.
Similarly (+) ;=> 0 (additive identity) and (conj) ;=> [].
I think this is a neat example of things coming together nicely, as (*) was decided long before transducers were a thing.
The default value (*) => 1 is there to avoid a NullPointerException in the event you don't know how many items you want to multiply. That is:
=> (apply * [1 2 3])
6
=> (apply * [3])
3
=> (apply * [])
1
=> (apply * nil)
1
=> (* nil)
nil
=> (*)
1
Wow! I am surprised! I couldn't force a NPE even when I tried!
I still think this behavior provides a false sense of security. If I am expecting to multiply some numbers and one (or more) are missing, I'd rather have the default be to detect the error via an Exception. As it is, the default is for Clojure to cover-up the error by being "helpful" and trying to guess what I may have meant.
Update
I did finally generate an NPE:
> (* 5 nil) => NullPointerException clojure.lang.Numbers.ops (Numbers.java:1013)
I find this even more surprising. If (* nil) => nil, I would have bet money that (* 5 nil) => nil would also be true. Strange!

How to do a filter with varargs?

I have a map of users and their favorite bands:
(def data
{
:David {"Tribalistas" 3.0
"Daft Punk" 5.0
"Lorde" 4.0
"Fall Out Boy" 1.0}
:Matt {"Imagine Dragons" 3.0
"Daft Punk" 4.0
"Lorde" 4.0
"Fall Out Boy" 1.0}
:Ben {"Kacey Musgraves" 4.0
"Imagine Dragons" 3.0
"Lorde" 3.0
"Fall Out Boy" 1.0}
}
)
and I need to filter the results that have two keys in common, in this case, band1 and band2
(defn common-ratings [band1 band2 ratings]
(filter #(and ((second %) band1) ((second %) band2)) ratings))
(common-ratings "Daft Punk" "Lorde" data) ; should return David and Matt lines
but now, I need to transform the bands in a varargs, I tried to use something like:
apply and...
So I can use the function like this:
(common-ratings "Daft Punk" "Lorde" "Another band" "Another Band2" data)
but It does not work.
Thanks in advance
As has been said, and is a macro, so cannot be an argument to a
function, apply or any other.
The closest standard function to and is every?.
Since you have an unknown number of bands, pass them as a collection:
(defn common-ratings [bands ratings]
(filter #(every? (val %) bands) ratings))
... where I've replaced second with val to show that we're dealing with map entries.
For example,
(common-ratings ["Daft Punk" "Lorde"] data)
;([:David {"Tribalistas" 3.0, "Daft Punk" 5.0, "Lorde" 4.0, "Fall Out Boy" 1.0}] [:Matt {"Imagine Dragons" 3.0, "Daft Punk" 4.0, "Lorde" 4.0, "Fall Out Boy" 1.0}])
If you want to pass the bands as individual arguments, put them last to capture them as a rest argument:
(defn common-ratings [ratings & bands]
... )
... which you call like this:
(common-ratings data "Daft Punk" "Lorde")
... with the same effect as before.
Thank you a lot guys, my final solution is pretty close to #Thumbnail solution:
(defn common-ratings [& bands] (filter #(every? (second %) bands) data))
One of the options is to wrap and macro into function:
(defn and-fn [x y] (and x y))
Then you can use reduce instead of apply to achieve the same result:
(reduce and-fn [true true true false]) ; => false
For convenience, last reduce can be wrapped into another function:
(defn and-multi [& args]
(reduce #(and %1 %2) true args)) ; initial value is needed for invocation with no args
Now it will behave very similar to normal and macro:
(and-multi) ; => true
(and-multi false true) ; => false
(apply and-multi [true true true]) ; => true
It will even have same behaviour with the evaluation of args:
(and false (range)) ; => false (despite of second param being infinite sequence)
(and-multi false (range)) ; => false

clojure.algo.monad strange m-plus behaviour with parser-m - why is second m-plus evaluated?

I'm getting unexpected behaviour in some monads I'm writing.
I've created a parser-m monad with
(def parser-m (state-t maybe-m))
which is pretty much the example given everywhere (here, here and here)
I'm using m-plus to act as a kind of fall-through query mechanism, in my case, it first reads values from a cache (database), if that returns nil, the next method is to read from "live" (a REST call).
However, the second value in the m-plus list is always called, even though its value is disgarded (if the cache hit was good) and the final return is that of the first monadic function.
Here's a cutdown version of the issue i'm seeing, and some solutions I found, but I don't know why.
My questions are:
Is this expected behaviour or a bug in m-plus? i.e. will the 2nd method in a m-plus list always be evaluated even if the first item returns a value?
Minor in comparison to the above, but if i remove the call
_ (fetch-state) from checker, when i evaluate that method, it
prints out the messages for the functions the m-plus is calling
(when i don't think it should). Is this also a bug?
Here's a cut-down version of the code in question highlighting the problem. It simply checks key/value pairs passed in are same as the initial state values, and updates the state to mark what it actually ran.
(ns monads.monad-test
(:require [clojure.algo.monads :refer :all]))
(def parser-m (state-t maybe-m))
(defn check-k-v [k v]
(println "calling with k,v:" k v)
(domonad parser-m
[kv (fetch-val k)
_ (do (println "k v kv (= kv v)" k v kv (= kv v)) (m-result 0))
:when (= kv v)
_ (do (println "passed") (m-result 0))
_ (update-val :ran #(conj % (str "[" k " = " v "]")))
]
[k v]))
(defn filler []
(println "filler called")
(domonad parser-m
[_ (fetch-state)
_ (do (println "filling") (m-result 0))
:when nil]
nil))
(def checker
(domonad parser-m
[_ (fetch-state)
result (m-plus
;; (filler) ;; intitially commented out deliberately
(check-k-v :a 1)
(check-k-v :b 2)
(check-k-v :c 3))]
result))
(checker {:a 1 :b 2 :c 3 :ran []})
When I run this as is, the output is:
> (checker {:a 1 :b 2 :c 3 :ran []})
calling with k,v: :a 1
calling with k,v: :b 2
calling with k,v: :c 3
k v kv (= kv v) :a 1 1 true
passed
k v kv (= kv v) :b 2 2 true
passed
[[:a 1] {:a 1, :b 2, :c 3, :ran ["[:a = 1]"]}]
I don't expect the line k v kv (= kv v) :b 2 2 true to show at all. The final result is the value returned from the first function to m-plus, as I expect, but I don't expect the second function to even be called.
Now, I've found if I pass a filler into m-plus that does nothing (i.e. uncomment the (filler) line) then the output is correct, the :b value isn't evaluated.
If I don't have the filler method, and make the first method test fail (i.e. change it to (check-k-v :a 2) then again everything is good, I don't get a call to check :c, only a and b are tested.
From my understanding of what the state-t maybe-m transformation is giving me, then the m-plus function should look like:
(defn m-plus
[left right]
(fn [state]
(if-let [result (left state)]
result
(right state))))
which would mean that right isn't called unless left returns nil/false.
Edit:
After looking at state-t and maybe-m source, the m-plus looks more like:
(fn [& statements]
(fn [state]
(apply (fn [& xs]
(first (drop-while nil? xs)))
(map #(% state) statements))))
But the principle is the same, (first (drop-while nil? ...) should only execute over the items that return a valid value.
I'd be interested to know if my understanding is correct or not, and why I have to put the filler method in to stop the extra evaluation (whose effects I don't want to happen).
Edit:
If I switch over to using Jim Duey's hand written implementation of parser-m (from his excellent blogs), there is no evaluation of the second function in m-plus, which seems to imply the transformation monad is breaking m-plus. However, even in this implementation, if I remove the initial (fetch-state) call in the checker function, the domonad definition causes the output of the creation of the m-plus functions, suggesting something going on in domonad's implementation I'm not expecting.
Apologies for the long winded post!

implement in Clojure integer? in scheme

I'm new to Clojure, and can't find an equivalent of integer? in Chez scheme 8.4, mainly for test cases as below:
(integer? 39.0)
=> #t
The function I've come up so far is:
(defn actual-integer? [x] (or (= 0.0 (- x (int x))) (integer? x)))
Does it work when x is arbitrary number types or is there a better solution?
Thanks.
Well, strictly speaking 39.0 isn't an integer literal because it has the .0 part at the end. A simple implementation of the procedure would be:
(defn actual-integer? [x] (== (int x) x))
Notice that the == operator:
Returns non-nil if nums all have the equivalent value (type-independent), otherwise false

Clojure Partial Application - How to get 'map' to return a collection of functions?

I have a function that I basically yanked from a discussion in the Clojure google group, that takes a collection and a list of functions of arbitrary length, and filters it to return a new collection containing all elements of the original list for which at least one of the functions evaluates to true:
(defn multi-any-filter [coll & funcs]
(filter #(some true? ((apply juxt funcs) %)) coll))
I'm playing around with making a generalizable solution to Project Euler Problem 1, so I'm using it like this:
(def f3 (fn [x] (= 0 (mod x 3))))
(def f5 (fn [x] (= 0 (mod x 5))))
(reduce + (multi-any-filter (range 1 1000) f3 f5))
Which gives the correct answer.
However, I want to modify it so I can pass ints to it instead of functions, like
(reduce + (multi-any-filter (range 1 1000) 3 5))
where I can replace 3 and 5 with an arbitrary number of ints and do the function wrapping of (=0 (mod x y)) as an anonymous function inside the multi-any-filter function.
Unfortunately this is past the limit of my Clojure ability. I'm thinking that I would need to do something with map to the list of args, but I'm not sure how to get map to return a list of functions, each of which is waiting for another argument. Clojure doesn't seem to support currying the way I learned how to do it in other functional languages. Perhaps I need to use partial in the right spot, but I'm not quite sure how.
In other words, I want to be able to pass an arbitrary number of arguments (that are not functions) and then have each of those arguments get wrapped in the same function, and then that list of functions gets passed to juxt in place of funcs in my multi-any-filter function above.
Thanks for any tips!
(defn evenly-divisible? [x y]
(zero? (mod x y)))
(defn multi-any-filter [col & nums]
(let [partials (map #(fn [x] (evenly-divisible? x %)) nums)
f (apply juxt partials)]
(filter #(some true? (f %)) col)))
I coudn't use partial because it applies the arg in the first position of the fn. We want it in the second position of evenly-divisible? We could re-arrange in evenly-divisible? but then it would not really look correct when using it standalone.
user=> (reduce + (multi-any-filter (range 1 1000) 3 5))
233168