as

Convenience function that creates a lazy view, where each element of the original slice is converted to the type T. It uses map and $(TT to)  composition under the hood.

  1. auto as(Slice!(Iterator, N, kind) slice)
    template as(T)
    @optmath
    @optmath
    as
    (
    Iterator
    size_t N
    SliceKind kind
    )
    (
    Slice!(Iterator, N, kind) slice
    )
  2. auto as(S[] array)
  3. auto as(S withAsSlice)
  4. auto as(Range r)

Members

Functions

as
auto as(Slice!(Iterator, N, kind) slice)
auto as(S[] array)
auto as(S withAsSlice)
auto as(Range r)

Parameters

slice

a slice to create a view on.

Return Value

A lazy slice with elements converted to the type T.

Examples

import mir.ndslice.slice: Slice;
import mir.ndslice.allocation : slice;
import mir.ndslice.topology : diagonal, as;

auto matrix = slice!double([2, 2], 0);
auto stringMatrixView = matrix.as!int;
assert(stringMatrixView ==
        [[0, 0],
         [0, 0]]);

matrix.diagonal[] = 1;
assert(stringMatrixView ==
        [[1, 0],
         [0, 1]]);

/// allocate new slice composed of strings
Slice!(int*, 2) stringMatrix = stringMatrixView.slice;

Special behavior for pointers to a constant data.

import mir.ndslice.allocation : slice;
import mir.ndslice.slice: Contiguous, Slice;

Slice!(double*, 2)              matrix = slice!double([2, 2], 0);
Slice!(const(double)*, 2) const_matrix = matrix.as!(const double);

Ranges

import mir.algorithm.iteration: filter, equal;
assert(5.iota.filter!"a % 2".as!double.map!"a / 2".equal([0.5, 1.5]));

See Also

Meta