This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
Hello everyone! I was chasing down a weird bug and do not really understand why clojure is showing this weird behaviour with the shorthand function creation #()
.
Why are all of these equal, except for the last one?
((fn [] "hello"))
"hello"
(#(str "hello"))
(#("hello"))
The first 3 work fine, and return "hello"
. The last throws
Unhandled java.lang.ClassCastException
java.lang.String cannot be cast to clojure.lang.IFn
@codemartin: I think it’s because the last one need to actually be a function… it just has data and no operator
@kopasetik: Why so? AFAIK the #()
syntax should be equal to (fn [] )
.
Trying to find out the definition in Clojure source, but getting nowhere closer than this line... https://github.com/clojure/clojure/blob/bc186508ab98514780efbbddb002bf6fd2938aee/test/clojure/test_clojure/reader.cljc#L448
well well
@codemartin: no, it is not equivalent to (fn [] )
. It is like (fn [] )
which wraps its body in ()
@ul: Thank you for the explanation. If you know where this is defined in Clojure source I'd be very interested! Thanks either way.
@codemartin: i think it is here https://github.com/clojure/clojure/blob/bc186508ab98514780efbbddb002bf6fd2938aee/src/jvm/clojure/lang/LispReader.java#L699
@ul: Thank you very much! Will read thru
Anyone good with geometry here? I'm working with the face normal of a 3d polygon. So I have an [x y z]
value and I know that x, y and z will have a value between -1.0 and +1.0. I have a function where I am summing up the values, ala (+ x y z)
, and I need to know what the min/max range can be for the summed values. I believe it is around -1.75 to +1.75 but would like to know for sure, and why.
@meow: I believe the answer is 3 * sqrt(1/3)
, which is 1.7320508075688772 and comes close to your 1.75 number.
@meow: How to formally prove it, I'm not exactly sure. I just started with x^2 + y^2 + z^2 = 1
. Here's where I kind of make a leap... since it's obvious to me that x, y, and z are all going to have to be equal to maximize this, I converted it to x^2 + x^2 + x^2 = 1
. Which is 3x^2 = 1
and solving for x you get x = sqrt(1/3)
. Then multiply by 3. That's how I got my answer.
@meow: I'm back. Sorry, I just wanted to add one more thing. You can solve this as an optimization problem. Basically where you have a constraint and the thing that you want to maximize/minimize, and using calculus you can find the answer. You can solve it by hand, but since that's a bit of work, I'll be lazy and plug it in here: https://www.wolframalpha.com/input/?i=maximize+%28x+%2B+y+%2B+z%29+on+%28x^2+%2B+y^2+%2B+z^2+%3D+1%29
I've got it on my todo list to learn Wolfram, but never have made the time to do so. Would be good for these kinds of problems.
okay, so this is the code I have at this point:
(defn normal-sum-hue []
(fn [mesh]
(let [mesh (op/compute-face-normals mesh)
fc (fn [mesh face]
(let [[x y z] (g/face-normal mesh face)
max-sum (* 3 (Math/sqrt (/ 1 3)))
min-sum (- max-sum)
hue (-> (+ x y z) (m/map-interval min-sum max-sum 0.0 1.0))
sat 1.0 val 1.0 alpha 1.0
color (col/as-rgba (col/hsva hue sat val alpha))]
@color))]
[mesh fc])))
I use http://thi.ng for the mesh data structure, but I'm augmenting it a lot with my own mesh operators and color functions.
Interesting. I didn’t know about http://thi.ng. Thanks
Take this for a spin: https://www.shapeways.com/model/3dtools/4150122/1/26?key=befbe3d4287f5fdcc267affa39d2212f