Implementing the map function for a Pony array - ponylang

I have been playing with Pony arrays to understand Pony better, and wanted to write the map function for any arrays.
I am talking about something like the standard map function most languages have nowadays for converting elements of collections, as in Clojure:
(map #(+ 1 %) [1 2 3]) ; => [2 3 4]
But I want it to actually modify the given array, not return a new one.
My current attempt so far runs into many errors due to capabilities:
// array is "iso" so I can give it to another actor and change it
let my_array: Array[U64] iso = [1; 2; 3; 4]
// other actor tries to recover arrays as "box" just to call pairs() on it
let a = recover box my_array end // ERROR: can't recover to this capability
for (i, item) in a.pairs() do
// TODO set item at i to some other mapped value
try my_array.update(i, fun(item))? end
end
Anyone knows how this can be done

Alright, took me a while, but I was able to get things working.
Here's my basic understanding of what's going on (please correct me if I'm wrong)!
The first step was to understand that we need to use aliases to change the capabilities of a variable in Pony.
So, in order to make an iso variable useable as a box, one must alias it by basically, consuming it into another variable:
let a: Array[U64] ref = consume array // array is "iso"
for (i, item) in a.pairs() do
try a.update(i, item + n)? end
end
This works!!
One more problem I had was that I couldn't do much with the resulting Array[U64] ref. Can't pass it to anyone, for example.
So I wrapped the whole thing into a recover block in order to end up with the same array, but as a val (immutable reference to the array) which is more useful as I can send it to other actors:
let result = recover val
let a: Array[U64] ref = consume array
for (i, item) in a.pairs() do
try a.update(i, item + n)? end
end
a
end
Now I can send result to anyone!

Related

How to convert string to variable name which will be passed into plot function

How I can convert a string to a variable which will be passed into plot function?
Time = [0,1,2,3];
A = sin(Time);
B = cos(Time);
c = 2*sin(Time);
lookup = {"A", "Freq(Hz)"; "B", "Pressure(bar)", "c", "time(ms),....};
for i=1:length(lookup)
plot(Time, lookup(i,1))
ylabel(lookup(i,2))
end
I want to plot Time vs A and Time vs B and Time vs C likewise I have 50 different variables to plot.
So I planned to create the lookup with string and planned to pass as variable to plot function using eval function call.
But in few places I read that using eval is not good option so kindly suggest the alternate method.
This will solve your immediate problem:
Replace
lookup = {"A", "Freq(Hz)"; "B", "Pressure(bar)", "c", "time(ms)", ...};
with
lookup = {A, "Freq(Hz)"; B, "Pressure(bar)", c, "time(ms)", ...};
Cell arrays are heterogeneous containers, each element can be an array of any time, independently from all other types.
With this change, the rest of your code should work as intended. You might want to add a figure command or a print command inside your loop, as each plot will overwrite the previous one. figure creates a new figure window to plot in, you'll have 50 windows (not so nice). print can save the plot to a file, which might be a better approach here. Don't try to combine the 50 data lines into a single plot, it'll be a mess!
On a larger scale, you might want to rethink your strategy with defining 50 different variables. Cell arrays and struct arrays are really good ways to go about this. For example, you can think of
data.A = sin(Time);
data.B = cos(Time);
data.c = 2*sin(Time);
or
data(1).values = sin(Time);
data(1).name = "A";
data(1).units = "Freq(Hz)";
data(2).values = cos(Time);
data(2).name = "B";
data(2).units = "Pressure(bar)";
data(3).values = 2*sin(Time);
data(3).name = "c";
data(3).units = "time(ms)";
Note that, in the first case, you can also index with data.("A"), which brings you pretty close to your original idea, except that you don't have 50 variables in your workspace, but one single data structure that is easier to deal with.
Here is a very detailed list of reasons why eval can be bad to use. That link also shows some alternatives, similar to what I summarized above.

Maximum value of fields of nested structure

I have following structure:
S.s1.val = 1;
S.s2.val= 5;
S.s3.val= 4;
...
S.s10.value = 3;
How can I find max value of all val fields without using loops. And what is general solution to apply functions to all nested structure fields?
There is no general solution, but one way to think of is structfun to collect the data you want to process to an array.
maxval = max( structfun(#(x) x.val, S) )
Internally structfun works serially like a loop, so if you're really into speed, don't use structs (or cell arrays).

How to write a function (or a macro) to create a Vec and a reference to it?

So I have something like this:
let v = vec![...];
let s = Data { vec: &v, ... };
Perhaps this is misguided, but the idea is that many data structures could share the same vector. But for the case where I don't want to share, it would be convenient to have something like this:
let (v, s) = make_data(...);
Apparently, unlike the first example, there is no way to connect the lifetime of v and s (correct me if I'm wrong). Anyway, I understand the borrow checker rejects this. So I end up doing:
let v = vec![];
let s = make_data(&v, ...);
Now, perhaps, I could make one of those work:
let (v, s) = make_data!(...);
let s = make_data!(v, ...);
let s = make_data!(...);
The problem here is that thse macros would expand to something like { let v = vec![]; ... } and v's destructor will be run in the end of this block, but what I really want is to have it expand to something like the first example.
Now, I can make this work:
make_data!(v, s, ...);
But it's odd. Is there any other way to solve this?
Rust allows you to define multiple variables with the same name in the same block.
let a = vec![...];
let a = Data { vec: &a, ... };
On the second line, the new a is not in scope yet, so you can still refer to the previous definition of a. However, on the following statements, you can no longer refer to the original a definition, since the second definition shadows the first one; nevertheless, the Vec remains alive until the end of the block, as usual.
You can take advantage of this in your macro by only taking a single identifier and using it for both the Vec and the slice.

Strange behavior in coffeescript for loop

I'm trying to iterate over array in CoffeeScript but there is some kind of strange behavior.
For example look on this code:
kapa = [1]
for i in kapa
console.log 't'
kapa.push 1
I expect that this code produce an infinite sequence of 't' symbols in console. But in reality it prints only one 't'.
What is the logic behind this behavior?
And how can I achieve expected behavior?
Explanation:
elements= [1]
for i in elements
proccess(element)
if someCond
newElement = ...
element.push(newElement) #
I want to achieve behavior when all newElements will be processed
The reason it only prints it out once is because that coffeescript compiles to this JS:
var i, kapa, _i, _len;
kapa = [1];
for (_i = 0, _len = kapa.length; _i < _len; _i++) {
i = kapa[_i];
console.log('t');
kapa.push(1);
}
So it calculates the length of the array in the beginning and doesn't get updated when you kapa.push(1). I would say this is expected behavior and moreover, modifying an array while you are iterating over it sounds like a bad idea. In other languages like Java, you would get an Exception if you tried to do that.
If you really want it to print infinite t and push 1 onto the array every time, you would need an infinite loop:
kapa = [1]
while true
console.log( 't' )
kapa.push( 1 )
(but thats obviously discouraged and I'm not sure why you would want that behavior)
UPDATE
Based on the updated question, you could use the array as a queue-like structure
elements= [1]
while nextElem = elements.shift()
proccess(nextElem)
if someCond
newElement = ...
elements.push(newElement)
Given your added explanation, maybe you are using your array as a queue:
elements= [1]
while elements.length
proccess(elements.shift())
if someCond
newElement = ...
element.push(newElement)
Notice the use of a while loop (whose end condition is evaluated at each iteration), and elements.shift that removes the first element of the queue.

Looping through documents in matlab

I am attempting to loop through the variable 'docs' which is a cell array that holds strings, i need to make a for loop that colllects the terms in a cell array and then uses command 'lower' and unique to create a dictionary.
Here is the code i've tried sp far and i just get errors
docsLength = length(docs);
for C = 1:docsLength
list = tokenize(docs, ' .,-');
Mylist = [list;C];
end
I get these errors
Error using textscan
First input must be of type double or string.
Error in tokenize (line 3)
C = textscan(str,'%s','MultipleDelimsAsOne',1,'delimiter',delimiters);
Error in tk (line 4)
list = tokenize(docs, ' .,-');
Generically, if you get an "must be of type" error, that means you are passing the wrong sort of input to a function. In this case you should look at the point in your code where this is taking place (here, in tokenize when textscan is called), and doublecheck that the input going in is what you expect it to be.
As tokenize is not a MATLAB builtin function, unless you show us that code we can't say what those inputs should be. However, as akfaz mentioned in comments, it is likely that you want to pass docs{C} (a string) to tokenize instead of docs (a cell array). Otherwise, there's no point in having a loop as it just repeatedly passes the same input, docs, into the function.
There are additional problems with the loop:
Mylist = [list; C]; will be overwritten each loop to consist of the latest version of list plus C, which is just a number (the index of the loop). Depending on what the output of tokenize looks like, Mylist = [Mylist; list] may work but you should initialise Mylist first.
Mylist = [];
for C = 1:length(docs)
list = tokenize(docs{C}, ' .,-');
Mylist = [Mylist; list];
end