Hi everybody,
I have a list containing numbers and I would like to transform them into characters. Is there a way to do that without using a loop? I was not able to find the proper function.
Thanks for your help!
Thanks @Craige_Hales ! I like it. Let me see if I can work it into the next Challenge. Should we try to avoid any iteration or just For (and its derivatives)? One way of avoiding iteration is through expression evaluation. If you're unfamiliar with this approach, check out @joseph_morgan 's Discovery tutorial, point 1. For the above problem, the function might look something like this:
convertNumberToCharacter = Function({inList},{i=1,outList={}},
outList[1::N Items(inList)] = Expr(Char(inList[i++]));
Eval List(outList);
);
/* You can check it out using the code below.
tStart = HP Time();
alist = As List(J(10000,1,Random Normal(1e6,1e5)));
newList = convertNumberToCharacter(alist);
Print((HP Time()-tStart)/1e6);
*/
I have tried this in a few of the Challenges and, while succinct, tends to take longer and is harder to understand (unless you are fairly familiar with expression handling).
@DonMcCormack The vanilla for-loop is both fastest, and slowest, depending on problem size. I'm really surprised @ms the words-based solution performs so well at large sizes. I really like the unexpected answers that showcase a feature in JMP like that. I'm a bit surprised for-each is faster than for, at some size. I like knowing that too.
I would not try to avoid anything. I like your current plan for a discussion, rather than a clear winner. Here are some discussion starter ideas:
There's also Transform Each, which appears to be a little faster than For Each here.
theList = Transform Each( {value}, theList, Char( value ) );