<commit_before>import ceylon.build.task { Goal, Writer, GoalGroup }
import ceylon.collection { LinkedList, MutableList, HashSet, MutableSet }

shared {Goal*} buildGoalExecutionList({<Goal|GoalGroup>+} definitions, String[] arguments, Writer writer) {
    value goalsRequested = findGoalsToExecute(definitions, arguments, writer);
    MutableList<Goal> goalsToExecute = LinkedList<Goal>();
    for (goal in goalsRequested) {
        goalsToExecute.addAll(linearize(goal));
    }
    return reduce(goalsToExecute);
}

shared {Goal*} findGoalsToExecute({<Goal|GoalGroup>+} definitions, String[] arguments, Writer writer) {
    MutableList<Goal> goalsToExecute = LinkedList<Goal>();
    for (argument in arguments) {
        if (!argument.startsWith(argumentPrefix)) {
            for (definition in definitions) {
                if (definition.name.equals(argument)) {
                    switch (definition)
                    case (is Goal) {
                        goalsToExecute.add(definition);
                    }
                    case (is GoalGroup) {
                        goalsToExecute.addAll(goalsList(definition.goals));
                    }
                    break;
                }
            } else {
                writer.error("# goal '``argument``' not found, stopping");
                return {};
            }
        }
    }
    return goalsToExecute;
}

shared {Goal*} linearize(Goal goal) {
    MutableList<Goal> goals = LinkedList<Goal>();
    for (<Goal|GoalGroup> dependency in goal.dependencies) {
        switch (dependency)
        case (is Goal) {
            goals.addAll(linearize(dependency));
        }
        case (is GoalGroup) {
            value groupGoals = goalsList(dependency.goals);
            for (groupGoal in groupGoals) {
                goals.addAll(linearize(groupGoal));
            }
        }
    }
    goals.add(goal);
    return goals;
}

shared {Goal*} reduce({Goal*} goals) {
    MutableSet<Goal> reducedGoalsSet = HashSet<Goal>();
    MutableList<Goal> reducedGoals = LinkedList<Goal>();
    for (Goal goal in goals) {
        if (!reducedGoalsSet.contains(goal)) {
            reducedGoals.add(goal);
            reducedGoalsSet.add(goal);
        }
    }
    return reducedGoals;
}<commit_msg>Update reduce to use goal names comparison instead of goal comparison<commit_after>import ceylon.build.task { Goal, Writer, GoalGroup }
import ceylon.collection { LinkedList, MutableList, HashSet, MutableSet }

shared {Goal*} buildGoalExecutionList({<Goal|GoalGroup>+} definitions, String[] arguments, Writer writer) {
    value goalsRequested = findGoalsToExecute(definitions, arguments, writer);
    MutableList<Goal> goalsToExecute = LinkedList<Goal>();
    for (goal in goalsRequested) {
        goalsToExecute.addAll(linearize(goal));
    }
    return reduce(goalsToExecute);
}

shared {Goal*} findGoalsToExecute({<Goal|GoalGroup>+} definitions, String[] arguments, Writer writer) {
    MutableList<Goal> goalsToExecute = LinkedList<Goal>();
    for (argument in arguments) {
        if (!argument.startsWith(argumentPrefix)) {
            for (definition in definitions) {
                if (definition.name.equals(argument)) {
                    switch (definition)
                    case (is Goal) {
                        goalsToExecute.add(definition);
                    }
                    case (is GoalGroup) {
                        goalsToExecute.addAll(goalsList(definition.goals));
                    }
                    break;
                }
            } else {
                writer.error("# goal '``argument``' not found, stopping");
                return {};
            }
        }
    }
    return goalsToExecute;
}

shared {Goal*} linearize(Goal goal) {
    MutableList<Goal> goals = LinkedList<Goal>();
    for (<Goal|GoalGroup> dependency in goal.dependencies) {
        switch (dependency)
        case (is Goal) {
            goals.addAll(linearize(dependency));
        }
        case (is GoalGroup) {
            value groupGoals = goalsList(dependency.goals);
            for (groupGoal in groupGoals) {
                goals.addAll(linearize(groupGoal));
            }
        }
    }
    goals.add(goal);
    return goals;
}

shared {Goal*} reduce({Goal*} goals) {
    MutableSet<String> reducedGoalsNames = HashSet<String>();
    MutableList<Goal> reducedGoals = LinkedList<Goal>();
    for (Goal goal in goals) {
        if (!reducedGoalsNames.contains(goal.name)) {
            reducedGoals.add(goal);
            reducedGoalsNames.add(goal.name);
        }
    }
    return reducedGoals;
}
{'fraction_non_alphanumeric': 0.11144839549002603, 'fraction_numerical': 0.0, 'mean_word_length': 2.2056984016678247, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '4503269', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1281, 'n_words': 272}