The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Iterable Concepts: Downloadable Demo
by Christopher Diggins
November 13, 2005
Summary
The Iterable concept is an extremely abstract way to treat a collection functionally, without worrying about indexers or iterators.

Advertisement

I have just uploaded a preview release of the OOTL which demonstrates the Iterable concept in action.

Here is an excerpt from the online documentation followed by some sample code from the download.

Iterable Concept

  concept {
    typedef value_type;
    template<typename Procedure>
    void for_each(Procedure f);
  }
  

The Iterable concept is a way of providing a minimalist functional interface to a collection. In the way that the STL collections start from the Container concept, the Iterable concept provides a foundation for the OOTL collections.

A model of the Iterable concept applies a Procedure to values it contains in a sequential manner. A simple example of an Iterable model in action is below:

  void array_adaptor_test() {
    writeln("This is a test of the array adapter");
    writeln("Expect the numbers 1 to 3 in sequence separated by spaces");
    int a[] = { 1, 2, 3 };
    output(array_to_iterable(a, 3));
    writeln();
  }
  

The Iterable concept is more flexible than a Container since it can easily model purely functional notions such as infinite series, or input streams, but it lacks the computational power which comes with positional information. This is similar to the trade off inherent in the usage of Input Iterators versus Random Access Iterators.

A model of the Iterable concept is expected to have shallow assignment semantics. Whether or not an iterable is single pass, is not specified at this time.

Demonstration of the Iterable Concept

void array_adaptor_test() {
  writeln("This is a test of the array adapter");
  writeln("Expect the numbers 1 to 3 in sequence separated by spaces");
  int a[] = { 1, 2, 3 };
  output(array_to_iterable(a, 3));
  writeln();
}

void generator_test() {
  writeln("This is a test of the iterable generator.");
  writeln("Expect 5 even numbers starting from 2.");
  output(iterable_gen(2, next_even(), 5));
  writeln();
}

void filter_test() {
  writeln("This is a test of the filter and adapter.");
  writeln("Expect the primes between 10-20");
  output(filter(int_range(10, 20), is_prime()));
  writeln();
}

template<typename Iterable, typename F, typename G>
void output_composition(Iterable i, F f, G g) {
  output(compose(i, compose_fxn(f, g)));
}

void composition_test() {
  writeln("This is a test of double composition.");
  writeln("Expect the squares of the values from 1 to 10, minus 1");
  output_composition(int_range(1, 11), square(), dec());
  writeln();
}

void alternating_test() {
  writeln("This is a test of conditional function composition.");
  writeln("Expect numbers from 1 to 10 with odd numbers as negatives.");
  output(
    compose(
      int_range(1, 11),
      cond_fxn(
        is_even(),
        identity(),
        negative()
      )
    )
  );
  writeln();
}

Talk Back!

Have an opinion? Readers have already posted 3 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us