Skip to content

how to convert a slice to a standard D array (T[]) ? #18

@timotheecour

Description

@timotheecour

@9il
what do i use for toArray?

auto s=slice!double(3);
double[]s2= s.toArray;

actually not super clear from docs

Also, from old experimental.nslice in https://dlang.org/phobos/std_experimental_ndslice.html:

T median(Range, T)(Slice!(2, Range) sl, T[] buf)
{
    import std.algorithm.sorting : topN;
    // copy sl to the buffer
    size_t n;
    foreach (r; sl)
        foreach (e; r)
            buf[n++] = e;
    buf[0 .. n].topN(n / 2);
    return buf[n / 2];
}

this snippet (copy sl to the buffer) seems to indicate no such utility to flatten elements into an array?

EDIT: OK i guess it's here:
http://docs.algorithm.dlang.io/latest/mir_ndslice_allocation.html

  • somehow i find it hard to find, not sure if i'm the only one.

  • IMO this should be more prominent since we want to make it easy to go from standard D to slices (plenty of examples) as well as opposite direction (very sparse in docs)

  • not sure why median above doesn't use ndarray

  • the docs (Creates a common n-dimensional array from a slice.) are not clear whether it allocates or merely returns a view over the data

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions