Thread overview
std.algorithms filter and string[]
Apr 11, 2012
Russel Winder
Apr 11, 2012
Matt Peterson
Apr 11, 2012
David Nadlinger
Apr 11, 2012
Robert Clipsham
Apr 19, 2012
Christophe
April 11, 2012
I am having a dumb n00b moment, but I need to solve this 10 mins ago ;-)

	immutable files = ( selector == 0 ) ? [ "." ] : filter ! ( ( string x ) { return x.isFile ; } ) ( sliceOfStrings ) ;

gives me the error:

 Error: incompatible types for ((["."]) ? (filter(sliceOfStrings))):
'string[]' and 'Result'

which in one universe is fine, but in my universe is a problem as I cannot see a way of properly creating a Result instance based on the array literal.

(I have a workaround, but I'd prefer to know the proper D idiom for this.

Thanks.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


April 11, 2012
On Wednesday, 11 April 2012 at 13:55:45 UTC, Russel Winder wrote:
> I am having a dumb n00b moment, but I need to solve this 10 mins ago ;-)
>
> 	immutable files = ( selector == 0 ) ? [ "." ] : filter ! ( ( string x ) { return x.isFile ; } ) ( sliceOfStrings ) ;
>
> gives me the error:
>
>  Error: incompatible types for ((["."]) ? (filter(sliceOfStrings))):
> 'string[]' and 'Result'
>
> which in one universe is fine, but in my universe is a problem as I
> cannot see a way of properly creating a Result instance based on the
> array literal.
>
> (I have a workaround, but I'd prefer to know the proper D idiom for
> this.
>
> Thanks.

Use the array(...) function from std.array to convert "Result" to the string[].
April 11, 2012
On Wednesday, 11 April 2012 at 13:55:45 UTC, Russel Winder wrote:
> I am having a dumb n00b moment, but I need to solve this 10 mins ago ;-)
>
> 	immutable files = ( selector == 0 ) ? [ "." ] : filter ! ( ( string x ) { return x.isFile ; } ) ( sliceOfStrings ) ;
>
> gives me the error:
> […]

filter() is lazy – just use array() for eager evaluation?

David
April 11, 2012
On 11/04/2012 14:55, Russel Winder wrote:
> I am having a dumb n00b moment, but I need to solve this 10 mins ago ;-)
>
> 	immutable files = ( selector == 0 ) ? [ "." ] : filter ! ( ( string x ) { return x.isFile ; } ) ( sliceOfStrings ) ;
>
> gives me the error:
>
>   Error: incompatible types for ((["."]) ? (filter(sliceOfStrings))):
> 'string[]' and 'Result'
>
> which in one universe is fine, but in my universe is a problem as I
> cannot see a way of properly creating a Result instance based on the
> array literal.
>
> (I have a workaround, but I'd prefer to know the proper D idiom for
> this.
>
> Thanks.
>

I don't know that there's an easy way to do array -> arbitrary range as you ask (or more specifically, Result), however you can use array() from std.array to go the other way (range -> array).

-- 
Robert
http://octarineparrot.com/
April 19, 2012
Robert Clipsham , dans le message (digitalmars.D:163910), a écrit :
> On 11/04/2012 14:55, Russel Winder wrote:
>> I am having a dumb n00b moment, but I need to solve this 10 mins ago ;-)
>>
>> 	immutable files = ( selector == 0 ) ? [ "." ] : filter ! ( ( string x ) { return x.isFile ; } ) ( sliceOfStrings ) ;
>>
>> gives me the error:
>>
>>   Error: incompatible types for ((["."]) ? (filter(sliceOfStrings))):
>> 'string[]' and 'Result'
>>
>> which in one universe is fine, but in my universe is a problem as I cannot see a way of properly creating a Result instance based on the array literal.
>>
>> (I have a workaround, but I'd prefer to know the proper D idiom for this.
>>
>> Thanks.
>>
> 
> I don't know that there's an easy way to do array -> arbitrary range as you ask (or more specifically, Result), however you can use array() from std.array to go the other way (range -> array).
> 
> -- 
> Robert
> http://octarineparrot.com/


I guess you could do something like :

auto files = filter ! (string x => x.isFile) ( (selector==0)? ["."]: sliceOfStrings)

as long as "." passes isFile.

But you may not want immutable for a lazy range.