The Artima Developer Community
Sponsored Link

Weblogs Forum
Iterable Concepts: Downloadable Demo

3 replies on 1 page. Most recent reply: Nov 15, 2005 4:40 PM by Chris Thiessen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Iterable Concepts: Downloadable Demo (View in Weblogs)
Posted: Nov 13, 2005 8:20 PM
Reply to this message Reply
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();
}


Chris Thiessen

Posts: 2
Nickname: cbthiess
Registered: Nov, 2005

Re: Iterable Concepts: Downloadable Demo Posted: Nov 14, 2005 7:25 PM
Reply to this message Reply
I actually implemented a major library which uses this concept heavily, to support both I/O and collections abstractions. The library is called Kataba Dynamics and you can find it at www.kataba.com.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Iterable Concepts: Downloadable Demo Posted: Nov 15, 2005 6:40 AM
Reply to this message Reply
> I actually implemented a major library which uses this
> concept heavily, to support both I/O and collections
> abstractions. The library is called Kataba Dynamics and
> you can find it at http://www.kataba.com.

That is very interesting! Having used this design approach in an industratial setting do you have any anecdottes to share with me? For instance: In general does it work well? Are there scenarios where you found it especially useful, or cumbersome? Did you find using Java in this manner at all constraining?

Thanks

Chris Thiessen

Posts: 2
Nickname: cbthiess
Registered: Nov, 2005

Re: Iterable Concepts: Downloadable Demo Posted: Nov 15, 2005 4:40 PM
Reply to this message Reply
The Iterable approach worked out really well. It makes a slew of things usable in exactly the same way: Iterators, StringTokenizers, Collections, byte-streams, char-streams, infinite series and computations, event sources, time ticks, etc. Wonderful.

The specific approach I used was to separate an Iterable (something you can iterate over) from at Iter (the actual iteration). Iter itself implemented Iterable, returning 'this' on a call to Iter.iter(). All the composition and processing methods took Iterable's as arguments, not Iter's, so could be applied to collections, iterations, or anything that could generate a stream of values.

I chose external iteration (hasNext/next) over internal iteration (for_each), partly to avoid having to implement a 'break;' concept in functions.

This worked out pretty well. Outerable/Outer took it to the next level, though. I think you can guess what they are: like Iterable/Iter, except you send values to them. It made a bunch of interop problems just go away. All the collections classes of Kataba Dynamics also implements Outerable. So, you could end up doing things like:

write_lines("sorted.txt")
.writen(list(read_lines("unsorted.txt")).sort());
OR:
List_o sorted_lines = list(read_lines("unsorted.txt")).sort());

For your examples, with:
import static com.kataba.io.Out.*;
import static com.kataba.io.Iters.*;
import static com.kataba.io.Operators.*;

// array_adapter_test
int[] a = { 1,2,3,4 };
ln(toString(elems(a)));

// generator_test - no precise equivalent
Func_t is_even = new Func_t() {
boolean f(int a) { return a%2 == 0; }
};
ln(toString(slice(filter(count(1), is_even), 0, 5)));

// filter_test
ln(toString(filter(count(10,20), is_prime)));

// composition_test
Func_i sqr = new Func_i() { int f(int a) { return a*a; }};
ln(toString(map(map(count(1,10), sqr), dec_i)));

// alternating_test - no precise equivalent
Func_i neg_if_odd = new Func_i() {
int f(int a) { return a%2==0 ? a : -a; }
};
ln(toString(map(count(1,11), neg_if_odd)));


To pull everything together, I wrote the collections I mentioned (com.kataba.coll), and a function library (com.kataba.func) which used behind-the-scenes bytecode compilation to make things go fast. I also implemented a macro language which lets you embed code-that-writes-code into your Java source.

Anyway, it turns out that selling a platform-like library is a really hard thing to do. Who would have guessed? :) So, I'm going to be open-sourcing it when I can find the time.

On another note: I like your use of _-separated names instead of camel-cased names. I've been leaning in the same direction recently, and it seems to be usefully easier to visually distinguish than the camel-casing. I think I picked it up from some Ruby source I was reading.

Flat View: This topic has 3 replies on 1 page
Topic: Introducing the Iterable Concept Previous Topic   Next Topic Topic: How They View Us

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use