How do I return a value from the Function used to initialize an array in Mathematica - function

In this example I'm trying to create an Array of length 5 where each ellement contains the number of times .3 can be summed without exceeding 1. i.e. 3 times. So each element should contain the number 3. Here is my code:
Array[(
workingCount = 0;
workingSum = 0;
done = false;
While[! done,
workingSum = workingSum + .3;
If[workingSum > 1, done = true; workingCount, workingCount++]
])
, 5]
In the 3rd to last line there I have workingCount without a ; after it because it seems like in Mathematica omitting the ; causes the value a statement resolves to to be returned.
Instead I get this:
{Null[1], Null[2], Null[3], Null[4], Null[5]}
Why does this happen? How can I get my program to do what I want it to do? i.e. In the context of the function passed to Array to initialize it's elements, how to I use complicated multi-line functions?
Thanks in advance.

Two things:
First, one way to be able to do that in Mathematica is
Array[
Catch[
workingCount = 0;
workingSum = 0;
done = False;
While[! done,
workingSum = workingSum + .3;
If[workingSum > 1,
done = True; Throw#workingCount,
workingCount++]]] &,
5]
Second, and most important: you never should do that in Mathematica! Really.
Please visit for example the Stack Exchange site for Mathematica, and read the questions an answers there to get some grip on the programming style.

Your problem comes from the fact that you are trying to initialize your array, but are trying to do so without an explicit function call - which is what you need to do.
See here for documentation on Arrays in Mathematica:
http://reference.wolfram.com/mathematica/ref/Array.html
That aside, and minor errors (True and False have to be capitalized), this is what you want to do:
f[x_] :=
(
workingCount = 0;
workingSum = 0;
done = False;
While[done != True, workingSum = workingSum + 0.3;
If[workingSum > 1, done = True, workingCount++]
];
Return[workingCount];
);
Array[f, 5] (* The array here is generating 5 values of the return value of f[x_] *)

Related

Partial replace in docs what matches only and preserve formatting

Let's assume that we have first paragraph in our google document:
Wo1rd word so2me word he3re last.
We need to search and replace some parts of text but it must be highlighted in editions history just like we changed only that parts and we must not loose our format (bold, italic, color etc).
What i have/understood for that moment: capturing groups didn't work in replaceText() as described in documentation. We can use pure js replace(), but it can be used only for strings. Our google document is array of objects, not strings. So i did a lot of tries and stopped at that code, attached in this message later.
Can't beat: how i can replace only part of what i've found. Capturing groups is very powerful and suitable instrument, but i can't use it for replacement. They didn't work or i can replace whole paragraph, that is unacceptable because of editions history will show full paragraph replace and paragraphs will lose formatting. What if what we searching will be in each and every paragraph, but only one letter must be changed? We will see full document replacement in history and it will be hard to find what really changed.
My first idea was to compare strings, that replace() gives to me with contents of paragraph then compare symbol after symbol and replace what is different, but i understand, that it will work only if we are sure that only one letter changed. But what if replace will delete/add some words, how it can be synced? It will be a lot bigger problem.
All topics that i've found and read triple times didn't helped and didn't moved me from the dead point.
So, is there any ideas how to beat that problem?
function RegExp_test() {
var docParagraphs = DocumentApp.getActiveDocument().getBody().getParagraphs();
var i = 0, text0, text1, test1, re, rt, count;
// equivalent of .asText() ???
text0 = docParagraphs[i].editAsText(); // obj
// equivalent of .editAsText().getText(), .asText().getText()
text1 = docParagraphs[i].getText(); // str
if (text1 !== '') {
re = new RegExp(/(?:([Ww]o)\d(rd))|(?:([Ss]o)\d(me))|(?:([Hh]e)\d(re))/g); // v1
// re = new RegExp(/(?:([Ww]o)\d(rd))/); // v2
count = (text1.match(re) || []).length; // re v1: 7, re v2: 3
if (count) {
test1 = text1.match(re); // v1: ["Wo1rd", "Wo", "rd", , , , , ]
// for (var j = 0; j < count; j++) {
// test1 = text1.match(re)[j];
// }
text0.replaceText("(?:([Ww]o)\\d(rd))", '\1-A-\2'); // GAS func
// #1: \1, \2 etc - didn't work: " -A- word so2me word he3re last."
test1 = text0.getText();
// js func, text2 OK: "Wo1rd word so-B-me word he3re last.", just in memory now
text1 = text1.replace(/(?:([Ss]o)\d(me))/, '$1-B-$2'); // working with str, not obj
// rt OK: "Wo1rd word so-B-me word he-C-re last."
rt = text1.replace(/(?:([Hh]e)\d(re))/, '$1-C-$2');
// #2: we used capturing groups ok, but replaced whole line and lost all formatting
text0.replaceText(".*", rt);
test1 = text0.getText();
}
}
Logger.log('Test finished')
}
Found a solution. It's a primitive enough but it can be a base for a more complex procedure that can fix all occurrences of capture groups, detect them, mix them etc. If someone wants to improve that - you are welcome!
function replaceTextCG(text0, re, to) {
var res, pos_f, pos_l;
var matches = text0.getText().match(re);
var count = (matches || []).length;
to = to.replace(/(\$\d+)/g, ',$1,').replace(/^,/, '').replace(/,$/, '').split(",");
for (var i = 0; i < count; i++) {
res = re.exec(text0.getText())
for (var j = 1; j < res.length - 1; j++) {
pos_f = res.index + res[j].length;
pos_l = re.lastIndex - res[j + 1].length - 1;
text0.deleteText(pos_f, pos_l);
text0.insertText(pos_f, to[1]);
}
}
return count;
}
function RegExp_test() {
var docParagraphs = DocumentApp.getActiveDocument().getBody().getParagraphs();
var i = 0, text0, count;
// equivalent of .asText() ???
text0 = docParagraphs[i].editAsText(); // obj
if (text0.getText() !== '') {
count = replaceTextCG(text0, /(?:([Ww]o)\d(rd))/g, '$1A$2');
count = replaceTextCG(text0, /(?:([Ss]o)\d(me))/g, '$1B$2');
count = replaceTextCG(text0, /(?:([Hh]e)\d(re))/g, '$1C$2');
}
Logger.log('Test finished')
}

AS3 Concatenating Reference Not Working

I'm struggling to concatenate a reference to a variable from a XML document. I'm trying to get:
chat_History.Msg.chatMessage1, chat_History.Msg.chatMessage2, chat_History.Msg.chatMessage3
It's instead over-riding the reference and turning into the value '0', '1', '2'. My code:
public function onReceivedChatData(Event:LoaderEvent)
{
var raw_user_info = LoaderMax.getContent("chatHistory");
var chat_History:XML = XML(raw_user_info);
if (chat_History.Msg)
{
trace("ReceivedChatData");
trace(chat_History);
for (var i:int = 0; i < int(chat_History.chatLength); i++)
{
var chatString:String = chat_History.Msg.chatMessage;
chatString += i.toString();
shopchatbox.shop_chat_window.text = shopchatbox.shop_chat_window.text + "\n" + chatString;
shopchatwidebox.shop_chat_window.text = shopchatwidebox.shop_chat_window.text + "\n" + chatString;
}
}
else
{
trace("chat_History XML Does Not Exist!!! Noooo :( ");
trace(chat_History);
}
}
The chatLength is 3, and it's calling the for statement 3 times correctly, however chatString isn't referencing it's variable (a string) correctly and only appears as '0', '1', '2'. I'm guessing I'm not concatenating this right and that's the problem, but I'm not sure how to do this?
Thanks!
chatString += i.toString();
That's going to give you the indexes 0, 1, 2. i just stores increments for iteration. Not the values from chat_History.Msg.chatMessage So you're adding i onto the content of chatString, not setting the variable name.
var chatString:String = chat_History.Msg.chatMessage;
chatString += i.toString();
Your code here says, take chatString, and set it to the value chat_History.Msg.chatMessage, then concatenate i as a String onto the content of chatString
If you wanted to access your variables by a variable name, I believe you'd do something like this;
var chatString:String = chat_History.Msg["chatMessage"+ String(i+1)];
So i+1 = 1, 2, 3. Which should mean you're accessing chat_History.Msg.chatMessage1, chat_History.Msg.chatMessage2 and chat_History.Msg.chatMessage3
Remove the line;
chatString += i.toString();
I'm afraid I can't test this as I don't have the XML code, but hopefully that gives you a general idea. I'm not 100% sure I've got calling a variable by a variable name correct, but it is possible to call a variable by a string name.
Edit:
After doing a little testing, the syntax appears to be correct, so that should get you the values from chat_History.Msg

Checking for same values using if statement in actionscript?

I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:
public function matchingCheck():void
{
if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
FlxG.log("Yay!");
}
}
However, this is rather unwieldy and would basically cause way too much repetition for my liking.
At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.
Thanks for your help!
EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.
EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!
You could cut down on that code a little bit by using another function
private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
if ( arrayOfItemsToCheck[i] != value ) {
return false;
}
}
return true;
}
Then you just do this in your if statement:
if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
FlxG.log("Yay!");
}
That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.
How about something like this which would tell you both if a match existed and what match it was:
public function checkForMatch():void{
var rows:int = piecesArray.length;
for(var i:int=0; i<rows; i++){
var match:int = checkRow(piecesArray[i]);
if(match > -1) {
FlxG.log("Yay you matched " + match);
}
}
}
private function ckeckRow(row:Array):int{
if(row[0] == row[1] == row[2]){
return row[0];
}
return -1;
}

What does this line of Actionscript do?

I'm looking at the as3delaunay library and most of the code is clear to me. This part is not, however (note the line that I put preceded with an arrow):
public function circles():Vector.<Circle>
{
var circles:Vector.<Circle> = new Vector.<Circle>();
for each (var site:Site in _sites)
{
var radius:Number = 0;
var nearestEdge:Edge = site.nearestEdge();
=======>> !nearestEdge.isPartOfConvexHull() && (radius = nearestEdge.sitesDistance() * 0.5);
circles.push(new Circle(site.x, site.y, radius));
}
return circles;
}
For reference, isPartOfConvexHull() is found in Edge.as and looks like this:
internal function isPartOfConvexHull():Boolean
{
return (_leftVertex == null || _rightVertex == null);
}
What does !nearestEdge.isPartOfConvexHull() do? Does that mean that the radius = nearestEdge.sitesDistance() * 0.5 only executes if false is returned from the call to isPartOfConvexHull()? Does that stop execution of any other code?
It is equivalent to:
if (!nearestEdge.isPartOfConvexHull()) {
radius = nearestEdge.sitesDistance() * 0.5;
}
In the following line:
var b:Boolean = expression1 && expression2;
expression2 will not be evaluated if expression1 is false because we already know the final result: b = false.
Now in the following line:
expression1 && expression2;
The same thing happens except the fact that we are not assigning the result to a variable.
And this is exactly what happens in the line you are asking about where !nearestEdge.isPartOfConvexHull() is the first expression and (radius = nearestEdge.sitesDistance() * 0.5) is the second expression.
To extends #sch answer with some explanations (I didn't knew if editing answer to almost double it was ok).
This is based on lazy execution of the interpreter. If (!nearestEdge.isPartOfConvexHull()) is False then there's no need to execute the second part of the AND statement to know it'll be False, then it's left unexecuted. If it's true the evaluation of the complete statement is needed (and then done) to tell wether or not this boolean is True. So this is equivalent to an if statement.
TMHO this is bad code since it's to much condensed and hard to understand.

Error 1010 Actionscript 3

i get Error #1010 when i use this while loop:
while (pos.length>0)
{
coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
(pos_array[index]).x = coo.x;
(pos_array[index]).y = coo.y;
index++;
}
The error says: A term is undefined and has no properties.
What is wrong with my loop because I used the same loop for other programms and i got no such error.
Thank you for your attention.
Your while loop is breaking.
pos.length will never change and eventually pos_array[index] will be out of bounds.
When when you are out of bounds it is undefined.
So basically you are doing.
undefined.x = coo.x;
And just like the error says undefined has has no properties.
I can't see how this loop ever worked.
Try this instead much cleaner
var savedX:Number = 0
for each( var obj:Object in pos_array ){
coo = new MovieClip()
coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
obj.x = savedX;
obj.y = 0;
savedX += coo.width;
}
Without knowing what the collection contains, I'm assuming it is filled with DisplayObjects or an object that has x and y properties?
Cast the reference so the compiler understands what the collection contains. For example:
DisplayObject(pos_array[index]).x = coo.x;
DisplayObject(pos_array[index]).y = coo.y;
...or whatever type your collection contains.
Perhaps pos.length and pos_array.length are not equal when the loop starts.
Try this:
while (pos.length>0)
{
coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
if (pos_array[index])
{
(pos_array[index]).x = coo.x;
(pos_array[index]).y = coo.y;
}
index++;
}