Do sorted ranges have any special properties?

bearophile bearophileHUGS at lycos.com
Wed Jul 28 10:52:23 PDT 2010


Philippe Sigaud:

> But the idea is that if you want to filter a sequence to find all vowels for example:
> 
> auto vowels = set("aeiou");
> auto vowelsInMyText = filter!vowels(myText);
> 
> vowels act as a predicate: if you call it with a char, it will returns true
> iff the char is in vowels. In Clojure, it produces very clean code.

In Python you can give a lamba function to filter(), or you can use:

vowels = set("aeiou")
txt = "hello how are you"
print filter(vowels.__contains__, txt)


__contains__ is the standard method used by the Python "in" operator. That code is a bit less nice than the Clojure code, but it's a bit more explicit (and you can use other member functions beside __contains__).

This is similar code in D, but it doesn't work:

import std.range, std.algorithm, std.array;
void main() {
  auto vowels = ['a':0, 'e':0, 'i':0, 'o':0, 'u':0];
  string txt = "hello how are you";
  auto f = filter!(&vowels.opBinary!("in"))(txt);
  writeln(array(f));
}

I don't know if there is some way to make that code work.

Bye,
bearophile


More information about the Digitalmars-d mailing list