How to stop string concatenated with array of strings in typescript - arrays

const a: string = "abc";
const b: string[] = ["def", "ghi"];
const c = a + b
produces abcdef,ghi comma separated string of items in string[]. How to let typescript know this should not be allowed in first place?

c = a + b is actually {} + \[\]
{} + []
The {} here is not parsed as an object, but instead as an empty block (§12.1, at least as long as you're not forcing that statement to be an expression, but more about that later). The return value of empty blocks is empty, so the result of that statement is the same as +[]. The unary + operator (§11.4.6) returns ToNumber(ToPrimitive(operand)). As we already know, ToPrimitive([]) is the empty string, and according to §9.3.1, ToNumber("") is 0.
a is string which is {} side and b (string[]) is []. When you sum string object with array of string, javascript implicitly converts array of string to concatenated string (Which is expected behavior)
so, there is nothing illegal to javascript here.

Related

Matlab concatenate variable string without curly braces

I'm trying to concatenate a series of strings in a loop into a variable array but the resulting strings are always within curly braces. Why does this happen, and how can I concatenate the string without them? Thanks
subs = {'abc001' 'abc002' 'abc003' 'abc004'};
for i = 1:size(subs,2)
subject = subs(i);
files_in(i).test = strcat('/home/data/','ind/',subject,'/test_ind_',subject,'.mat');
end
files_in(1)
% ans =
% test: {'/home/data/ind/abc001/test_ind_abc001.mat'}
I would like it to be:
test: '/home/data/ind/abc001/test_ind_abc001.mat'
subs is a cell array. If you index it using () notation, you will also get a cell array.
a = {'1', '2', '3'};
class(a(1))
% cell
To get the string inside the cell array you need to use {} notation to index into it.
class(a{1})
% char
When you use strcat with cell arrays, the result will be a cell array. When you use it with strings, the resut will be a string. So if we switch out (k) with {k} we get what you expect.
for k = 1:numel(subs)
subject = subs{k};
files_in(k).test = strcat('/home/data/ind/', subject, '/test_ind_', subject, '.mat');
end
A few side notes:
Don't use i as a variable. i and j are used in MATLAB to indicate sqrt(-1).
It is recommended to use fullfile to construct file paths rather than strcat.

How to convert array of strings to string in D?

I have got array of strings like:
string [] foo = ["zxc", "asd", "qwe"];
I need to create string from them. Like:
"zxc", "asd", "qwe" (yes every elements need be quoted and separate with comma from another, but it should be string, but not array of strings.
How can I do it's in functional style?
import std.algorithm, std.array, std.string;
string[] foo = ["zxc", "asd", "qwe"];
string str = foo.map!(a => format(`"%s"`, a)).join(", ");
assert(str == `"zxc", "asd", "qwe"`);
std.algorithm.map takes a lambda which converts an element in the range into something else, and it returns a lazy range where each element is the result of passing an element from the original range to the lambda function. In this case, the lambda takes the input string and creates a new string which has quotes around it, so the result of passing foo to it is a range of strings which have quotes around them.
std.array.join then takes a range and eagerly concatenates each of its elements with the given separator separating each one and returns a new array, and since it's given a range of strings it returns a string. So, by giving it the result of the call to map and ", " for the separator, you get your string made up of the original strings quoted and separated by commas.
std.algorithm.joiner would do the same thing as join, except that it results in a lazy range instead of an array.
Alternatively, use std.string.format
Your format specifier should be something like this:
auto joinedstring = format("%(\“%s\",%)", stringarray)
this is untested as I'm on mobile, but it's something similar to it!

F# script change argument in function

I've just studied F# scripting, and I have a strange situation with changing variable in it:
let ParseActual (arg:string) = let mutable value = false
if arg.[1] = '-' then value <- false
else value <- true
if arg.[1] = '-' || arg.[1] = '+' then ref arg := arg.Remove(0,2)
else (
Console.WriteLine(arg)
ref arg := arg.Remove(0,1)
Console.WriteLine(arg)
)
AddActual(arg, value)
But it doesn't change the string. First output \r and second is the same \r. What's wrong removing some characters from string and assigning it a new value?
The arg string is immutable. What you can do is to use the string to create and return a new string with changed literal but you won't be able to mutate the existing string.
It's the case with all variables (not the most fortunate word, I know) in F#, once they are created and bound to a value they cannot be changed (unless you explicitly use the mutable keyword, as you did for the bool value).

Ruby C API string and symbol equality?

Writing a C extension for a Ruby gem, I need to test a parameter value for equality with a known symbol and string.
I realize you can intern a string with
char *foo = "foo";
VALUE foo_string_value = rb_intern(foo);
and then convert it into a symbol:
VALUE foo_sym_value = ID2SYM(foo_string_value);
My question is whether I can now reliably test the parameter, which may be a symbol or string, :foo or 'foo', with these using C pointer equality:
if (param == foo_string_value || param == foo_sym_value) {
...
}
In other words, does the interning guarantee that pointer equality is also value equality for symbols and strings?
If this is the wrong approach, then what's the right one to do this comparison?
I figured this out. A reasonable way to do what I want appears to be
VALUE square_sym = ID2SYM(rb_intern("square"));
VALUE kind_as_sym = rb_funcall(kind_value, rb_intern("to_sym"), 0);
if (square_sym == kind_as_sym) {
...
}
Symbols are always interned and have unique values. Strings aren't and don't. They can be interned to get IDs that are integers unique for each string, but that doesn't help in this case.

Stripping non-numeric string elements

I Have an string array having structure like this:
arr[0] = "AB82374892";
arr[1] = "QBA9980309";
arr[2] = "AC00098320";
and so on.
How do I remove each non-numeric string elements from each array element? So that the above array becomes:
arr[0] = "82374892";
arr[1] = "9980309";
arr[2] = "00098320";
The objective is to obtain numeric only strings, so that dictionary level sorting can be performed efficiently.
For each string, use two pointers - writer and reader.
Iterate with the reader pointer on the array - if the element is not numeric, increase only it.
If the element is numeric - write it and increase both pointers.
pseudo code:
writer = reader = 0
while reader < n:
if arr[reader] is numeric:
arr[writer++] = arr[reader++]
else:
reader++
(Don't forget to add a null terminator to each string)

Resources