<commit_before>/*     / \____  _    ______   _____ / \____   ____  _____
 *    /  \__  \/ \  / \__  \ /  __//  \__  \ /    \/ __  \   Javaslang
 *  _/  // _\  \  \/  / _\  \\_  \/  // _\  \  /\  \__/  /   Copyright 2014-2015 <NAME>
 * /___/ \_____/\____/\_____/____/\___\_____/_/  \_/____/    Licensed under the Apache License, Version 2.0
 */
package javaslang;

import javaslang.collection.IndexedSeq;
import javaslang.collection.Iterator;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * A rich extension of {@code java.lang.Iterable} and basis of all {@link Value} types, e.g. controls, collections et al.
 *
 * @param <T> Component type
 */
public interface Iterable<T> extends java.lang.Iterable<T> {

    /**
     * Returns a rich {@code javaslang.collection.Iterator}.
     *
     * @return A new Iterator
     */
    @Override
    Iterator<T> iterator();

    /**
     * Tests whether every element of this iterable relates to the corresponding element of another iterable by
     * satisfying a test predicate.
     *
     * @param <U> Component type of that iterable
     * @param that      the other iterable
     * @param predicate the test predicate, which relates elements from both iterables
     * @return {@code true} if both iterables have the same length and {@code predicate(x, y)}
     * is {@code true} for all corresponding elements {@code x} of this iterable and {@code y} of {@code that},
     * otherwise {@code false}.
     */
    default <U> boolean corresponds(java.lang.Iterable<U> that, BiPredicate<T, U> predicate) {
        final java.util.Iterator<T> it1 = iterator();
        final java.util.Iterator<U> it2 = that.iterator();
        while (it1.hasNext() && it2.hasNext()) {
            if (!predicate.test(it1.next(), it2.next())) {
                return false;
            }
        }
        return !it1.hasNext() && !it2.hasNext();
    }

    /**
     * A <em>smoothing</em> replacement for {@code equals}. It is similar to Scala's {@code ==} but better in the way
     * that it is not limited to collection types, e.g. `Some(1) eq List(1)`, `None eq Failure(x)` etc.
     *
     * <pre><code>
     * o == this                       : true
     * o instanceof javaslang.Iterable : all iterable elements (this or that) are eq, all non-iterable elements are equal
     * o instanceof java.lang.Iterable : this.eq(Iterator.ofAll((java.lang.Iterable&lt;?&gt;) o));
     * otherwise                       : false
     * </code></pre>
     *
     * @param o An object
     * @return true, if this equals o according to the rules defined above, otherwise false.
     */
    default boolean eq(Object o) {
        if (o == this) {
            return true;
        } else if (o instanceof Iterable) {
            final Iterable<?> that = (Iterable<?>) o;
            final Iterator<?> iter1 = this.iterator();
            final Iterator<?> iter2 = that.iterator();
            // TODO: use corresponds and remove duplicate code
            while (iter1.hasNext() && iter2.hasNext()) {
                final Object o1 = iter1.next();
                final Object o2 = iter2.next();
                if (o1 instanceof Iterable) {
                    final Iterable<?> iterable1 = (Iterable<?>) o1;
                    if (!iterable1.eq(o2)) {
                        return false;
                    }
                } else if (o2 instanceof Iterable) { // premise: symmetrie of eq
                    final Iterable<?> iterable2 = (Iterable<?>) o2;
                    if (!iterable2.eq(o1)) {
                        return false;
                    }
                } else { // members of o1 and o2 are not necessarily compared via eq, even if applicable
                    if (!Objects.equals(o1, o2)) {
                        return false;
                    }
                }
            }
            return iter1.hasNext() == iter2.hasNext();
        } else if (o instanceof java.lang.Iterable) {
            final Iterable<?> that = Iterator.ofAll((java.lang.Iterable<?>) o);
            return this.eq(that);
        } else {
            return false;
        }
    }
<commit_msg>removed duplicate code in eq<commit_after>/*     / \____  _    ______   _____ / \____   ____  _____
 *    /  \__  \/ \  / \__  \ /  __//  \__  \ /    \/ __  \   Javaslang
 *  _/  // _\  \  \/  / _\  \\_  \/  // _\  \  /\  \__/  /   Copyright 2014-2015 <NAME>
 * /___/ \_____/\____/\_____/____/\___\_____/_/  \_/____/    Licensed under the Apache License, Version 2.0
 */
package javaslang;

import javaslang.collection.IndexedSeq;
import javaslang.collection.Iterator;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * A rich extension of {@code java.lang.Iterable} and basis of all {@link Value} types, e.g. controls, collections et al.
 *
 * @param <T> Component type
 */
public interface Iterable<T> extends java.lang.Iterable<T> {

    /**
     * Returns a rich {@code javaslang.collection.Iterator}.
     *
     * @return A new Iterator
     */
    @Override
    Iterator<T> iterator();

    /**
     * Tests whether every element of this iterable relates to the corresponding element of another iterable by
     * satisfying a test predicate.
     *
     * @param <U> Component type of that iterable
     * @param that      the other iterable
     * @param predicate the test predicate, which relates elements from both iterables
     * @return {@code true} if both iterables have the same length and {@code predicate(x, y)}
     * is {@code true} for all corresponding elements {@code x} of this iterable and {@code y} of {@code that},
     * otherwise {@code false}.
     */
    default <U> boolean corresponds(java.lang.Iterable<U> that, BiPredicate<T, U> predicate) {
        final java.util.Iterator<T> it1 = iterator();
        final java.util.Iterator<U> it2 = that.iterator();
        while (it1.hasNext() && it2.hasNext()) {
            if (!predicate.test(it1.next(), it2.next())) {
                return false;
            }
        }
        return !it1.hasNext() && !it2.hasNext();
    }

    /**
     * A <em>smoothing</em> replacement for {@code equals}. It is similar to Scala's {@code ==} but better in the way
     * that it is not limited to collection types, e.g. `Some(1) eq List(1)`, `None eq Failure(x)` etc.
     *
     * <pre><code>
     * o == this                       : true
     * o instanceof javaslang.Iterable : all iterable elements (this or that) are eq, all non-iterable elements are equal
     * o instanceof java.lang.Iterable : this.eq(Iterator.ofAll((java.lang.Iterable&lt;?&gt;) o));
     * otherwise                       : false
     * </code></pre>
     *
     * @param o An object
     * @return true, if this equals o according to the rules defined above, otherwise false.
     */
    default boolean eq(Object o) {
        if (o == this) {
            return true;
        } else if (o instanceof Iterable) {
            final Iterable<?> that = (Iterable<?>) o;
            return this.iterator().corresponds(that.iterator(), (o1, o2) ->  {
                if (o1 instanceof Iterable) {
                    return ((Iterable<?>) o1).eq(o2);
                } else if (o2 instanceof Iterable) {
                    return ((Iterable<?>) o2).eq(o1);
                } else {
                    return Objects.equals(o1, o2);
                }
            });
        } else if (o instanceof java.lang.Iterable) {
            final Iterable<?> that = Iterator.ofAll((java.lang.Iterable<?>) o);
            return this.eq(that);
        } else {
            return false;
        }
    }

{'fraction_non_alphanumeric': 0.12715214896059177, 'fraction_numerical': 0.009437571738298687, 'mean_word_length': 1.9012208657047724, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2476750', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2288, 'n_words': 677}