 As I sit down to make this video, I respectfully acknowledge that I'm standing on the unceder traditional territory of the Comox First Nation and I thank them for the use of their lands. Now this is a follow-up video to one that I did yesterday where I was explaining how my verb, because that's what we call functions in J, how my verb spiral works. And it was pretty complicated and it showed a lot of things, but while I was doing that I actually thought of a few things that I could to make it a little simpler and also a little quicker. So this follow-up is actually this new version of spiral. So let's first review what a spiral array is. I'll go through this really quickly because I covered it yesterday but I don't want you to have to walk through the whole video that I did yesterday as well. So spiral array starts with one in the center and then the numbers increase to two to three to four in a spiral around one and then once it gets to the fills out that next ring, it starts in a new ring and starts all the way that spiral around until it gets to this ring and then it's out and it finishes up there. Now the challenge is that for every value that you see in this array what we're trying to do is we're going to develop a Cartesian index for that and we're going to call zero zero is the index of one. So that's going to be our origin. So if you were say for instance looking for the index of 14 it would be one two over and one up so it would be two one would be its its coordinates and similarly if you went to 25 you'd be going minus one minus two one two up so it would be minus two two would be the coordinates and so we're trying to develop the coordinates for every value or for any value is probably the best way to say it that we are given. So if you give me a value I'll tell you what the coordinates would be in a spiral array. So I developed a verb called spiral and spiral is a verb so of course it would work on a noun so what do we give it 29 let's do 29. So spiral 29 is one three so starting from one we go over one and then up three one two three so it works oh I'm so glad it works because even it doesn't work it makes for a much more confusing video. Okay so spiral is written in what's called an explicit format which is a little easier to understand for people who aren't as familiar with point-free and especially array languages. So this might be a little bit easier to follow and this is what spiral looks like. This is the verb that actually will create those coordinates. So going down line by line and in the case of an explicit verb its line after line is how you read it they just run down the lines until you get to this little parentheses and that says that's the end of the verb. So three colon zero all that does is it says this is a verb and the zero actually says everything every line after this is part of this verb until I have this final parentheses so that's all that zero means. So three tells me it's a verb and the colon which is a conjunction as we're talking verbs conjunctions are between nouns or verbs in this case it's between two nouns. Well colon is a conjunction and when it's used with two nouns the zero means all these lines are what become the verb that's a definition. The nb dot is just comments so let's go through the comments because it sort of sets up how the verb works. So y which in this case is 29 is the value I'm given of the verb spiral is an argument that's fairly straightforward. S is the number of the ring that contains y so in the case of y being 29 the ring we get is the first ring is here, the second ring is here and the third ring is the one that contains 29. Okay so we've got s is the number of the ring that contains y, t is the length of each side of the ring that contains y and so t so when we're out in this third ring t is going to be 1, 2, 3, 4, 5, 6 that's going to be the length of a side of the spiral array. So we're going to have 6 as t in this case and d is the value distance from y to the upper left corner of that ring. So if it's 29 the distance I want to go to is all the way up to 49 so the difference between 49 and 29 which is 20 of course is going to be my value for d and then this select block calculates the index according to the side selected so now we actually get into the part of the verb that's going to be doing things and in the case of select it's going to this whole line here is going to evaluate to either 0, 1, 2, or 3 and depending on what evaluates to will be what it actually does so let's see how it calculates the 0, 1, 2, and 3 and on the way to calculating the 0, 1, 2, and 3 it also creates the values for these variables so starting with y which we know is 29 this verb here is the square root so I'm taking the square root of 29 which I know is going to be a little bit above 5 not sure how much above 5 but until I got to 36 it wasn't going to get to 6 so say it's a little bit above 5 I'm going to round it up which is what this verb does it just rounds up whatever I've got so that would round it up from 5 something to 6 I'm going to take half of that and so that gives me 3 and I'm going to round down and that round down is just in case my half ended up with a half thing I'm going to round down and in this case rounding 3 down is gives me 3 it's an integer so it just rounds down to itself so let's take a look at in that that that is assigned this this equals dot is the assignment one of the assignment ways to do things in J and this s is what I get assigned to so s in this case will be assigned 3 so what that's telling us is 3 is the ring number that the number 29 is on is it well 1 2 3 and 29 is right there so it's on the third ring and so s is calculated as we'd expect so now we have a value for s which is a number of learning that contains why okay now the next step is I'm going to double s this verb here I keep working from my right to my left as I go through J this verb here doubles whatever the value is to its to its right and then I do an assignment to T well s was 3 and if I double 3 I get 6 which is the length of a side so that's that I'm building these variables okay now this next step gets a little bit trickier because I'm J's execution always goes right to left except that you do if you if you want to change that execution up a little bit you use parentheses and then you do what's inside of parentheses before taking the next step so we've got this is the signal for divided by and it's a percent sign but it's actually what J uses is divided and the easy way to think of it is you've got one dot above a slash and then a dot below a slash and so J says that's divided the slash itself is used for something else it's actually an adverb of course we were talking about verbs nouns and conjunctions well there are adverbs to they modify verbs so I know whatever it is I'm going to divide but it's going to be divided by T but what is going to be divided by T well I have to look at these parentheses and so I've got a parentheses that go parenthesis that goes right from here all the way over to here but inside that it's whatever I'm subtracting why from is in this parentheses so let's start on the inner parentheses and work our way out so we've got T which we know is 6 and this is the verb for increment so I'm taking it from 6 up to 7 and then I'm squaring it to get 49 49 is this value right up here you notice something when you look at this spiral array you've got one of the center and then you go to 9 here well 9 is 2n plus 1 squared if n is the number of the ring so in this ring 9 is calculated because 2n plus 1 is 2 plus 1 and 2 plus 1 squared is 3 and 3 square sorry 2 plus 1 is 3 and 3 squared is 9 so then you love this one and you got n is 2 so it's 2n is 4 plus 1 is 5 squared is 25 so each of these can be calculated really quickly just by knowing what ring it is so that's where they sort of like an anchor point if I know what those values are I can calculate a lot of other things from that so right now I've figured out that wherever I am in this case I'm at 49 then I'm going to subtract why which is the value I've got which I think we had is 29 so it's going to be 49 minus 29 and that's going to be D the distance from the upper left corner of that ring so right now I know D I know T I know s and I know why and those are actually all the variables that I need to know I've got everything I need to know to do my calculation the question is which calculation do I do so this brings us back we're not finished yet because of course we got to the point here where we've got D which is 49 minus y which is 29 which gives us 20 now we're dividing it by T which is 6 so we end up with 3 something and we round down so rounding down on this means that our answer it will become not 3 point something it's going to be 3 on its own so what we've decided now is we're doing case 3 which when you look at my comments over here I've just done this so I can keep track of what's going on it says this is what I do when I'm on the top side and that actually makes perfect sense I'm going to show you why when I if I was case 0 that means that this value this distance divided by T will be less than 1 well if you think about it any of these numbers the distance here is less than the distance of this side so if you divide by the length of the side and you're any of these numbers you're going to get 0 point something and you round down and you're going to be case 0 so NB is the left side until you get to 21 or sorry I'm 49 until you get to 43 and then when you get to 43 what's going to happen is now that distance is going to be 6 it's going to be divided by 6 which is the length you're going to be at 1 and you will be at 1 all the way until you get to 37 and then the distance will be 12 and it'll be divided by the length which is 6 and you'll be at 2s and you'll be at 2s all the way to here until you go to 3s and then you'll be 3s all the way to here so that's where this select comes from it's actually choosing which side you're on and the reason it does that is depending on which side you're on depends what you're going to do with the coordinates relative to the nearest corner and something about the corners is really interesting when you think about it each one of these corners on this way is of the form minus 1 1 so it'll be a multiple of minus 1 1 so in the case of 9 it'll be minus 1 1 in 25 it'll be minus 2 minus sorry minus 2 2 49 it'll be minus 3 3 and so on now that's on this diagonal going up going down all of these positions are going to be 1 minus 1 and then 2 minus 2 and 3 minus 3 so on going this way this 7 is always going to be minus 1 minus 1 format the 3 is always going to be plus 1 plus 1 and 13 would be plus 2 plus 2 that'll be the coordinates so you can know right away on each of these corners exactly what the form of the coordinates are all you need to know to get the actual coordinates is to know what ring you're in and we already know that that's what s is so now we're going to do our calculations so in case 3 we've got t is 6 and we're going to multiply by that by 3 and we're going to subtract that from D which is a distance and you think where did that come from well think about it for a second if I want to go relative to the nearest corner and my distance is actually the distance all the way back to 49 I don't want to be adjusting my my index based on where 49 is I want to do it based on where 31 is and in order to get to 31 and not 49 I'm going to get rid of these and these and these so that's 3 times t that I'm just going to get rid of and then I'm going to multiply that by 1 0 and why why 1 0 because when I'm on the top the only movement I want is along the x-axis so I'm going to do it relative to 31 I had 29 and D was 20 I'm subtract subtracting 3 times t which is 18 gives me 2 so I'm subtracting 2 0 what am I subtracting it from I'm subtracting it from s which tells me what ring I'm on times 1 1 which is this corner and so s is 3 so I've got 3 3 minus 2 0 and I end up with 1 3 and that's how I get that answer so instead of having to calculate all these coordinates in a spiral matrix I can actually and I actually sort of put off doing all my calculations right up to the point where I need them I haven't even calculated these positions and like the corner positions until I'm actually in here all I've done is I know how far I am away from the top corner here but then based on what selected I can choose how many of these sides I ignore before I make my move and then of course these different numbers here correspond to if I'm on the bottom side I'm moving along the x-axis if I'm on the top side I'm moving along the x-axis that's why those are 1 0s right side and left side I'm moving along the y-axis so that's 0 1s and whether or not it's a minus or a plus depends on whether I'm having to move up to get to my number or down to get to my number if I'm on the y-axis or right to left if I'm moving on the x-axis so this is the entire verb the entire function for determining the coordinates of the different values in a spiral array now the thing is I'll just do something that is a way to measure stuff in j and what it's called is time space x and what it does is it actually returns the time it takes to complete a function and also the space that it uses so I will use spiral and let's do oh we said 29 didn't we so let's just stay with 29 there we go so this is going to do time space x a thousand times and when it's finished that thousand times it'll come back and tell me how much each time averaged out to and how much space it used so this took about 1.336 milliseconds that's pretty quick it only used two and a half k that these are bytes so 2400 almost 2500 bytes and that's the amount of space it used now in my older version I've because I've kept that around I called it spiral one isn't that clever now look what happens when I go spiral one my old version was roughly four times more so it says 4.8 milliseconds micro seconds 4.8 millionths of a second that's 10 to the minus 6 but look how much space it used to do it and the reason was is because I was calculating all of the diagonal values in that case I wasn't even calculating all of the values I was only calculating diagonal values and working from there but then I realized I didn't have to do that I just have to find out what ring I was on and then once I'm on the ring I can go relative to the corners and it saves up a lot of time will you really see it if I use a really big number let's do oh 4040 so what that's telling me is I'm using and this is even though I'm doing this a thousand times and averaging it there is a little bit of play in this so it's actually telling me that was faster than the first time where I was doing spiral 29 even though I'm using a much bigger number but and it did take a bit more space well it would take a bit more space because the calculations are with slightly bigger numbers but now is where the real eye opener comes and with j a golf and all these are really quick but this is 1.96 times 10 to the minus five so this is actually like 19 times slower than this one and look how much space it used it used almost 22k whereas this one is just under 3k so you can see that there's a lot of efficiency built into j and and depending on how you as I said my older version is it's still j and it's still fairly quick but I wasn't making full use of the of the problem space as it was not as it were I had an old math professor used to say the the the best way to come up with a good solution to something is get rid of everything that's not the solution and what you're left with is a solution well when I was thinking about this I figured out all the things that I really needed to know and that's all I worked with so thanks for spending the time listening to this and I hope this was useful and I think it was even seeing this different this more explicit version of j it becomes a little easier for traditional programmers to understand compared to some of the tacit stuff which I find fascinating and I really enjoy it because it's so functional and it's so you just manipulating and it's almost like writing poetry it's just it's it's I really enjoy it but certainly there's nothing wrong with this format and it's very quick and for people who are used to reading programs this way it's a little easier so I hope that helps as well j isn't anything to be scared of it's it's kind of new it's kind of exciting and and if you get a chance to play with it I I would I would advise it I think it's a good idea to to widen your scope of programming languages by playing with this one thanks for listening