How to unpack/destructure an array in D?
I have an array ([3,4,5]) of three elements and want to assign it to three variables (a, b, c) with one assignment.
How would I do this in D?
Try tie from my dub package vest:
import vest.utils: tie;
int a,b,c;
tie(a,b,c) = [1, 2, 2];
tie supports arrays, ranges, tuples
The module letassign.d at https://bitbucket.org/infognition/dstuff/src allows the following code:
int x, y, z;
let (x,y,z) = [1,2,3];
This should be in the standard D library, by the way!
Related
Suppose I have m-by-n matrices A, B, C arranged in an m-by-n-by-3 tensor P:
P = cat(3, A, B, C);
I now want to make a new tensor where each matrix is repeated K times, making the third dimension size 3K. That is, if K=2 then I want to build the tensor
Q = cat(3, A, A, B, B, C, C);
Is there a nice builtin way to achieve this, or do I need to write a loop for it? Preferably as fast or faster than the manual way.
If A, B, C were scalars I could have used repelem, but it does not work the way I want for matrices. repmat can be used to build
cat(3, A, B, C, A, B, C)
but that is not what I am after either.
As noted by #Cris Luengo, repelem(P, 1, 1, k) will actually do what you want (in spite of what the MATLAB documentation says), but I can think of two other ways to achieve this.
First, you could use repmat to duplicate the tensor k times in the second dimension and then reshape:
Q = reshape(repmat(P, 1, k, 1), m, n, []);
Second, you could use repelem to give you the indices of the third dimension to construct Q from:
Q = P(:, :, repelem(1:size(P,3), k));
In Rust and Swift you can create a slice of either/both arrays and strings which doesn't make a new copy of the elements but gives you a view into the existing elements using a range or iterator and implemented internally a reference pair or reference + length.
Does Kotlin have a similar concept? It seems to have a similar concept for lists.
It's hard to Google since there is a function called "slice" which seems to copy the elements. Have I got it all wrong or is it missing?
(I'm aware I can work around it easily. I have no Java background btw.)
I don't think so.
You can use asList to get a view of the Array as a list. Then the function you already found, subList works as usual. However, I'm not aware of an equivalent of subList for Arrays. The list returned from asList is immutable, so you cannot use it to modify the array. If you attempt to make the list mutable via toMutableList, it will just make a new copy.
fun main() {
val alphabet = CharArray(26) { 'a' + it }
println(alphabet.joinToString(", "))
val listView = alphabet.asList().subList(10, 15)
println(listView)
for (i in alphabet.indices) alphabet[i] = alphabet[i].toUpperCase()
println(alphabet.joinToString(", "))
// listView is also updated
println(listView)
}
Output:
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
[k, l, m, n, o]
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
[K, L, M, N, O]
Using lists (or Collections) is preferred in Kotlin, and comes with much better support for generics and immutability than arrays.
If you need to mutate arrays, you probably have to manage it yourself, passing around (a reference to) the array and an IntRange of the indices you care about.
In JavaScript I can store the values of array into variables like this:
[a, b, c] = [1, 2, 3]
I created a variable function( it return a map with the same number of arguments), I would like to know if Go has a shortcut like JavaScript
Based on the suggestion from comments ,I created a small example for your scenario:
package main
import (
"fmt"
)
func main() {
myArray := []int{1, 2, 3}
fmt.Println(myArray)
a, b, c := myArray[0], myArray[1], myArray[2]
fmt.Println(a, b, c)
}
Output:
[1 2 3]
1 2 3
Javascript's destructuring assignment syntax is not available in Go, however Go's syntax is sometimes concise too, depending what your input looks like.
Declaring and initializing several variables in a single LOC is straightforward, and it works even with variables of different types:
a, b, c := 42, "hello", 5.0
Source. Playground.
You can assign values to existing variables as well:
a, b, c = 42, "hello", 5.0
Playground.
If your input data is a slice s, then per #Gopher's answer the code will look like:
a, b, c := s[0], s[1], s[2]
I have a function f(x,a,b,c) and i want to use plot() to display it. That means i have to compute f(x) for each of my x and store them in a vector to use plot().
How can i apply my function to each element of x individually? My function requires 3 arguments aside from the value of x. I've tried arrayfun() but can't seem to get it working...
x = linspace(0.008,0.08);
a = 0.005;
b = 0.0015;
re = (1.23*40*0.005)/(1.79*10^-5);
y = arrayfun(#f, x, a, b, re);
plot(y);
Any ideas?
You could use an anonymous function:
y = arrayfun(#(x) f(x, a, b, re), x);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I do multiple assignment in MATLAB?
When dealing with cell arrays, I can use the deal() function to assign cells to output variables, such as:
[a, b, c] = deal(myCell{:});
or just:
[a, b, c] = myCell{:};
I would like to do the same thing for a simple array, such as:
myArray = [1, 2, 3];
[a, b, c] = deal(myArray(:));
But this doesn't work. What's the alternative?
One option is to convert your array to a cell array first using NUM2CELL:
myArray = [1, 2, 3];
cArray = num2cell(myArray);
[a, b, c] = cArray{:};
As you note, you don't even need to use DEAL to distribute the cell contents.
Not terribly pretty, but:
myArray = 1:3;
c = arrayfun(#(x) x, myArray , 'UniformOutput', false);
c{:}