Initialize array of array of strings in GoLang and Rust - arrays

I want to initialize a 2D array in which each member of inner array holds a string of 1000 x's. Something like:
var data = [num_rows][num_cols]string("x....x(upto 1000)")
But all google searches have been futile. In C++ I can achieve similar thing like this:
vector<vector<string>> data(num_rows, vector<string>(num_cols, string("x",1000)));
And in Ruby something like this:
Array.new(num_rows) { Array.new(num_cols) { "x"*1000 } }
Want to achieve similar result in go but I am unable to find any documentation to fill a string and initialize a 2D array. Also note that I want to generate the string for each member of array rather than using an available string.
PS : I am also looking for something similar in Rust

In Rust, it depends on what you want to use these values for. I like this answer for creating the repeated string. The "rows" depend on if you want reference or copy semantics which is made explicit in rust. The borrows vector is a bunch of borrowed strings that refer back to the memory owned by x_s. The copies vector is a bunch of in memory copies of the original x_s string.
use std::iter;
fn main() {
let num_rows = 1000;
let num_cols = 1000;
let x_s : String = iter::repeat('x').take(num_cols).collect();
// pick one of the below
let borrows : Vec<&str> = vec![&*x_s ; num_rows];
let copies : Vec<String> = vec![x_s.clone() ; num_rows];
}
The call to clone in the last line is because the vec macro moves the value sent into it. The vec macro will also clone this clone num_rows times in the case of the copies. Note that this clone is probably not necessary in most use cases as you would not normally have borrows and copies in the same scope at the same time.
As a caveat, I am fairly new to rust but believe this to be a decent answer. I am happy to accept corrections.

you could use slices. this may not be the shortest solution, but it works for me.
package main
import (
"fmt"
"strings"
)
func main() {
xs := strings.Repeat("x", 1000)
num_rows := 5
num_cols := 5
data := make([][]string, num_rows)
for y := 0; y < num_rows; y++ {
data[y] = make([]string, num_cols)
for x := 0; x < num_cols; x++ {
data[y][x] = xs
}
}
fmt.Printf("%T", data)
fmt.Print(data)
}

A very simple on-line exemple in rust :
fn main() {
let data: Vec<String> = (0..1000).map(|n| (0..n).map(|_| 'x').collect()).collect();
println!("{:?}", data);
}

Related

How to concatenate two arrays in Go

A basic question that I'm struggling to find an answer for as there are a lot of answers about how to join two slices using the append function and the spread operator which erroneously use the word 'array'.
I am new to Go and have made the assumption that using sized arrays is good practice where the size is known. However I am struggling to work with arrays as I can't figure out how to do simple operations such as concatenation. Here is some code.
var seven [7]int
five := [5]int{1,2,3,4,5}
two := [2]int{6,7}
//this doesn't work as both the inputs and assignment are the wrong type
seven = append(five,two)
//this doesn't work as the assignment is still the wrong type
seven = append(five[:],two[:])
//this works but I'm not using arrays anymore so may as well use slices everywhere and forget sizing
seven2 := append(five[:],two[:])
As far as I can see I can either just give up on arrays and use slices exclusively or I could write a loop to explicitly construct the new array. Is there a third option?
append() can only be used to append elements to a slice. If you have an array, you can't pass that directly to append().
What you may do is slice the array, so you get a slice (which will use the array as its backing store), and you can use that slice as the target and source of elements.
For example:
s := seven[:0]
s = append(s, five[:]...)
s = append(s, two[:]...)
fmt.Println(seven)
This will print (try it on the Go Playground):
[1 2 3 4 5 6 7]
Also note that since append() returns the resulting slice, it's possible to write all this in one line:
_ = append(append(seven[:0], five[:]...), two[:]...)
(Storing the result is not needed here because we have and want to use only the backing array, but in general that is not the case.)
This outputs the same, try it on the Go Playground. Although this isn't very readable, so it's not worth compacting it into a single line.
Although when you have the target array, "appending" arrays is nothing more than copying them to the target, to the proper position. For that, you may use the builtin copy() function too. Note that the copy() function also accepts only slices, so you have to slice the arrays here too.
copy(seven[:], five[:])
copy(seven[len(five):], two[:])
fmt.Println(seven)
This will output the same. Try this one on the Go Playground.
You can use copy
copy(seven[:], five[:])
copy(seven[5:], two[:])
fmt.Printf("%v\n", seven)
> [1 2 3 4 5 6 7]
You can concatenate two arrays in go using copy function
package main
import "fmt"
func main() {
five := [5]int{1, 2, 3, 4, 5}
two := [2]int{6, 7}
var n [len(five) + len(two)]int
copy(n[:], five[:])
copy(n[len(five):], two[:])
fmt.Println(n)
}
https://blog.golang.org/go-slices-usage-and-internals
Golang runtime used to check whether current index exceeds the maximum possible.
On the side of array, it look ups its type (which contain its len and reference to the element type), because that's type, that can be registered only at compile time.
// each array mention with unique size creates new type
array := [5]byte{1,2,3,4,5}
On the side of slice, it look ups their header which looks like:
type slice {
data *byte
len int
cap int // capacity, the maximum possible index
}
As you can see, any slice is a single structure with data and len, cap fields, meanwhile array is just single pointer to data (*byte).
When you trying to convert array to slice, it just creates slice header and fills fields with:
slice := array[:]
==
slice := Slice{}
slice.data = array
slice.len = type_of(array).len
slice.cap = type_of(array).len
you can do that simply by converting array into slice:
arr1 := [...]int {1,2,3,}
arr2 := [...]int {4,5,6, }
//arr3 = arr1 + arr2 // not allowed
// converting arrays into slice
slc_arr1, slc_arr2 := arr1[:], arr2[:]
slc_arr3 := make([]int, 0)
slc_arr3 = append(slc_arr1, slc_arr2...)
fmt.Println(slc_arr3) // [1 2 3 4 5 6]
There is a more general way of appending an array of any type(once Golang has generics, but for now this solution is specific to strings. Just change the type as appropriate). The notion of Fold comes from Functional Programming. Note I have also included a filter function which also uses Fold. The solution is not stack safe but in many cases that does not matter. It can be made stack safe with trampolining. At the end is an example of its usage.
func FoldRightStrings(as, z []string, f func(string, []string) []string) []string {
if len(as) > 1 { //Slice has a head and a tail.
h, t := as[0], as[1:len(as)]
return f(h, FoldRightStrings(t, z, f))
} else if len(as) == 1 { //Slice has a head and an empty tail.
h := as[0]
return f(h, FoldRightStrings([]string{}, z, f))
}
return z
}
func FilterStrings(as []string, p func(string) bool) []string {
var g = func(h string, accum []string) []string {
if p(h) {
return append(accum, h)
} else {
return accum
}
}
return FoldRightStrings(as, []string{}, g)
}
func AppendStrings(as1, as2 []string) []string {
var g = func(h string, accum []string) []string {
return append(accum, h)
}
return FoldRightStrings(as1, as2, g)
}
func TestAppendStringArrays(t *testing.T) {
strings := []string{"a","b","c"}
bigarray := AppendStrings(AppendStrings(strings, strings),AppendStrings(strings, strings))
if diff := deep.Equal(bigarray, []string{"a","b","c","c","b","a","a","b","c","c","b","a"}); diff != nil {
t.Error(diff)
}
}

Golang convert interface{} to array of N size

I have an array of T wrapped in an interface. I know the size of the array beforehand. How do I write a generic function that gets back an array (or a slice) for any array length? E.g. for size 3 I want something like
var values interface{} = [3]byte{1, 2, 3}
var size = 3 // I know the size
var _ = values.([size]byte) // wrong, array bound must be a const expression
I can't really do a type switch because [1]byte is a different type from [2]byte etc so I'd have to explicitly enumerate all possible sizes.
Reflect is your friend here:
package main
import (
"fmt"
"reflect"
)
func main() {
var in interface{} = [3]byte{1, 2, 3} // an element from your []interface{}
var size = 3 // you got this
out := make([]byte, size) // slice output
for i := 0; i < size; i++ {
idxval := reflect.ValueOf(in).Index(i) // magic here
uidxval := uint8(idxval.Uint()) // you may mess around with the types here
out[i] = uidxval // and dump in output
}
fmt.Printf("%v\n", out)
}
Slices are the better choice output here, since you indicate that you have an undefined length.
What Magic here does is indexing the value of your input interface through reflect. This is not quick, but it does the trick.

Receiving any array type in function arguments

I just started playing with Go. I started creating a function that accepts an array of integers and returning the chunks of that array. To see what I mean, here is that program:
package main
import (
"fmt"
"math"
)
func main() {
a:= []int{1,2,3,4,5,6,2, 231, 521,21, 51}
c:=chunks(a[:], 3)
fmt.Println(c) // [[1 2 3] [4 5 6] [2 231 521] [51]]
}
func chunks(a []int, size int) [][]int{
var f float64 = float64(len(a)) / float64(size)
size_of_wrapper := int(math.Ceil(f))
i := 0
j := 0
twoD := make([][]int, size_of_wrapper )
for i < len(a) {
if i + size < len(a) {
twoD[j] = make([]int, size)
twoD[j] = append(a[i:i+size])
i = i + size
j++
} else {
if i + size == len(a){
i++
} else {
twoD[j] = make([]int, 1)
twoD[j] = append(a[len(a)-1:])
i++
}
}
}
return twoD
}
Now, I have a question. Can I turn this function to be able to receive an array that has strings in it or any other types? Also, can I set to return that same type at the end? In this case, I return an array of arrays that contain only integer values.
I seem to really struggle to find the solution to this problem. One of the posts that I have read recommended to use interface type for this kind of job. Is that the only one?
Can I turn this function to be able to receive an array that has strings in it or any other types?
Also, can I set to return that same type at the end? I
Unfortunately the Go programming language does not have generics. AFAIK Go will have generics in the next 2.0 release so right now you mainly have 3 maybe 4 options.
1. Using the []interface{} type
Your function declaration will look something like this.
func chunks(a []interface{}, size int) [][]interface{}
Using this approach will imply some type assertion.
2. Using the reflect package
The definition will look the same but this time in your implementation, instead of using the type assertion technique you will use the reflect package to determine your type and retrieve values. Remember you will pay a reasonable cost using this approach.
3. Using some unsafe.Pointer magic.
You could use this generic pointer type and do some pointer arithmetic in a C spirit way.
func chunks(a unsafe.Pointer, len uintptr, size uintptr) unsafe.Pointer
I know if you search Go does not oficially support doing pointer arithmetic but you can fake it using tricks like.
package main
import "fmt"
import "unsafe"
func main() {
vals := []int{10, 20, 30, 40}
start := unsafe.Pointer(&vals[0])
size := unsafe.Sizeof(int(0))
for i := 0; i < len(vals); i++ {
item := *(*int)(unsafe.Pointer(uintptr(start) + size*uintptr(i)))
fmt.Println(item)
}
}
4. Try using go generate in your building phase
You can find more information on how to generate go code base on what types you provide.
Because Go does not yet have the generics, the options are to use reflection or to duplicate code for each type. Here's how to use the reflect package:
// chunkAny slices a into size chunks and puts result in cp.
// The variable cp must be pointer to slice of a's type.
func chunkAny(a interface{}, size int, cp interface{}) {
av := reflect.ValueOf(a)
cv := reflect.ValueOf(cp).Elem()
cv.SetLen(0) // reset length in case backing array allocated
i, j := 0, size
for j < av.Len() {
cv.Set(reflect.Append(cv, av.Slice(i, j)))
i += size
j += size
}
cv.Set(reflect.Append(cv, av.Slice(i, av.Len())))
}
Call the function like this:
a := []string{"a", "b", "c", "d"}
var b [][]string
chunkAny(a, 3, &b)
Run it on the playground.

Multidimensional array not letting me assign values through loop?

I have a small bit of code that looks like this:
func initAllLabels() {
var scoreLabels:[[[SKLabelNode]]] = []
for (var x = 0; x < modesInGame; x++) {
for (var y = 0; y < levelsInMode; y++) {
for (z = 0; z < labelsInLevel; z++) {
scoreLabels[x][y][z] = SKLabelNode(fontNamed: "Font")
}
}
}
}
So what I am trying to do is store all my labels for every game mode. The reason I'm trying to use a multidimensional array is because I will have several labels per level (3-5) and I would like to access them like this:
updateText(scoreLabels[currentMode][currentLevel][thisLabel])
And accessing all the labels for the current label like this:
for label in labelsInLevel:
label.hidden = false
The problem is that when I try to create all my labels at the start of the game in initAllLabels, I get an "index out of range" error at the first run in the loop (index: 0). I think the problem is because I need to "append" to the array before setting its contents, is this right? How would I accomplish this in an array structure like mine?
You need to initialize the array to a given size before updating items at positions within it. It might help to start with the single-dimensional case:
var labels: [SKLabelNode] = [] // creates an empty array
// since the array is empty, this will generate an index out of range error:
labels[0] = SKLabelNode(fontNamed: "Font")
Instead you need to extend the array with the elements you want to add. For example,
for _ in 0..<labelsInLevel {
labels.append(SKLabelNode(fontNamed: "Font"))
}
(the _ means “I don’t care about the actual number of each iteration - normally if you wanted to know this was the ith time around the loop you’d write for i in 0..<n)
There are nicer ways to do this though. But be careful with one of them, the initializer for Array that takes a count and a repeatedValue:
let labels = Array(count: labelsInLevel, repeatedValue: SKLabelNode(fontNamed: "Font”))
SKLabelNode is a reference type. That means that a variable only refers to an instance, and assigning one variable to another only copies the reference. So for example:
let variableOne = SKLabelNode(fontNamed: "Foo")
let variableTwo = variableOne
// variableTwo now refers to the same instance of SKLabelNode
variableTwo.fontName = "Bar"
print(variableOne)
// fontName will now be “Bar” even though this was
// done via variableTwo
You get the same effect with the repeatedValue code above. The label is created once, and the same reference to it is inserted multiple times. Change a property on one label in the array, and you change them all. Note the for loop version does not have this problem. Every time around the loop, a new SKLabelNode instance will be created. They won’t be shared.
An alternative to the repeatedValue initializer, that creates without using a for loop is to use map:
(0..<labelsInLevel).map { _ in SKLabelNode(fontNamed: "Font") }
Here, just like in the for loop version, a new SKLabelNode instance is created every time.
(again, we use the _ to indicate we don’t care about the number of the iteration)
Finally, to create the nested multidimensional arrays with the loops inside, you can run map multiple times:
var scoreLabels =
(0..<modesInGame).map { _ in
(0..<levelsInMode).map { _ in
(0..<labelsInLevel).map { _ in
SKLabelNode(fontNamed: "Font")
}
}
}
You need to initialize the 3D array like this if you want all SKLabelNode to point at different SKLabelNodes :
func initAllLabels() {
var scoreLabels = [[[SKLabelNode]]](count: modesInGame, repeatedValue:
[[SKLabelNode]](count: levelsInMode, repeatedValue:
(0..< labelsInLevel).map { _ in SKLabelNode(fontNamed: "Font") }))
}
The repeatedValue is what you are storing in the cells (type or value) and the count represent the number of times you want to store it in your array.
PS: This answer was tested with Xcode Playground and works perfectly fine.
I edited my answer following Airspeed Velocity's comment.

Swift Dictionary of Arrays

I am making an app that has different game modes, and each game mode has a few scores. I am trying to store all the scores in a dictionary of arrays, where the dictionary's key is a game's id (a String), and the associated array has the list of scores for that game mode. But when I try to initialize the arrays' values to random values, Swift breaks, giving me the error below. This chunk of code will break in a playground. What am I doing wrong?
let modes = ["mode1", "mode2", "mode3"]
var dict = Dictionary<String, [Int]>()
for mode in modes
{
dict[mode] = Array<Int>()
for j in 1...5
{
dict[mode]?.append(j)
let array:[Int] = dict[mode]!
let value:Int = array[j] //breaks here
}
}
ERROR:
Execution was interrupted, reason: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0).
Your problem is array subscripts are zero-based. So when you write:
var a: [Int] = []
for i in 1...5 {
a.append(42)
println(a[i])
}
you will get a runtime error, because first time around the loop you are subscripting a[1] when there is only an a[0]. In your code, you either need to do for j in 0..<5 or let value = array[j-1].
By the way, even though it’s perfectly safe to do dict[mode]! (since you just added it), it’s a habit best avoided as one of these days your code won’t be as correct as you think, and that ! will explode in your face. There’s almost always a better way to write what you want without needing !.
Also, generally speaking, whenever you use array subscripts you are risking an accidental screw-up by accidentally addressing an out-of-bounds index like here. There are lots of alternatives that mean actually using a[i] is easy to avoid:
If you want the indices for a collection (like an array), instead of:
for i in 0..<a.count { }
you can write
for i in indices(a) { }
If you want to number the elements in an array, instead of
for i in indices(a) { println("item \(i) is \(a[i])" }
you can write
for (i, elem) in enumerate(a) { println("item \(i) is \(elem)") }
If the collection happens to have an Int for an index (such as Array), you can use i as an index, but if it doesn’t (such as String) an alternative to get the index and element is:
let s = "hello"
for (idx, char) in Zip2(indices(s),s) { }
If you want the first or last element of an array, instead of:
if a.count > 0 { let x = a[0] }
if a.count > 0 { let x = a[a.count - 1] }
you can write
if let first = a.first { let x = first }
if let last = a.last { let x = first }
Prefer map, filter and reduce to for loops in general (but don’t obsess over it, sometimes a for loop is better)

Resources