Fork me on GitHub
#clojure
<
2015-12-26
>
codemartin08:12:40

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

kopasetik08:12:45

@codemartin: I think it’s because the last one need to actually be a function… it just has data and no operator

codemartin09:12:43

@kopasetik: Why so? AFAIK the #() syntax should be equal to (fn [] ).

codemartin09:12:00

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

kopasetik09:12:14

Yeah, just was making a shot in the dark 🤷

ul09:12:34

@codemartin: no, it is not equivalent to (fn [] ). It is like (fn [] ) which wraps its body in ()

ul09:12:45

so first form must be callable

jindrichm10:12:21

Can you juxt transducers?

codemartin11:12:08

@ul: Thank you for the explanation. If you know where this is defined in Clojure source I'd be very interested! Thanks either way.

codemartin13:12:57

@ul: Thank you very much! Will read thru

meow15:12:38

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.

solicode15:12:49

@meow: I believe the answer is 3 * sqrt(1/3), which is 1.7320508075688772 and comes close to your 1.75 number.

meow16:12:49

@solicode: Cool. How did you arrive at that?

solicode16:12:00

@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.

solicode18:12:29

@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

solicode18:12:42

So that confirms the answer from earlier.

meow18:12:31

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.

meow18:12:55

@solicode: thanks for the followup.

meow18:12:06

What I don't yet see is how you went from x to x^2

solicode18:12:29

I did that because it’s a vector and you want to find the length, right?

solicode18:12:37

and = 1 because it’s the unit vector

meow18:12:51

right - and it's been a long time since I did this kind of math

solicode18:12:26

Me too. Which is why this was fun. I needed this. simple_smile

meow18:12:04

conceptually [x y z] represent any position on a unit sphere

meow18:12:26

meaning r=1

solicode18:12:48

Right, so the vector from (0, 0, 0) to (x, y, z)

meow18:12:25

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])))

solicode18:12:30

Great. What are you using to generate the image?

sarcilav18:12:14

not sure if are the colors, but the lower left corner doesn't look circular

sarcilav18:12:27

the red-yellow one

meow18:12:37

As opposed to the normal-mod1-sum-hue

meow18:12:39

I have code that creates various polygon meshes and outputs to X3D files.

meow18:12:45

These are just png snapshots from an X3D viewer app.

solicode18:12:04

Ah, I see. So no special Clojure libraries for this

meow18:12:48

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.

meow19:12:06

@sarcilav: I spend a lot of time spinning these things around in an X3D viewer. simple_smile

solicode19:12:42

Interesting. I didn’t know about http://thi.ng. Thanks

max20:12:37

I’d like to implement a lazy record. Basically like a lazy map, but a record

max20:12:28

My first idea was to proxy a record and redefine get, but I can’t do this because the class is final

VerifyError Cannot inherit from final class  java.lang.ClassLoader.defineClass1 (ClassLoader.java:-2)

max20:12:57

any suggestions for the easiest way to go about doing this?

meow20:12:18

@max It might not be the easiest way, but you could use a deftype and implement all the mapping protocols.