Simultaneous assignment in Go - variable-assignment

I'm learning Go and can't understand one thing, why creators of this language do support simultaneous assignment? It is very easy to make mistakes like a, b = a, b and not a, b = b, a, as I would want, thanks in advance for any good explanations.

It is very easy to make mistakes like a, b = a, b and not a, b = b, a,
If simultaneous assignment were not available then you would have to do something else instead. An alternative approach might look something like this:
tmp = a
a = b
b = tmp
That's much easier to get wrong.

How else would you get access to the second, third, fourth, … return value of a function?

Related

need a Maple program

Write a Maple code in order to find all the at most three digits Pythagorean triples (a, b, c), for
a, b, c > 0. We say that an integer triple(a, b, c) is a Pythagorean triple if a^2+b^2=c^2.
Hint: You might need to use the command type(sqrt(x),integer) as it returns true if x is a complete square. Get help for type from the help center.
one possibility. You can try to make it more efficient if needed.
result:=Array(1..999);
n:=0;
for a from 1 to 999 do
for b from a to 999 do
c:=sqrt(a^2+b^2);
if type(c,integer) and length(c)<=3 then
n:=n+1;
result(n):=[a,b,c];
fi;
od;
od;
result:=result(1..n);
To print them
for item in result do
print(item[1]^`2`+item[2]^`2`=item[3]^`2`)
od
....
Consider this:
isolve(a^2+b^2=c^2);

Consider we use a single lock variable to solve the mutual exclusion problem as in the code below?

boolean lock = false;
// in thread 1
while (true) {
if (lock) {
lock = true;
criticalRegion1();
lock = false;
}
}
// in thread 2
while (true) {
if (!lock) {
lock = true;
criticalRegion(2);
lock = false;
}
}
Does this work correctly? If yes, explain how. If no, describe how the program can execute resulting in a race condition?
This is a homework question. To find the right answer for this (and for most things involving race conditions in general):
Break the code that each thread does into "atomic pieces". For example, something like x++; should be considered three steps (temp = x; temp++; x = temp; where temp is a register and not a variable). Forget about things that don't have globally visible state (e.g. accessing a local variable isn't important because a different thread won't care, but accessing a global variable is important because a different thread will see or effect it).
Next; imagine every possible order that these "important atomic pieces" could be done in; and see if there's a problem.
For example, if one thread has 2 atomic pieces A and B; and if the other thread has 2 atomic pieces C and D; then you would want to consider:
A, B, C then D
A, C, B then D
C, A, B then D
A, C, D then B
C, A, D then B
C, D, A then B
Note: This sounds like a lot of work at first; but it won't take much practice before you're able to quickly skip "not likely to be a problem" cases and focus on "more likely to be a problem" cases.

Matlab: regexp usage

I am going to start illustration using a code:
A = 'G1(General G1Airlines american G1Fungus )';
Using regexp (or any other function) in Matlab I want to distinctively locate: G1, G1A and G1F.
Currently if I try to do something as:
B = regexp( A, 'G1')
It is not able to distinguish G1 with the G1A and G1F i.e. I need to force the comparison to find me only case with G1 and ignore G1A and G1F.
However, when I am searching for G1A then it should still find me the location of G1A.
Can someone please help ?
Edit: Another case for A is:
A = 'R1George Service SmalR1Al C&I)';
And the expression this time I need to find is R1 and R1A instead.
Edit:
I have a giant array containing A's and another big vector containing G1, R1, etc I need to search for.
If you want to find 'G1' but not 'G1A' or 'G1F' you can use
>> B = regexp(A, 'G1[^AF]')
B =
1
This will find 'G1' and the ^ is used to specify that it should not match any characters contained with []. Then you could use
>> B = regexp(A, 'G1[AF]')
B =
12 32
to find both 'G1A' and 'G1F'.

How to shuffle between Play! templates in Scala

I have a wrapper template that looks like this:
#(first: Html, second:Html, third:Html)
<div class="wrapper">
#first
#second
#third
</div>
I have three templates I want to shuffle and place as first, second and third.
Let's name them: views.html.a, views.html.b, views.html.c.
The controller code:
val a = views.html.a
val b = views.html.b
val c = views.html.c
val list = Random.shuffle(List(a, b, c)) // Will use Random.shuffle here but it fails complication either way
Ok(views.html.wrapper(list(0)(), list(1)(), list(2)()))
The complication error:
play.templates.BaseScalaTemplate[play.api.templates.HtmlFormat.Appendable,play.templates.Format[play.api.templates.HtmlFormat.Appendable]] does not take parameters
It appears as entering the object to the List and getting it out tricks the compiler.
If I don't use list and do:
Ok(views.html.wrapper(a(), b(), c()))
it works as expected and renders the page.
I know I can move the random logic to the wrapper template but I prefer to understand / fix the current implementation and learn some Scala in the process.
Thanks
EDIT
After reading serejja's answer, I'll add complexity to the question since this better represents the problem I'm facing.
The three templates need to take a boolean so views.html.a looks like:
#(checkMe:Boolean)
<div ...
So I can't use parentheses before the shuffle. Only after the shuffle occur I wish to send true false true as the booleans.
Using this approach:
Ok(views.html.wrapper(list(0)(true), list(1)(false), list(2)(true)))
gives the following compilation error:
play.templates.BaseScalaTemplate[play.api.templates.Html,play.templates.Format[play.api.templates.Html]] with play.api.templates.Template1[Boolean,play.api.templates.Html] does not take parameters
You were almost there :)
val a = views.html.a()
val b = views.html.b()
val c = views.html.c()
Notice the parentheses. The type of a, b and c now is play.api.templates.HtmlFormat.Appendable instead of the one before.
Now you can pass it as you wanted:
Ok(views.html.wrapper(list(0), list(1), list(2)))
EDIT:
Ok, I cannot imagine what you are up to (so that the solution could be simplified if possible) but I found a workaround.
First, consider that views a, b and c are on the one level of hierarchy:
/ a
BaseScalaTemplate - b
\ c
For this solution to work, these views must have the same number of parameters (a(check: Boolean), b(check: Boolean), c(check: Boolean)) so that they make a List[play.templates.BaseScalaTemplate[play.api.templates.Html,play.templates.Format[play.api.templates.Html]]
with play.api.templates.Template1[Boolean,play.api.templates.Html]] (which means "a generic template with one Boolean parameter").
play.api.templates.Template1 has a method render which takes that parameter and returns you a HtmlFormat.Appendable (which I mentioned earlier).
Considering this your solution might be like this:
val a = views.html.a
val b = views.html.b
val c = views.html.c
val randomizedViews = Random.shuffle(List(a, b, c))
Ok(views.html.wrapper(list(0).render(true), list(1).render(false), list(2).render(true)))
Note that this solution is far from being perfect and I'd suggest you not to use it in real life. I dont think views are intended to be used this way.

Equivalent of Fieldnames function for cells

As the title says, just wondering if there is a function that works as fieldnames (http://www.mathworks.co.uk/help/matlab/ref/fieldnames.html) does, but for cells.
So if I have something like:
a = imread('redsquare.bmp');
b = imread('bluesquare.bmp');
c = imread('yellowsquare.bmp');
d = imread('greysquare.bmp');
e = {a, b, c, d};
I'm trying to retrieve either: a, b, c, d OR the image name without the extension.
I have tried fn = fileparts(e) and fntemp = cell2struct(e,2), but I can't get it working.
Hope this makes sense
Thanks
The cell array does not include any meta-information, like field name or file name. If you want access to that information you'll need to change your data storage structure. Some options include:
Scalar Structure
Good for when there is a single name to reference.
images.red = imread('redsquare.bmp');
images.blue = imread('bluesquare.bmp');
Use fieldnames(images) to get the names.
Array of structures
A little bit more general. Allows completely general names (including special characters and spaces) and additional metadata if you need it (like "size", "author")
images(1).name = 'red';
images(1).im = imread('redsquare.bmp');
images(2).name = 'blue';
images(3).im = imread('bluesquare.bmp');
Use {fieldnames.name} to get just the names.
Containers.map
Probably more than you need here, but good to know about. help comtainers.map for more.