<commit_before>doc "Abstract supertype of containers whose elements may be 
     iterated. An iterable container need not be finite, but
     its elements must at least be countable. There may not
     be a well-defined iteration order, and so the order of
     iterated elements may not be stable."
see (Collection)
by "Gavin"
shared interface Iterable<out Element> 
        satisfies Container {

    doc "An iterator for the elements belonging to this 
         container."
    shared formal Iterator<Element> iterator;

    doc "Determines if the iterable object is empty, that is
         to say, if the iterator returns no elements."
    shared actual default Boolean empty {
        return is Finished iterator.next();
    }

    doc "A sequence containing the elements returned by the
         iterator."
    shared default Element[] sequence {
        value builder = SequenceBuilder<Element>();
        builder.appendAll(this...);
        return builder.sequence;
    }

    doc "Returns an Iterable that will return the transformation of the original elements."
    shared default Iterable<Result> map<Result>(Result collecting(Element elem)) {
        object mapped satisfies Iterable<Result> {
            value outerIterator {
               return outer.iterator;
            }
            shared actual Iterator<Result> iterator {
                object mappedIterator satisfies Iterator<Result> {
                    Iterator<Element> iter = outerIterator;
                    shared actual Result|Finished next() {
                        value e = iter.next();
                        if (is Element x=e) {
                            return collecting(x);
                        } else {
                            return exhausted;
                        }
                    }
                }
                return mappedIterator;
            }
        }
        return mapped;
    }

    doc "Returns an Iterable that returns only the elements which satisfy the collecting condition."
    shared default Iterable<Element> filter(Boolean selecting(Element elem)) {
        object filtered satisfies Iterable<Element> {
            shared actual Iterator<Element> iterator {
                object filteredIterator satisfies Iterator<Element> {
                    Iterator<Element> iter = outer.iterator;
                    shared actual Element|Finished next() {
                        variable Element|Finished e := iter.next();
                        variable Boolean flag;
                        if (is Element x=e) {
                            flag := selecting(x);
                        } else {
                            flag := true;
                        }
                        while (!flag) {
                            e := iter.next();
                            if (is Element x=e) {
                                flag := selecting(x);
                            } else {
                                flag := true;
                            }
                        }
                        return e;
                    }
                }
                return filteredIterator;
            }
        }
        return filtered;
    }

    doc "Returns the result of applying the accumulating function to each element." 
    shared default Result fold<Result>(Result initial,
            Result accumulating(Result partial, Element elem)) {
        variable value r := initial;
        for (e in this) {
            r := accumulating(r, e);
        }
        return r;
    }

    doc "Return the first element which satisfies the selecting function, if any."
    shared default Element? find(Boolean selecting(Element elem)) {
        for (e in this) {
            if (selecting(e)) {
                return e;
            }
        }
        return null;
    }
 
}<commit_msg>simplify implementations using comprehensions, and improve doc<commit_after>doc "Abstract supertype of containers whose elements may be 
     iterated. An iterable container need not be finite, but
     its elements must at least be countable. There may not
     be a well-defined iteration order, and so the order of
     iterated elements may not be stable."
see (Collection)
by "Gavin"
shared interface Iterable<out Element> 
        satisfies Container {

    doc "An iterator for the elements belonging to this 
         container."
    shared formal Iterator<Element> iterator;

    doc "Determines if the iterable object is empty, that is
         to say, if the iterator returns no elements."
    shared actual default Boolean empty {
        return is Finished iterator.next();
    }

    doc "A sequence containing the elements returned by the
         iterator."
    shared default Element[] sequence {
        return { this... };
    }

    doc "An `Iterable` containing the results of applying
         the given mapping to the elements of to this 
         container."
    shared default Iterable<Result> map<Result>(
            doc "The mapping to apply to the elements."
            Result collecting(Element elem)) {
        return entries(for (elem in this) collecting(elem));
    }

    doc "An `Iterable` containing the elements of this 
         container that satisfy the given predicate."
    shared default Iterable<Element> filter(
            doc "The predicate the elements must satisfy."
            Boolean selecting(Element elem)) {
        return entries(for (elem in this) if (selecting(elem)) elem);
    }

    doc "The result of applying the accumulating function to 
         each element of this container in turn." 
    shared default Result fold<Result>(Result initial,
            doc "The accumulating function that accepts an
                 intermediate result, and the next element."
            Result accumulating(Result partial, Element elem)) {
        variable value r := initial;
        for (e in this) {
            r := accumulating(r, e);
        }
        return r;
    }

    doc "The first element which satisfies the given 
         predicate, if any."
    shared default Element? find(
            doc "The predicate the element must satisfy."
            Boolean selecting(Element elem)) {
        for (e in this) {
            if (selecting(e)) {
                return e;
            }
        }
        return null;
    }
 
}
{'fraction_non_alphanumeric': 0.06407828282828283, 'fraction_numerical': 0.0, 'mean_word_length': 1.6470342522974102, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2538243', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1362, 'n_words': 589}