perl6 - Typed array from non-typed array -


let's have following multi sub:

multi sub abc(int        @array) { 10, ' ', @array; }  multi sub abc(array[int] @array) { 20, ' ', @array; }  multi sub abc(str        @array) { 30, ' ', @array; }  multi sub abc(array[str] @array) { 40, ' ', @array; } 

as mentioned in this question, calling these typed arrays can verbose:

abc array[int].new([1,2,3]);  abc array[array[int]].new([array[int].new([1,2,3]), array[int].new([2,3,4])]); 

it nice if type inferred literal such this:

abc typed([1,2,3]);  abc typed([[1,2,3],[2,3,4]]);  abc typed(['a', 'b', 'c']);  abc typed([['a', 'b', 'c'], ['b', 'c', 'd']]); 

going further, let's add clause type inference us:

multi sub abc(@array) { abc typed(@array); } 

now can full inference no imposed syntax:

abc [1,2,3];  abc [[1,2,3],[2,3,4]];  abc ['a', 'b', 'c'];  abc [['a', 'b', 'c'], ['b', 'c', 'd']]; 

the above displays following:

10 [1 2 3] 20 [[1 2 3] [2 3 4]] 30 [a b c] 40 [[a b c] [b c d]] 

below simple version of typed works on:

  • array[int]
  • array[array[int]]
  • array[str]
  • array[array[str]]

my question is, how go implementing sort of type inference? there better approach? there similar facility available?

sub type-of(\obj) {     if obj.^name eq 'array'     {         if obj.map({ type-of($_).^name }).all eq obj.map({ type-of($_).^name })[0]         {             $type = type-of(obj[0]);             return array[$type];         }         return array;     }      if obj.^name eq 'int' { return int; }     if obj.^name eq 'str' { return str; } }  sub typed(\obj) {     if obj.^name eq 'array'     {         return type-of(obj)(obj.list.map({ $_.&typed }).array);     }      return (type-of(obj))(obj); } 


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -