www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reverse and sort array elements

reply Andrey <saasecondbox yandex.ru> writes:
Hi,
Have array:
 enum array = ["qwerty", "a", "baz"];
Need to reverse and sort array elements to get this result:
 [a, ytrewq, zab]
Did this:
 enum result = array.map!(value => value.retro()).sort();
Got:
 Error: template std.algorithm.sorting.sort cannot deduce 
 function from argument types !()(MapResult!(__lambda1, 
 string[])), candidates are:
/usr/include/dmd/phobos/std/algorithm/sorting.d(1849,1): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) How to solve the problem?
Dec 18 2018
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Tuesday, 18 December 2018 at 12:07:37 UTC, Andrey wrote:
 Hi,
 Have array:
 enum array = ["qwerty", "a", "baz"];
Need to reverse and sort array elements to get this result:
 [a, ytrewq, zab]
Did this:
 enum result = array.map!(value => value.retro()).sort();
Got:
 Error: template std.algorithm.sorting.sort cannot deduce 
 function from argument types !()(MapResult!(__lambda1, 
 string[])), candidates are:
/usr/include/dmd/phobos/std/algorithm/sorting.d(1849,1): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) How to solve the problem?
There are in fact to instances of the same problem here: The problem is map and retro are lazy - they return an element at a time, and so can't be sorted. You will need to make a arrays from them: import std.array : array; import std.range : retro; import std.algorithm : map, sort; enum arr = ["qwerty", "a", "baz"]; enum result = arr .map!(value => value.retro().array) .array // This creates an array from map's result. .sort(); -- Simen
Dec 18 2018
prev sibling parent reply angel <andrey.gelman gmail.com> writes:
On Tuesday, 18 December 2018 at 12:07:37 UTC, Andrey wrote:
 Hi,
 Have array:
 enum array = ["qwerty", "a", "baz"];
Need to reverse and sort array elements to get this result:
 [a, ytrewq, zab]
Did this:
 enum result = array.map!(value => value.retro()).sort();
Got:
 Error: template std.algorithm.sorting.sort cannot deduce 
 function from argument types !()(MapResult!(__lambda1, 
 string[])), candidates are:
/usr/include/dmd/phobos/std/algorithm/sorting.d(1849,1): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) How to solve the problem?
Did you try this ? import std.stdio; import std.algorithm; import std.range; import std.conv; void main() { enum input = ["qwerty", "a", "baz"]; enum output = input.map!(value => value.retro().to!string()).array.sort(); writeln(input); writeln(output); }
Dec 18 2018
parent reply Andrey <saasecondbox yandex.ru> writes:
On Tuesday, 18 December 2018 at 12:32:35 UTC, angel wrote:
 On Tuesday, 18 December 2018 at 12:07:37 UTC, Andrey wrote:
Thank you everybody. Here was another problem that local variable 'array' shadows function 'array()' from std.array.
Dec 18 2018
parent Andrea Fontana <nospam example.org> writes:
On Tuesday, 18 December 2018 at 12:47:59 UTC, Andrey wrote:
 On Tuesday, 18 December 2018 at 12:32:35 UTC, angel wrote:
 On Tuesday, 18 December 2018 at 12:07:37 UTC, Andrey wrote:
Thank you everybody. Here was another problem that local variable 'array' shadows function 'array()' from std.array.
You call "array" the enum just like the array() function. Change enum array into enum arr and array.map!... into arr.map!...
Dec 18 2018