Can't add a const ubyte to a dynamic array of ubyte?

a11e99z black80 at bk.ru
Tue Aug 20 09:45:27 UTC 2019


On Tuesday, 20 August 2019 at 09:27:36 UTC, ads wrote:
> import std.stdio;
>
> ubyte[] extend(in uint[] arr)
> {
> 	ubyte[] result;
> 	foreach (n; arr)
> 	{
> 		if (n < 10)
> 			result ~= n;
> 		else
> 		{
> 			import std.conv : to;
> 			foreach (digit; n.to!string)
> 				result ~= digit.to!ubyte;
> 		}
> 	}
> 	return result;
> }
>
> unittest
> {
> 	import std.algorithm : equal;
> 	assert(extend([1, 25, 70, 0]).equal([1, 2, 5, 7, 0, 0]));
> }
>
> How can I get around this? I want to ensure that the array is 
> not mutated in the function in the signature too.

what u using here?
> result ~= digit.to!ubyte;
why not to do same in line?
> result ~= n;

2) digit is '0'+(0..9) so u need subtract '0' ('0' is \x30)


More information about the Digitalmars-d-learn mailing list