Fork me on GitHub
#beginners
<
2016-07-19
>
krchia03:07:42

i’m not sure how best to express it, but how do i use “pointers”? i have an om component, and a map {:nodes .. :links ..} links is a vector of node to node connections, and i need links to contain a pointer to the node as the node’s x and y values changes i’m trying to work with d3, if it’s relevant here what happens when i declare a new link is that it just uses the current value of x and y, and doesn’t update when the node’s coordinates change i’m not even sure that pointers are the right term to use, and i haven’t been getting good results from searching “clojurescript pointers"

seancorfield04:07:27

@krchia: Since Clojure(Script) has immutable data structures, when "x and y values change" what you get is a new data structure with updated elements.

seancorfield04:07:45

So even if you had a "pointer" to the original data structure, that isn’t going to help you.

krchia04:07:26

i’m sorry, i’m not sure i understand that example - could you elaborate?

seancorfield04:07:45

I don’t know what to elaborate on.

seancorfield04:07:29

Clojure’s immutability means your x and y values don’t "change" — you get a whole new structure with updated elements.

seancorfield04:07:31

The way folks generally deal with that is by providing repeatable navigation into data structures — so it works with each new structure (i.e., what is generated by "changing" the values contained in it).

seancorfield04:07:55

What is in your :nodes element in that map?

krchia04:07:23

i just got my example to work with a hack

krchia04:07:31

the part where i wrote ;; rebuild links

krchia04:07:48

i’m just going to re-read what you said - i’m not quite getting what you’re saying

krchia04:07:01

apparently, after an event that adds a node to the state, i have to reinitialize the links with the same declarations - i’m not sure why this is so

krchia04:07:10

it obviously won’t scale if i add new functionality to add new links, but i can use this to figure out what’s going on at least

seancorfield04:07:07

Immutability is "why this is so"

seancorfield04:07:50

Because the nodes don’t "change", what you get is just a new nodes data structure. So links references the original unchanged data structure.

krchia04:07:56

oh.. isn’t that kind of inconvenient?

krchia04:07:25

what did you mean by repeatable navigation into data structures?

seancorfield04:07:53

A path into a data structure, e.g., [:left 0 1] such as an argument to get-in.

seancorfield04:07:48

Am I right that your links structure is always built as a set of pairs of source/target for each node pair from 0 .. n-1 (i.e,. the last link is source (n-1) / target (n)?

krchia04:07:21

it could be any node pair 🙂

seancorfield04:07:07

So you could have links from node 0 to node 1 and from node 0 to node 2?

krchia04:07:59

so my attempt at writing a proper reference i.e. (defn get-node [owner idx] (aget (:nodes (om/get-state owner)) idx)) isn’t the proper way?

krchia04:07:52

but the reference to the node in links work BEFORE a mouse event

krchia04:07:00

the x and y coordinates update nicely

krchia04:07:41

i suspect it has something to do with the sequence of d3 calls with .. enter append exit remove when i add something to the node map

seancorfield04:07:55

when you do this (clj->js (conj (js->clj (om/get-state owner :nodes)) {:id lastnodeID … you are converting the JS array to a Clojure vector (a brand new data structure) and then conj’ing the new element, producing another new data structure, then converting that to JS and replacing the old nodes array with this completely new one

seancorfield04:07:00

I think if you change that to (.push (om/get-state owner :nodes) (clj->js {:id lastnodeID :x x :y y})) then the old nodes array will be preserved I think.

krchia04:07:18

oh, i tried that before

seancorfield04:07:37

(and don’t call set-state! on it)

krchia04:07:49

i get Uncaught TypeError: om.core.get_state.call(...).push is not a function

krchia04:07:01

thats why the convoluted statement, sorry

krchia04:07:02

(.log js/console (om/get-state owner :nodes)) yields an array, funnily enough

seancorfield04:07:06

OK so try (let [nodes (om/get-state owner :nodes)] (.push nodes (clj->js {:id lastnodeID …})))

krchia04:07:39

nope, that still doesn’t work! i also tried that

krchia04:07:29

i’m not sure if this is relevant or not

krchia04:07:31

The push() method adds one or more elements to the end of an array and returns the new length of the array.

seancorfield04:07:27

Remember that I’m not suggesting you do anything with the result of calling .push

seancorfield04:07:39

I’m suggesting calling it purely for its side-effects.

seancorfield04:07:07

That’s why I said do not call set-state! on the result.

krchia04:07:17

it works - i’m so happy

seancorfield04:07:39

Basically your nodes is a JS array, so you have to work with it as a mutable structure — not a cljs structure.

krchia04:07:59

a misunderstanding of array.push and a lesson in immutable data structures

krchia04:07:12

i’ve never been one for reading stuff, preferring to try and tinker with stuff

krchia04:07:33

but i think the pain i faced today and yesterday might just persuade me to change my mind about that

krchia04:07:42

thanks so much seancorfield 🙂

seancorfield05:07:58

Clojure rewards careful thinking 🙂

seancorfield05:07:09

I’m out for the night. Have fun with D3!

cored16:07:47

feel like this is the idea behind Clojure

andrepnh16:07:32

I think it's

andrepnh16:07:41

or perhaps it's a consequence of avoiding incidental complexity

andrepnh16:07:57

there's also this blog post that talks about "data orientation"

cored16:07:38

andrepnh: found that article in a page for Go lang

cored16:07:46

I think its also apply for that language

andrepnh16:07:00

I think it does, but I can't say I'm that familiar with Go

cored16:07:41

I've been using Go for six months now; but still my heart is saying that probaly I should spend the same amount of time with Clojure

cored16:07:07

there's something about the Go language that I don't like. Sometime I feel Gophers tend to over simplify stuffs

andrepnh17:07:37

I've dabbled with Go too, but it just didn't click

andrepnh17:07:08

I think it's to close to C and C++, which is not wrong at all, but I wanted to try FP

cored17:07:41

andrepnh: yeap, I'm using it at my current job. We are migrating some components of a Rails app into it