Fork me on GitHub
#specter
<
2020-05-06
>
Alex Ragone15:05:26

Hi everyone! I am trying to navigate a 4D array and would like to get all the indicies (the path to get there). How do I do that? So far I have the following

(transform [ALL (collect-one INDEXED-VALS FIRST)
              ALL (collect-one INDEXED-VALS FIRST)
              ALL (collect-one INDEXED-VALS FIRST)
              ALL (collect-one INDEXED-VALS)]
             (fn [d1 d2 d3 d4 item]
                 ...))
             4d-array))
But I don’t quite understand how the collect function works? Can anyone please help me? 🙂 Thank you.

nathanmarz19:05:54

@ragonedk you want something more like:

(transform [INDEXED-VALS
            (collect-one FIRST)
            LAST
            INDEXED-VALS
            (collect-one FIRST)
            LAST
            INDEXED-VALS
            (collect-one FIRST)
            LAST
            INDEXED-VALS
            (collect-one FIRST)
            LAST
            ]
  (fn [d1 d2 d3 d4 item]
    )
  4d-arr
  )

nathanmarz19:05:57

you can shorten that like this:

(def ALL-AND-COLLECT-INDEX (path INDEXED-VALS (collect-one FIRST) LAST))

(transform [ALL-AND-COLLECT-INDEX
            ALL-AND-COLLECT-INDEX
            ALL-AND-COLLECT-INDEX
            ALL-AND-COLLECT-INDEX]
  (fn [d1 d2 d3 d4 item]
    )
  4d-arr
  )

Alex Ragone19:05:15

Awesome, that worked! Thank you :)

Joe22:05:14

Hello, I have a 9x9 matrix using nested vectors, and I want to pull out the top left 'subsquare' , i.e. elements at [0-2, 0-2]. I got the results I wanted with (select [(srange 0 3) ALL (srange 0 3)] data) , but I don't understand the need for the ALL inbetween the two sranges. I was thinking the first srange would grab the first 3 vectors, and the second would grab the first 3 elements from each of them - what's the concept I'm missing that means you need the ALL in the middle? Here's the data:

(def data
  [[5 3 0 0 7 0 0 0 0]
   [6 0 0 1 9 5 0 0 0]
   [0 9 8 0 0 0 0 6 0]
   [8 0 0 0 6 0 0 0 3]
   [4 0 0 8 0 3 0 0 1]
   [7 0 0 0 2 0 0 0 6]
   [0 6 0 0 0 0 2 8 0]
   [0 0 0 4 1 9 0 0 5]
   [0 0 0 0 8 0 0 7 9]]) 

nathanmarz22:05:05

@allaboutthatmace1789 srange navigates to a single subsequence, ALL navigates to each element

Joe22:05:22

So the first srange gets to [[1 2 3 ,,,] [1 2 3 ,,,] [1 2 3 ,,,]], you need the ALL to tell it grab every element [1 2 3 ,,,] [1 2 3 ,,,] [1 2 3 ,,,], then for each of those elements the second srange gets the subseq of first 3 elements [1 2 3] [1 2 3] [1 2 3] - then wraps everything back up again?