In D language, what is the shortest way to construct an array from a given range?
Let I have an iterator i. How to make an array of its elements (in order)?
.array (from std.array).
Example:
import std.array : array;
import std.range : iota;
int[] arr = 10.iota.array;
Related
I find Lambda in Kotlin to be very confusing and on the top of it is "it".
There are two things I know about "it" and i.e.
If your Lambda has their own argument, you can replace its name with "it".
"It" is an automatically generated name for your Lambda, if it has
only one argument, and you don't specify a different argument name.
Still I don't understand what actually passes as "it".
For E.g. I wanted to apply modulo function on each element of a 3x3 matrix.
fun main(){
var result = Array(3) {
IntArray(3) { 3;2;4;6;7;9;12;11;23 }
}
result = Array(3){ IntArray(3) {it%2} }
println(result.joinToString("\n") { it.joinToString(" ") })
}
Here I assumed that "it" takes each element of the matrix which is clearly not the case as my output was:
0 1 0
0 1 0
0 1 0
So can you please explain me how "it" works, what is happening here? and what would be the correct way to implement this program?
Your line
result = Array(3){ IntArray(3) {it%2} }
isn't doing anything to the original Array that result is pointing at. You are creating a brand new group of array objects by calling the Array and IntArray constructors.
The lambda that you pass to the IntArray constructor has an input parameter that represents the array index, and the return value of your lambda is what will be put into the array at that index. So in this case it is the array index, and your lambda is returning 0 and 1 for even and odd indices respectively.
You are also instantiating your array incorrectly to begin with. Your lambda that you pass to that IntArray constructor is throwing away a bunch of pointless Int values and then returning 23 for each item. So you've created a 3x3 matrix that is completely filled with the number 23.
The correct syntax for creating an array with explicit values is to use arrayOf or intArrayOf.
val result = arrayOf(
intArrayOf(3, 2, 4),
intArrayOf(6, 7, 9),
intArrayOf(12, 11, 23)
)
To modify all the values of an array, you typically iterate the traditional way, not with a lambda:
for (innerArray in result) {
for (i in innerArray.indices)
innerArray[i] = innerArray[i] % 2
}
You were probably thinking of the map function, which lets you pass a lambda and returns a new List with the lambda function applied to every element of the input collection. Or when working with collections other than arrays, you can use forEach or onEach to iterate them without modifying them.
I want to create an array of mutable lists, and each mutable list should have a specified number of ints. These ints should have a starting value of 0.
Example: I know the number of mutable lists, n, in the array, and I know how many integers, m are in each list. I think I can declare the array like so:
// represents number of mutable lists in the array
val n = 2
// represents number of Ints inside each mutable list
val m = 3
val arr = Array<MutableList<Int>>(n) { ??? }
What this should do is it should create an Array arr that contains only the type MutableList. The array is of size n. The mutable lists take in only the type Int.
I am not sure how to iterate through these mutable lists and add m number of 0 valued integers.
Array (size: Int, init: (Int) -> T)
The function init is called for each array element sequentially starting from the first one. It should return the value for an array element given its index.
Same for MutableList
// represents number of mutable lists in the array
val n = 2
// represents number of Ints inside each mutable list
val m = 3
val arr = Array(n) { MutableList(m) { 0 } }
I'm looping through the depends array from a META6.json. I've loaded into a Hash using JSON::Fast. When I'm looping through it using a for loop, however, it only goes through the loop once, and the item is the same array:
use JSON::Fast;
my %meta = from-json(slurp("META6.json"));
for %meta<depends> -> $dependency {
dd $dependency;
}
This piece of code returns
Array $dependency = $["Config::Parser::toml:ver<1.0.1+>", "Config:api<1>:ver<1.3.5+>", "Dist::Helper:ver<0.21.0+>", "Hash::Merge", "Terminal::Getpass:ver<0.0.5+>", "zef"]
I'm expecting it to loop through the %meta<depends> 6 times, which each iteration holding a different element from that array.
For good measure, this is the output of dd %meta<depends> from the example:
Array %meta = $["Config::Parser::toml:ver<1.0.1+>", "Config:api<1>:ver<1.3.5+>", "Dist::Helper:ver<0.21.0+>", "Hash::Merge", "Terminal::Getpass:ver<0.0.5+>", "zef"]
Why is the loop not looping the way I expected?
EDIT: I'm using the latest Rakudo Star:
This is Rakudo Star version 2018.04.1 built on MoarVM version 2018.04.1
implementing Perl 6.c.
Even though %meta<depends> contains an Array, it is contained inside an item (container). The for statement looks at that and decides there's only 1 thing to iterate over (the container).
This is easily remedied: by suffixing .list you convert the item to something Iterable, and thus it will iterate over the Array in the container:
for %meta<depends>.list -> $dependency {
A slightly shorter syntax for this is #():
for #(%meta<depends>) -> $dependency {
EDIT: or use the syntax suggested by jjmerelo, which decontainerizes the element, and thus exposes the underlying Array to for:
for %meta<depends><> -> $dependency {
This is a pitfall. Essentially this is like:
my $var = ['a', 'b', 'c'];
for $var -> $v {
dd $v;
}
Which gives you: $["a", "b", "c"]
If you iterate an array with # sigil it already acts as an Array, but when you have a list inside a scalar it will return the Array and not iterate inside it.
The solution is to use .list to make it act as a list instead of as a scalar.
How can I create a fixed multidimensional array in Specman/e using varibles?
And then access individual elements or whole rows?
For example in SystemVerilog I would have:
module top;
function automatic my_func();
bit [7:0] arr [4][8]; // matrix: 4 rows, 8 columns of bytes
bit [7:0] row [8]; // array : 8 elements of bytes
row = '{1, 2, 3, 4, 5, 6, 7, 8};
$display("Array:");
foreach (arr[i]) begin
arr[i] = row;
$display("row[%0d] = %p", i, row);
end
$display("\narr[2][3] = %0d", arr[2][3]);
endfunction : my_func
initial begin
my_func();
end
endmodule : top
This will produce this output:
Array:
row[0] = '{'h1, 'h2, 'h3, 'h4, 'h5, 'h6, 'h7, 'h8}
row[1] = '{'h1, 'h2, 'h3, 'h4, 'h5, 'h6, 'h7, 'h8}
row[2] = '{'h1, 'h2, 'h3, 'h4, 'h5, 'h6, 'h7, 'h8}
row[3] = '{'h1, 'h2, 'h3, 'h4, 'h5, 'h6, 'h7, 'h8}
arr[2][3] = 4
Can someone rewrite my_func() in Specman/e?
There are no fixed arrays in e. But you can define a variable of a list type, including a multi-dimensional list, such as:
var my_md_list: list of list of my_type;
It is not the same as a multi-dimensional array in other languages, in the sense that in general each inner list (being an element of the outer list) may be of a different size. But you still can achieve your purpose using it. For example, your code might be rewritten in e more or less like this:
var arr: list of list of byte;
var row: list of byte = {1;2;3;4;5;6;7;8};
for i from 0 to 3 do {
arr.add(row.copy());
print arr[i];
};
print arr[2][3];
Notice the usage of row.copy() - it ensures that each outer list element will be a copy of the original list.
If we don't use copy(), we will get a list of many pointers to the same list. This may also be legitimate, depending on the purpose of your code.
In case of a field (as opposed to a local variable), it is also possible to declare it with a given size. This size is, again, not "fixed" and can be modified at run time (by adding or removing items), but it determines the original size of the list upon creation, for example:
struct foo {
my_list[4][8]: list of list of int;
};
How to create a array of array in mq4?
I have a function with this pararameters:
double & v1[], double & v2[], double & v3[], double & v4[]
I want to create a array where each position has a reference to another array like:
double v[];
v[0] = v1;
v[1] = v2;
v[2] = v3;
v[3] = v4;
and then iterate like:
v[0][2] == v1[2]; // true
It's possible to do something like that? How can I do it?
You have pretty much already answered yourself already. A 2D array can be thought of/imagined as an array with each cell of the first array containing a 1D array of the specified size. Similarly, a 3D array could be imagined as a 1D array containing a 2D array in each cell. So instead of passing in v1,v2,v3,v4 you could just have the input parameter as double &v[4][6] and loop through them.
TestFunction(double &v[4][6])
{
for(int i=0;i<4;i++)
{
for(int j=0;j<6;j++)
{
v[i][j] = 0;
}
}
}
If the arrays v1,v2,v3,v4 in your example are different sizes then you could created an array of CArrayDouble objects and pass that. E.g.
CArrayDouble TestArray2[4]
void TestFunction2(CArrayDouble &v[4])
{
for(int i=0;i<4;i++)
{
for(int j=0;j<v[i].Total();j++)
{
v[i][j];
}
}
}
In answer to your comment, if you are unable to change the function signature. You could copy the arrays into an instance of CArrayDouble.
CArrayDouble ArrayOfArray[4];
ArrayOfArray[0].AssignArray(v1);
ArrayOfArray[1].AssignArray(v2);
ArrayOfArray[2].AssignArray(v3);
ArrayOfArray[3].AssignArray(v4);
If the arrays v1,v2 etc are buffers and thus change in size on every new bar, I would declare the CArrayDouble as static and after the initial copying (which is what AssignArray does), add each new element from the arrays as and when the function is called (using the member function 'Add').