<commit_before>//############################################################################
// Programmation IV - 2002 - Week 05
//############################################################################
// $Id$

module M0 {
  def partition[a](xs: List[a], pred: a => boolean): Pair[List[a], List[a]] = {
    if (xs.isEmpty)
      Pair(List(),List())
    else {
      val tailPartition = partition(xs.tail, pred);
      if (pred(xs.head))
        Pair(xs.head :: tailPartition._1, tailPartition._2)
      else
        Pair(tailPartition._1, xs.head :: tailPartition._2)
    }
  }

  def quicksort[a] (less : (a,a) => boolean) (xs : List[a]) : List[a] = {
    if (xs.isEmpty)
      xs
    else {
      val pivot = xs.head;
      val sub = partition(xs.tail, (elem : a => less(elem, pivot)));
      quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2)
    }
  }

  def test = {
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 0)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 5)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 9)));
    System.out.println();

    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 0)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 5)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 9)));
    System.out.println();

    System.out.println(partition[int](List(7,2,1,5,4,3,8,6), (x => x < 0)));
    System.out.println(partition[int](List(7,2,1,5,4,3,8,6), (x => x < 5)));
    System.out.println(partition[int](List(7,2,1,5,4,3,8,6), (x => x < 9)));
    System.out.println();

    System.out.println(quicksort[int]((x,y) => x < y)(List(7,2,1,5,4,3,8,6)));
    System.out.println();
  }
}

//############################################################################

module M1 {
  def partition[a](xs: List[a], pred: a => boolean): Pair[List[a], List[a]] = {
    xs.foldr[Pair[List[a], List[a]]](Pair(List(), List())) {
      (x, p) => if (pred (x)) Pair(x :: p._1, p._2) else Pair(p._1, x :: p._2)
    }
  }

  def quicksort[a] (less : (a,a) => boolean) (xs : List[a]) : List[a] = {
    if (xs.isEmpty)
      xs
    else {
      val pivot = xs.head;
      val sub = partition(xs.tail, (elem : a => less(elem, pivot)));
      quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2)
    }
  }

  def test = {
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 0)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 5)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 9)));
    System.out.println();

    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 0)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 5)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 9)));<commit_msg>- Replaced foldr by foldRight<commit_after>//############################################################################
// Programmation IV - 2002 - Week 05
//############################################################################
// $Id$

module M0 {
  def partition[a](xs: List[a], pred: a => boolean): Pair[List[a], List[a]] = {
    if (xs.isEmpty)
      Pair(List(),List())
    else {
      val tailPartition = partition(xs.tail, pred);
      if (pred(xs.head))
        Pair(xs.head :: tailPartition._1, tailPartition._2)
      else
        Pair(tailPartition._1, xs.head :: tailPartition._2)
    }
  }

  def quicksort[a] (less : (a,a) => boolean) (xs : List[a]) : List[a] = {
    if (xs.isEmpty)
      xs
    else {
      val pivot = xs.head;
      val sub = partition(xs.tail, (elem : a => less(elem, pivot)));
      quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2)
    }
  }

  def test = {
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 0)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 5)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 9)));
    System.out.println();

    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 0)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 5)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 9)));
    System.out.println();

    System.out.println(partition[int](List(7,2,1,5,4,3,8,6), (x => x < 0)));
    System.out.println(partition[int](List(7,2,1,5,4,3,8,6), (x => x < 5)));
    System.out.println(partition[int](List(7,2,1,5,4,3,8,6), (x => x < 9)));
    System.out.println();

    System.out.println(quicksort[int]((x,y) => x < y)(List(7,2,1,5,4,3,8,6)));
    System.out.println();
  }
}

//############################################################################

module M1 {
  def partition[a](xs: List[a], pred: a => boolean): Pair[List[a], List[a]] = {
    xs.foldRight[Pair[List[a], List[a]]](Pair(List(), List())) {
      (x, p) => if (pred (x)) Pair(x :: p._1, p._2) else Pair(p._1, x :: p._2)
    }
  }

  def quicksort[a] (less : (a,a) => boolean) (xs : List[a]) : List[a] = {
    if (xs.isEmpty)
      xs
    else {
      val pivot = xs.head;
      val sub = partition(xs.tail, (elem : a => less(elem, pivot)));
      quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2)
    }
  }

  def test = {
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 0)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 5)));
    System.out.println(partition[int](List(1,2,3,4,5,6,7,8), (x => x < 9)));
    System.out.println();

    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 0)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 5)));
    System.out.println(partition[int](List(8,7,6,5,4,3,2,1), (x => x < 9)));
{'fraction_non_alphanumeric': 0.31220095693779903, 'fraction_numerical': 0.05570745044429255, 'mean_word_length': 3.7975409836065572, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 49, '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': '3611778', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2332, 'n_words': 365}