Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
May 31, 2015 Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
auto outputString = (char[][] inParam) => (inParam[0].length == 0 || target.startsWith(inParam[0])) && (inParam[1].length == 0 || target.endsWith(inParam[1])); auto numArr = threeDArr.filter!(a => a[0].canFind('?')). filter!(a => outputString(a[0].split('?'))). map!"to!int(a[1])"; writeln(numArr); I don't want to get into details of my code and bore you.It takes a 3d array like below: [["201212?4", "64"], ["20121235", "93"], ["2012123?", "87"], ["2012?234", "75"]] And returns range of numbers like : [64, 87, 75] Now I want to find the maximum of this range "numArr". Since I couldn't find a "max" function works on ranges I decided to sort them and take the first element. But when I tried to sort like numArr.sort(); Error: template std.algorithm.sorting.sort cannot deduce function from argument types !()(MapResult!(unaryFun, FilterResult!(__lambda3, FilterResult!(__lambda2, char[][][])))). Ok I said I will first copy the range to array then use sort. So I tried numArr.array Error: no property 'array' for type 'MapResult! Now only thing I can do with the range I have to call writeln();Why other functions of standart library are not working with this range type? |
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On 05/31/2015 12:23 AM, kerdemdemir wrote: > But when I tried to sort like numArr.sort(); > > Error: template std.algorithm.sorting.sort cannot deduce function from > argument types !()(MapResult!(unaryFun, FilterResult!(__lambda3, > FilterResult!(__lambda2, char[][][])))). > > Ok I said I will first copy the range to array then use sort. So I tried > numArr.array Yeah, that's necessary because sort() requires a random access range. > Error: no property 'array' for type 'MapResult! The solution may be as simple as importing std.array (or std.range) but we can't be sure without a short but complete code. Ali |
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | > The solution may be as simple as importing std.array (or std.range) but we can't be sure without a short but complete code.
Solution was as simple as importing std.array. Thanks a lot again Ali.
Just for curiosity; why can't I use
numArr.take(3);
source\app.d(33): Error: no property 'take' for type 'MapResult!(unaryFun, FilterResult!(__lambda3, FilterResult!(__lambda2, char[][][])))'.
|
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | And one more question out of curiosity why I couldn't find any method which will return the max element in a range. |
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On 05/31/2015 12:36 AM, kerdemdemir wrote:
>> The solution may be as simple as importing std.array (or std.range)
>> but we can't be sure without a short but complete code.
>
> Solution was as simple as importing std.array. Thanks a lot again Ali.
>
> Just for curiosity; why can't I use
> numArr.take(3);
>
> source\app.d(33): Error: no property 'take' for type
> 'MapResult!(unaryFun, FilterResult!(__lambda3, FilterResult!(__lambda2,
> char[][][])))'.
I'm pretty sure that is the following. :)
import std.algorithm;
Ali
|
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Unfortunately not this time :) import std.stdio; import std.algorithm; import std.string; import std.conv; import std.array; ---> I added this line thanks to you. |
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On 05/31/2015 12:39 AM, kerdemdemir wrote:
> And one more question out of curiosity why I couldn't find any method
> which will return the max element in a range.
I am not sure but perhaps because it is easily implemented by reduce:
import std.stdio;
import std.algorithm;
import std.range;
import std.random;
import std.format;
import std.conv;
auto maxElement(R)(R range)
{
return reduce!((current, a) => current >= a ? current : a)
(ElementType!R.min, range);
}
void main()
{
auto numArr = 100
.iota
.randomSample(20, Random(unpredictableSeed))
.array;
randomShuffle(numArr);
auto result = numArr.maxElement;
writefln("Max: %s", result);
writefln("%-(%s %)", numArr.map!(a => (a == result
? format("((%s))", a)
: a.to!string)));
}
One output:
Max: 98
93 16 74 94 23 ((98)) 9 73 71 77 8 11 36 55 66 46 42 44 58 5
Ali
|
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Sunday, 31 May 2015 at 08:02:10 UTC, Ali Çehreli wrote: > auto maxElement(R)(R range) > { > return reduce!((current, a) => current >= a ? current : a) > (ElementType!R.min, range); > } [...] > auto result = numArr.maxElement; Can be shortened to `auto result = numArr.reduce!max`; |
May 31, 2015 Re: Woldemort Type can't even call "take" or "array" | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On Sunday, 31 May 2015 at 07:45:02 UTC, kerdemdemir wrote:
> Unfortunately not this time :)
>
> import std.stdio;
> import std.algorithm;
> import std.string;
> import std.conv;
> import std.array; ---> I added this line thanks to you.
`take` is in `std.range`. Try importing that.
|
Copyright © 1999-2021 by the D Language Foundation