Re: destructuring let and for
Thanks for the answer, I'll try to do as you suggest. Thanks On Tuesday, June 10, 2014 11:51:59 PM UTC+2, Paul G wrote: > > Hi Francesco, > > I apologize for using "for" earlier, as I did not explain how you should > be using it. The comment of "do something with rel" should have indicated > that it resolved to an expression, and those expressions would be returned > in a seq (the "for" expression resolves to have a value of a seq). > > Given your last example, I could read it a couple of ways. My first > interpretation is that you just need a way to convert names to nodes, and > then to connect all the nodes. My preference would be to separate these > steps, so the conversion would be done with: > > (map convert-to-node list-of-names) > > The result of this is then looped through to create the relations (as in > my previous example, the relations are both ways, and not reflexive): > > (let [nodes (map convert-to-node list-of-names)] > (for [node1 nodes, node2 nodes :when (not= node1 node2)] > (nrl/create conn page1 page2 :links))) > > This creates a seq of all the relations, in both directions. So one > relationship will be [Jupiter->Pluto] and another will be [Pluto->Jupiter]. > To create one-way relationships, I suggest the use of indexes, as in my > previous post. > > However, when I followed your link to the example data, I see that there > are connections between some nodes and not others. In that case it would be > preferable to create a mapping of names to nodes, and then as the > relationships are required those nodes can be found easily: > > (def create-nodemap [names] > (zipmap names (map convert-to-node names))) > > Now you can grab the nodes as needed to work with them: > > (defn connect [nodemap name1 name2] > (nrl/create conn (nodemap name1) (nodemap name2 :link))) > > (let [nodemap (create-nodemap ["jupiter" "hercules" "neptune" "pluto"])] > (connect nodemap "jupiter" "hercules" :link) > (connect nodemap "jupiter" "neptune" :link) > (connect nodemap "jupiter" "pluto" :link) > (connect nodemap "neptune" "pluto" :link)) > > I am avoiding many tricks here that I'd use in reality, to avoid making it > too obscure. > > Note that the above has links between Juptier, Neptune and Pluto, but > Hercules is only linked to Jupiter. So we don't have everything connected > to everything else. However, this approach is only useful in simple > examples like this, since any real data set would be too large to write > code for every connection. Instead, the actual data (coming from a csv > file, or something) would be read and used to create the connections. > > What does your original data look like? Are you just trying to re-create > the graph you linked to, or is the data available in some structured format? > > Paul > > > > On Tue, Jun 10, 2014 at 4:08 PM, Francesco Lunelli > wrote: > >> Thanks for the answer. >> If I understand well your code it's a partial answer to my question >> because if I'm not wrong it creates a link between to elements at a time. >> What I need is to create all the elements then create on them every kind of >> relation. >> I take as example a graph db used in another engine >> https://raw.githubusercontent.com/wiki/thinkaurelius/titan/images/graph-of-the-gods-2.png >> Here you have a graph of Roman gods, with the name of the gods and the >> links among them. So if I have for example a list of the names of Roman >> gods I need to create all the nodes, then connect them in various mode. >> I want to take the list '("jupiter" "saturn" "hercules" "neptune" "pluto" >> ...) and cycling on it I want to creare the nodes. When I finished to >> create them I need to be able to call every node to crete the various links >> among them. For example I need to link jupiter with pluto, jupiter with >> neptun, neptun with pluto and so on. >> >> Thanks >> >> Francesco >> >> >> On Tuesday, June 10, 2014 7:43:21 PM UTC+2, Paul G wrote: >> >>> Hi Francesco, >>> >>> You want to decouple your code from the data that it is operating on, so >>> your code can operate regardless of the contents of the list. Otherwise, >>> you'll need code that matches the list, in which case it could all be code >>> anyway. Operating on arbitrary lists makes it easy to test simple examples >>> too. >>> >>> A
Re: destructuring let and for
Thanks for the answer. If I understand well your code it's a partial answer to my question because if I'm not wrong it creates a link between to elements at a time. What I need is to create all the elements then create on them every kind of relation. I take as example a graph db used in another engine https://raw.githubusercontent.com/wiki/thinkaurelius/titan/images/graph-of-the-gods-2.png Here you have a graph of Roman gods, with the name of the gods and the links among them. So if I have for example a list of the names of Roman gods I need to create all the nodes, then connect them in various mode. I want to take the list '("jupiter" "saturn" "hercules" "neptune" "pluto" ...) and cycling on it I want to creare the nodes. When I finished to create them I need to be able to call every node to crete the various links among them. For example I need to link jupiter with pluto, jupiter with neptun, neptun with pluto and so on. Thanks Francesco On Tuesday, June 10, 2014 7:43:21 PM UTC+2, Paul G wrote: > > Hi Francesco, > > You want to decouple your code from the data that it is operating on, so > your code can operate regardless of the contents of the list. Otherwise, > you'll need code that matches the list, in which case it could all be code > anyway. Operating on arbitrary lists makes it easy to test simple examples > too. > > A simple way to create links between pages might be like this (assuming > that conn is in scope and initialized): > > (for [list-elt1 the-list, list-elt2 the-list :when (not= list-elt1 > list-elt2)] > (let [page1 (page-fn list-elt1) > page2 (page-fn list-elt2) > rel (nrl/create conn page1 page2 :links)] > ;; do something with rel > )) > > This creates links both ways. > > If you only want one-way links then the first approach that comes to mind > (there are others, and they're probably better) is to index through the seq: > > (for [n (range (count the-list)), m (range (inc n) (count the-list))] > (let [page1 (page-fn (nth the-list n)) > page2 (page-fn (nth the-list m)) > rel (nrl/create conn page1 page2 :links)] > ;; do something with rel > )) > > The general idea here is to use an arbitrary var to represent your list > elements and pages. > > Does this address your issue? > > Regards, > Paul > > > > On Tue, Jun 10, 2014 at 1:04 PM, Francesco Lunelli > wrote: > >> Thanks for the answer, I try to explain better what I have to do. >> >> I need to create some nodes in Neo4j then I need to create relationships >> between those nodes. >> >> This is an example taken from necons doc. >> >> (let [conn (nr/connect "http://localhost:7474/db/data/";) >> page1 (nn/create conn {:url "http://clojurewerkz.org"}) >> page2 (nn/create conn {:url "http://clojureneo4j.info"}) >> ;; a relationship that indicates that page1 links to page2 >> rel (nrl/create conn page1 page2 :links)] >> (println rel))) >> >> They create two nodes called page1 and pag2 then connect them. >> >> In my case I have a list of names, I want to create one node for each of >> them with the possibility to work on nodes after they are created. >> So if I have for example a list like this one (def mylist '(j"john" >> "paul" "mary")) I want to create three nodes calling them john paul and >> mary storing the value "John" "Paul" "Mary" and after having created them I >> want to be albe to connect nodes creating a relationship among them, with a >> funciont like rel (nrl/create conn john paul :friend)] >> >> I hope this example explain better my needs. >> >> Thanks in advance >> >> Francesco >> >> >> On Tuesday, June 10, 2014 6:11:16 PM UTC+2, James Reeves wrote: >> >>> Could you explain a little more what your end goal is? >>> >>> It sounds like you want a map, but without knowing more about the >>> purpose, it's difficult to say. >>> >>> - James >>> >>> >>> On 10 June 2014 16:43, Francesco Lunelli wrote: >>> >>>> Hello everybody, I have a newbie question about destructuring and >>>> assigning and didn't find an answer in documentation or books. >>>> >>>> I have a list that contains an arbitrary number of elements for example >>>> '("one" "two" "three" ...) I now only that elements are strings. >>>> >>>> I
Re: destructuring let and for
Thanks for the answer, I try to explain better what I have to do. I need to create some nodes in Neo4j then I need to create relationships between those nodes. This is an example taken from necons doc. (let [conn (nr/connect "http://localhost:7474/db/data/";) page1 (nn/create conn {:url "http://clojurewerkz.org"}) page2 (nn/create conn {:url "http://clojureneo4j.info"}) ;; a relationship that indicates that page1 links to page2 rel (nrl/create conn page1 page2 :links)] (println rel))) They create two nodes called page1 and pag2 then connect them. In my case I have a list of names, I want to create one node for each of them with the possibility to work on nodes after they are created. So if I have for example a list like this one (def mylist '(j"john" "paul" "mary")) I want to create three nodes calling them john paul and mary storing the value "John" "Paul" "Mary" and after having created them I want to be albe to connect nodes creating a relationship among them, with a funciont like rel (nrl/create conn john paul :friend)] I hope this example explain better my needs. Thanks in advance Francesco On Tuesday, June 10, 2014 6:11:16 PM UTC+2, James Reeves wrote: > > Could you explain a little more what your end goal is? > > It sounds like you want a map, but without knowing more about the purpose, > it's difficult to say. > > - James > > > On 10 June 2014 16:43, Francesco Lunelli > wrote: > >> Hello everybody, I have a newbie question about destructuring and >> assigning and didn't find an answer in documentation or books. >> >> I have a list that contains an arbitrary number of elements for example >> '("one" "two" "three" ...) I now only that elements are strings. >> >> I need to to take every element and use it as the name of a variable >> inside a let and assign the result of a funcion on that element to that >> variable. >> Somthing like this for example: >> >> (let [one (clojure.string/capitalize "one") >> two (clojure.string/capitalize "two") >> three (clojure.string/capitalize "three")] >> ;; here I have access to vars one two three etc. >> ) >> >> where the names of the vars are taken from the list and values are >> obtained applying a function on the corresponding element of the list (the >> capitalize function il only an example, it could be everything else). >> >> If I do in this way >> (for [word ["the" "quick" "brown" "fox"]] (let [word >> (clojure.string/capitalize word)] (prn word))) >> it works, but I need to access the variables outside of the cycle for, >> after having defined and assigned everyone because I need to put in >> relation some of them. >> >> Thanks to everybody >> >> Francesco >> >> >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@googlegroups.com >> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+u...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+u...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
destructuring let and for
Hello everybody, I have a newbie question about destructuring and assigning and didn't find an answer in documentation or books. I have a list that contains an arbitrary number of elements for example '("one" "two" "three" ...) I now only that elements are strings. I need to to take every element and use it as the name of a variable inside a let and assign the result of a funcion on that element to that variable. Somthing like this for example: (let [one (clojure.string/capitalize "one") two (clojure.string/capitalize "two") three (clojure.string/capitalize "three")] ;; here I have access to vars one two three etc. ) where the names of the vars are taken from the list and values are obtained applying a function on the corresponding element of the list (the capitalize function il only an example, it could be everything else). If I do in this way (for [word ["the" "quick" "brown" "fox"]] (let [word (clojure.string/capitalize word)] (prn word))) it works, but I need to access the variables outside of the cycle for, after having defined and assigned everyone because I need to put in relation some of them. Thanks to everybody Francesco -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.