how to use index inside range in html/template to iterate through parallel arrays? - go

I'm executing a template with 2 parallel arrays (same size) and I want to list items from both arrays in parallel, how do I use index inside of range?
this obviously doesn't work:
{{range $i, $e := .First}}$e - {{index .Second $i}}{{end}}

One of the predefined global template functions is index.
index Returns the result of indexing its first argument by the
following arguments. Thus index x 1 2 3 is, in Go syntax,
x[1][2][3]. Each indexed item must be a map, slice, or array.
So you are on the right track. The only issue is that you are not accounting for the fact the dot has been reassigned within the range block.
So you need to get back to the original dot, for that we have the following
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
So (assuming there is nothing else going on in your template) you should be able to do:
{{range $i, $e := .First}}$e - {{index $.Second $i}}{{end}}
Personally though, I would create a template function called zip that accepts multiple slices and returns a slice of each pair of values. It would look cleaner in your template and probably get reused somewhere.

Related

Is there a bug in handling slices with references in Go?

I'm trying to build a new list of structs that contains references to items that exist in another slice. It's easier to understand if you see it, so I've prepared a snippet that you can run.
I have a list (dummylist) of two points (Cartesian coordinates) that I want to parse to build a new list (mylist) with items having some features (in the example, X > 80). I've defined two points: {X:90.0, Y:50.0} and {X:20.0 , Y:30.0}. I expect that mylist will contain {X:90.0, Y:50.0}, instead at the end there is {X:20.0 , Y:30.0}. With some print here and there I can verify that the algorithm is working fine (it enters in the "if" condition in the right case), but, at the end, "mylist" contains the wrong element.
package main
import(
"fmt"
)
func main() {
type point struct {
X float64
Y float64
}
type pointsList []point
type pointContainer struct {
Point *point
}
type pointContainerList []pointContainer
// Prepare a slice with two elements
dummylist := new(pointsList)
*dummylist = append(*dummylist, point{X:90.0, Y:50.0})
*dummylist = append(*dummylist, point{X:20.0 , Y:30.0})
// My empty list
mylist := new(pointContainerList)
fmt.Println(fmt.Sprintf("---- At the beginning, mylist contains %d points", len(*mylist)))
// Filter the initial list to take only elements
for _, pt := range *dummylist {
fmt.Println("\n---- Evaluating point ", pt)
if pt.X > 80 {
fmt.Println("Appending", pt)
*mylist = append(*mylist, pointContainer{Point: &pt})
fmt.Println("Inserted point:", (*mylist)[0].Point, "len = ", len(*mylist))
}
}
// mylist should contain {X:90.0, Y:50.0}, instead...
fmt.Println(fmt.Sprintf("\n---- At the end, mylist contains %d points", len(*mylist)))
fmt.Println("Content of mylist:", (*mylist)[0].Point)
}
Here you can run the code:
https://play.golang.org/p/AvrC3JJBLdT
Some helpful consideration:
I've seen through multiple tests that, at the end, mylist contains the last parsed item in the loop. I think there is a problem with references. It's like if the inserted item in the list (in the first iteration) is dependent on the "pt" of other iterations. Instead, if I use indexes (for i, pt := range *dummylist and (*dummylist)[i]), everything works fine.
Before talking about bugs in Golang... am I missing something?
Yes, you're missing something. On this line:
*mylist = append(*mylist, pointContainer{Point: &pt})
you're putting the address of the loop variable &pt into your structure. As the loop continues, the value of pt changes. (Or to put it another way, &pt will be the same pointer for each iteration of the loop).
From the go language specification:
...
The iteration values are assigned to the respective iteration
variables as in an assignment statement.
The iteration variables may be declared by the "range" clause using a
form of short variable declaration (:=). In this case their types are
set to the types of the respective iteration values and their scope is
the block of the "for" statement; they are re-used in each iteration.
If the iteration variables are declared outside the "for" statement,
after execution their values will be those of the last iteration.
One solution would be to create a new value, but I'm not sure what you're gaining from so many pointers: []point would probably be more effective (and less error-prone) than a pointer to a slice of structs of pointers to points.

In Ruby is there a way to get the index of an item in an array that consists of structs?

With a normal array, I can use the arrayname.find_index('whatimlookingfor') to get the position within the array.
I can't figure out how to do this when the elements of the array are Struct's.
Scenario: I have a struct that consists of an ID and the Filename. In one function I need to find within that array the ID of a different file than the one I'm currently processing. I know the other filename, so what I was hoping that I could do something like:
arrayname.filename.find_index(parsedfilename)
But this obviously fails. Without iterating through the entire array is there a way to quickly reference the index of where the match happens? Or am I out of luck because the array is a collection of structs?
index (same as find_index) takes a block in which you can code up any true/false logic for your finder. To find the index of the first item whose filename does not match parsedfilename...
found_index = items.index { |item| item.filename != parsedfilename }
Many methods which work with Arrays and Enumerables also take blocks.

How to check if array index exists in go

I am coming from javascript and know how to check if a variable exists. We can use !!var
I have come across an array in Go where I want to know if an index exists:
myArr := []int{1, 2, 3}
if myArr[3] {
fmt.Println("YES")
}
When I run this it gives me an error: Index Out Of Range: 3
Since Go is a compiled language the concept of a variable not existing does not make sense. The closest thing is that some types can take a nil value.
As far as arrays go they just have a length (without gaps). So if the length is N then only indices 0 to N-1 are valid. The built-in len() function works with any array or slice.

Get Capped Maximum Value From List

I have a list of values that range anywhere from 500-1000. I have a second list of values that denote relevant breakpoints in the 500-1000 range (500, 520, 540, 600, etc). I need to return the highest value in the second list that is less than the value in a given number from the first list. I noticed the "N" functions let you set a conditional on them, so for example if I do:
List.Max(List.FirstN(SomeTable[Breakpoints], each _ < 530))
It correctly returns 520 to me. However if I put this inside an AddColumn function and change the 530 to a local field reference:
Table.AddColumn(MyTable, "MinValue", each List.Max(List.FirstN(SomeTable[Breakpoints], each _ < [SomeNumbers])))
Then I get a "We cannot apply field access to the type Number" error. Is what I'm trying to do possible and I'm just formatting it wrong? I always get confused with scope and references in PQ, so it may just be that.
After each, [SomeNumbers] by itself is short for _[SomeNumbers] (which is what you see when filtering a column). In the List.FirstN call, _ refers to a number in the list instead of a row in a table: the value of _ is tied to the closest each, where closeness is measured by the number of layers of nesting between _ and the appearance of each . Therefore, in your code [SomeNumbers] is trying to find the column SomeNumbers on a number, which doesn't exist.
There are a couple ways to fix this:
You can use a let...in statement to store the current value of the SomeNumbers column to use it for later, like so:
each
let
currentNumber = [SomeNumbers],
result = List.Max(List.FirstN(SomeTable[Breakpoints], each _ < currentNumber))
in
result
You can explicitly define a function with the (x) => ... syntax instead of using each twice, like so:
each List.Max(List.FirstN(SomeTable[Breakpoints], (point) => point < [SomeNumbers]))

Nim: How to iterate over a slice?

I'm puzzled by the following observation. On the one hand, this works:
for i in 5..10:
echo i
But as soon as I store the slice in a variable, I can no longer iterate over it, i.e., this fails:
var slice = 5..10
for i in slice:
echo i
The error is type mismatch: got (Slice[system.int]), and apparently there is no overloaded signature of the system.items iterator for Slice[T]. This raises the questions:
Why does it work at all in the first case?
Is there a workaround to iterate over a slice in the second case?
With for i in 5..10: you invoke the iterator .. (doc), which is just an alias for countup. Since this is an inline iterator it transforms the for-loop into a while-loop over the values 5 to 10. But inline iterators cannot be assigned to a variable, other than closure iterators.
With var slice = 5..10 you invoke the proc .. (doc), which generates a Slice(a: 5, b: 10). But Slices don't have a default items iterator defined.
You could iterate from slice.a to slice.b, like this:
var slice = 5..10
for i in slice.a .. slice.b:
echo i
Since this is not very nice, the proper solution is to define your own items iterator, like this:
iterator items*[T](s: Slice[T]): T =
for i in s.a .. s.b:
yield i
var slice = 5..10
for i in slice:
echo i
Since this seems pretty reasonable to me, I made a pull request to have this included in the system module: https://github.com/nim-lang/Nim/pull/2449

Resources