www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.algorithms filter and string[]

reply Russel Winder <russel winder.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

I am having a dumb n00b moment, but I need to solve this 10 mins ago ;-)

	immutable files =3D ( selector =3D=3D 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.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
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
Apr 11 2012
next sibling parent "Matt Peterson" <ricochet1k gmail.com> writes:
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[].
Apr 11 2012
prev sibling next sibling parent "David Nadlinger" <see klickverbot.at> writes:
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
Apr 11 2012
prev sibling parent reply Robert Clipsham <robert octarineparrot.com> writes:
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/
Apr 11 2012
parent travert phare.normalesup.org (Christophe) writes:
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.
Apr 19 2012