<commit_before>defprotocol Enumerable do
  @moduledoc """
  This is the protocol used by the `Enum` module.

  Usually, when you invoke a function in the module `Enum`, the first argument
  passed to it is a collection which is forwarded to this protocol in order to
  perform operations on the collection.

  For example, in the expression

      Enum.map([1, 2, 3], &1 * 2)

  `Enum.map` invokes `Enumerable.reduce` to perform the reducing operation
  that builds a mapped list by calling the mapping function `&1 * 2` on every
  element in the collection and cons'ing the element with the accumulated list.
  """

  @only [List, Record, Function]

  @doc """
  This function performs the reducing operation on a given collection. It
  returns the accumulated value of applying the given function `fun` on every
  element with the accumulated value.

  As an example, here is the implementation of `reduce` for lists:

      def reduce([h|t], acc, fun), do: reduce(t, fun.(h, acc), fun)
      def reduce([], acc, _fun),   do: acc

  As an additional example, here is the implementation of `Enum.map` with
  `Enumerable`:

      def map(collection, fun) do
        Enumerable.reduce(collection, [], fn(entry, acc) ->
          [fun.(entry)|acc]
        end) |> :lists.reverse
      end
  """
  def reduce(collection, acc, fun)

  @doc """
  The function used to check if a value exists within the collection.
  """
  def member?(collection, value)

  @doc """
  The function used to retrieve the collection's size.
  """
  def count(collection)
end

defmodule Enum do
  import Kernel, except: [max: 2, min: 2]

  @moduledoc """
  Provides a set of algorithms that enumerate over collections according to the
  `Enumerable` protocol. Most of the functions in this module have two
  flavours. If a given collection implements the mentioned protocol (like
  list, for instance), you can do:

      Enum.map([1, 2, 3], fn(x) -> x * 2 end)

  Depending on the type of the collection, the user-provided function will
  accept a certain type of argument. For dicts, the argument is always a
  `{ key, value }` tuple.
  """

  @compile :inline_list_funcs

  @type t :: Enumerable.t
  @type element :: any
  @type index :: non_neg_integer
  @type default :: any

  @doc """
  Checks if the `value` exists within the `collection`.

  ## Examples

      iex> Enum.member?(1..10, 5)
      true
      iex> Enum.member?([:a, :b, :c], :d)
      false

  """
  @spec member?(t, element) :: boolean
  def member?(collection, value) do
    Enumerable.member?(collection, value)
  end

  @doc """
  Returns the collection size.

  ## Examples

      iex> Enum.count([1, 2, 3])
      3

  """
  @spec count(t) :: non_neg_integer
  def count(collection) do
    Enumerable.count(collection)
  end

  @doc """
  Counts for how many items the function returns true.

  ## Examples
      iex> Enum.count([1, 2, 3, 4, 5], fn(x) -> rem(x, 2) == 0 end)
      2

  """
  @spec count(t, (element -> as_boolean(term))) :: non_neg_integer
  def count(collection, fun) do
    Enumerable.reduce(collection, 0, fn(entry, acc) ->
      if fun.(entry), do: acc + 1, else: acc
    end)
  end

  @doc """
  Invokes the given `fun` for each item in the `collection` and returns true if
  each invocation returns true as well, otherwise it short-circuits and returns
  false.

  ## Examples

      iex> Enum.all?([2, 4, 6], fn(x) -> rem(x, 2) == 0 end)
      true

      iex> Enum.all?([2, 3, 4], fn(x) -> rem(x, 2) == 0 end)
      false

  If no function is given, it defaults to checking if
  all items in the collection evaluate to true.

      iex> Enum.all?([1, 2, 3])
      true
      iex> Enum.all?([1, nil, 3])
      false

  """
  @spec all?(t) :: boolean
  @spec all?(t, (element -> as_boolean(term))) :: boolean

  def all?(collection, fun // fn(x) -> x end)

  def all?(collection, fun) when is_list(collection) do
    do_all?(collection, fun)
  end

  def all?(collection, fun) do
    Enumerable.reduce(collection, true, fn(entry, _) ->
      if fun.(entry), do: true, else: throw(:enum_all?)
    end)
  catch
    :enum_all? -> false
  end

  @doc """
  Invokes the given `fun` for each item in the `collection` and returns true if
  at least one invocation returns true. Returns false otherwise.

  ## Examples

      iex> Enum.any?([2, 4, 6], fn(x) -> rem(x, 2) == 1 end)
      false

      iex> Enum.any?([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
      true

  If no function is given, it defaults to checking if
  at least one item in the collection evaluates to true.

      iex> Enum.any?([false, false, false])
      false
      iex> Enum.any?([false, true, false])
      true

  """
  @spec any?(t) :: boolean
  @spec any?(t, (element -> as_boolean(term))) :: boolean

  def any?(collection, fun // fn(x) -> x end)

  def any?(collection, fun) when is_list(collection) do
    do_any?(collection, fun)
  end

  def any?(collection, fun) do
    Enumerable.reduce(collection, false, fn(entry, _) ->
      if fun.(entry), do: throw(:enum_any?), else: false
    end)
  catch
    :enum_any? -> true
  end

  @doc """
  Finds the element at the given index (zero-based).
  Raises out of bounds error in case the given position
  is outside the range of the collection.

  Expects an ordered collection.

  ## Examples

      iex> Enum.at([2, 4, 6], 0)
      2
      iex> Enum.at([2, 4, 6], 2)
      6
      iex> Enum.at([2, 4, 6], 4)
      nil
      iex> Enum.at([2, 4, 6], 4, :none)
      :none

  """
  @spec at(t, index) :: element | nil
  @spec at(t, index, default) :: element | default
  def at(collection, n, default // nil) when n >= 0 do
    case fetch(collection, n) do
      { :ok, h } -> h
      :error     -> default
    end
  end

  @doc """
  Drops the first `count` items from the collection.
  Expects an ordered collection.

  ## Examples

      iex> Enum.drop([1, 2, 3], 2)
      [3]
      iex> Enum.drop([1, 2, 3], 10)
      []
      iex> Enum.drop([1, 2, 3], 0)
      [1,2,3]

  """
  @spec drop(t, integer) :: list
  def drop(collection, count) when is_list(collection) and count >= 0 do
    do_drop(collection, count)
  end

  def drop(collection, count) when count >= 0 do
    { list, _ } = Enumerable.reduce(collection, { [], count }, fn
      _, { _, count } when count > 0 -> { [], count - 1 }
      entry, { list, count }         -> { [entry|list], count }
    end)
    :lists.reverse(list)
  end

  def drop(collection, count) when count < 0 do
    { list, count } = iterate_and_count(collection, count)
    drop(list, count)
  end

  @doc """
  Drops items at the beginning of `collection` while `fun` returns true.
  Expects an ordered collection.

  ## Examples

      iex> Enum.drop_while([1, 2, 3, 4, 5], fn(x) -> x < 3 end)
      [3,4,5]
  """
  @spec drop_while(t, (element -> as_boolean(term))) :: list
  def drop_while(collection, fun) when is_list(collection) do
    do_drop_while(collection, fun)
  end

  def drop_while(collection, fun) do
    Enumerable.reduce(collection, [], fn
      entry, []  -> if fun.(entry), do: [], else: [entry]
      entry, acc -> [entry|acc]
    end) |> :lists.reverse
  end

  @doc """
  Invokes the given `fun` for each item in the `collection`.
  Returns `:ok`. `fun` can take two parameters,
  in which case the second parameter will be the iteration index.

  ## Examples

      iex> Enum.each(["some", "example"], fn(x) -> IO.puts x end)
      "some"
      "example"
      :ok

  """
  @spec each(t, (element -> any) | (element, index -> any)) :: :ok
  def each(collection, fun) when is_list(collection) do
    cond do
      is_function(fun, 1) -> :lists.foreach(fun, collection)
      is_function(fun, 2) -> :lists.foldl(fn(h, idx) -> fun.(h, idx); idx + 1 end, 0, collection)
    end

    :ok
  end

  def each(collection, fun) do
    cond do
      is_function(fun, 1) ->
        Enumerable.reduce(collection, nil, fn(entry, _) ->
          fun.(entry)
          nil
        end)<commit_msg>Remove verbose doctest<commit_after>defprotocol Enumerable do
  @moduledoc """
  This is the protocol used by the `Enum` module.

  Usually, when you invoke a function in the module `Enum`, the first argument
  passed to it is a collection which is forwarded to this protocol in order to
  perform operations on the collection.

  For example, in the expression

      Enum.map([1, 2, 3], &1 * 2)

  `Enum.map` invokes `Enumerable.reduce` to perform the reducing operation
  that builds a mapped list by calling the mapping function `&1 * 2` on every
  element in the collection and cons'ing the element with the accumulated list.
  """

  @only [List, Record, Function]

  @doc """
  This function performs the reducing operation on a given collection. It
  returns the accumulated value of applying the given function `fun` on every
  element with the accumulated value.

  As an example, here is the implementation of `reduce` for lists:

      def reduce([h|t], acc, fun), do: reduce(t, fun.(h, acc), fun)
      def reduce([], acc, _fun),   do: acc

  As an additional example, here is the implementation of `Enum.map` with
  `Enumerable`:

      def map(collection, fun) do
        Enumerable.reduce(collection, [], fn(entry, acc) ->
          [fun.(entry)|acc]
        end) |> :lists.reverse
      end
  """
  def reduce(collection, acc, fun)

  @doc """
  The function used to check if a value exists within the collection.
  """
  def member?(collection, value)

  @doc """
  The function used to retrieve the collection's size.
  """
  def count(collection)
end

defmodule Enum do
  import Kernel, except: [max: 2, min: 2]

  @moduledoc """
  Provides a set of algorithms that enumerate over collections according to the
  `Enumerable` protocol. Most of the functions in this module have two
  flavours. If a given collection implements the mentioned protocol (like
  list, for instance), you can do:

      Enum.map([1, 2, 3], fn(x) -> x * 2 end)

  Depending on the type of the collection, the user-provided function will
  accept a certain type of argument. For dicts, the argument is always a
  `{ key, value }` tuple.
  """

  @compile :inline_list_funcs

  @type t :: Enumerable.t
  @type element :: any
  @type index :: non_neg_integer
  @type default :: any

  @doc """
  Checks if the `value` exists within the `collection`.

  ## Examples

      iex> Enum.member?(1..10, 5)
      true
      iex> Enum.member?([:a, :b, :c], :d)
      false

  """
  @spec member?(t, element) :: boolean
  def member?(collection, value) do
    Enumerable.member?(collection, value)
  end

  @doc """
  Returns the collection size.

  ## Examples

      iex> Enum.count([1, 2, 3])
      3

  """
  @spec count(t) :: non_neg_integer
  def count(collection) do
    Enumerable.count(collection)
  end

  @doc """
  Counts for how many items the function returns true.

  ## Examples
      iex> Enum.count([1, 2, 3, 4, 5], fn(x) -> rem(x, 2) == 0 end)
      2

  """
  @spec count(t, (element -> as_boolean(term))) :: non_neg_integer
  def count(collection, fun) do
    Enumerable.reduce(collection, 0, fn(entry, acc) ->
      if fun.(entry), do: acc + 1, else: acc
    end)
  end

  @doc """
  Invokes the given `fun` for each item in the `collection` and returns true if
  each invocation returns true as well, otherwise it short-circuits and returns
  false.

  ## Examples

      iex> Enum.all?([2, 4, 6], fn(x) -> rem(x, 2) == 0 end)
      true

      iex> Enum.all?([2, 3, 4], fn(x) -> rem(x, 2) == 0 end)
      false

  If no function is given, it defaults to checking if
  all items in the collection evaluate to true.

      iex> Enum.all?([1, 2, 3])
      true
      iex> Enum.all?([1, nil, 3])
      false

  """
  @spec all?(t) :: boolean
  @spec all?(t, (element -> as_boolean(term))) :: boolean

  def all?(collection, fun // fn(x) -> x end)

  def all?(collection, fun) when is_list(collection) do
    do_all?(collection, fun)
  end

  def all?(collection, fun) do
    Enumerable.reduce(collection, true, fn(entry, _) ->
      if fun.(entry), do: true, else: throw(:enum_all?)
    end)
  catch
    :enum_all? -> false
  end

  @doc """
  Invokes the given `fun` for each item in the `collection` and returns true if
  at least one invocation returns true. Returns false otherwise.

  ## Examples

      iex> Enum.any?([2, 4, 6], fn(x) -> rem(x, 2) == 1 end)
      false

      iex> Enum.any?([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
      true

  If no function is given, it defaults to checking if
  at least one item in the collection evaluates to true.

      iex> Enum.any?([false, false, false])
      false
      iex> Enum.any?([false, true, false])
      true

  """
  @spec any?(t) :: boolean
  @spec any?(t, (element -> as_boolean(term))) :: boolean

  def any?(collection, fun // fn(x) -> x end)

  def any?(collection, fun) when is_list(collection) do
    do_any?(collection, fun)
  end

  def any?(collection, fun) do
    Enumerable.reduce(collection, false, fn(entry, _) ->
      if fun.(entry), do: throw(:enum_any?), else: false
    end)
  catch
    :enum_any? -> true
  end

  @doc """
  Finds the element at the given index (zero-based).
  Raises out of bounds error in case the given position
  is outside the range of the collection.

  Expects an ordered collection.

  ## Examples

      iex> Enum.at([2, 4, 6], 0)
      2
      iex> Enum.at([2, 4, 6], 2)
      6
      iex> Enum.at([2, 4, 6], 4)
      nil
      iex> Enum.at([2, 4, 6], 4, :none)
      :none

  """
  @spec at(t, index) :: element | nil
  @spec at(t, index, default) :: element | default
  def at(collection, n, default // nil) when n >= 0 do
    case fetch(collection, n) do
      { :ok, h } -> h
      :error     -> default
    end
  end

  @doc """
  Drops the first `count` items from the collection.
  Expects an ordered collection.

  ## Examples

      iex> Enum.drop([1, 2, 3], 2)
      [3]
      iex> Enum.drop([1, 2, 3], 10)
      []
      iex> Enum.drop([1, 2, 3], 0)
      [1,2,3]

  """
  @spec drop(t, integer) :: list
  def drop(collection, count) when is_list(collection) and count >= 0 do
    do_drop(collection, count)
  end

  def drop(collection, count) when count >= 0 do
    { list, _ } = Enumerable.reduce(collection, { [], count }, fn
      _, { _, count } when count > 0 -> { [], count - 1 }
      entry, { list, count }         -> { [entry|list], count }
    end)
    :lists.reverse(list)
  end

  def drop(collection, count) when count < 0 do
    { list, count } = iterate_and_count(collection, count)
    drop(list, count)
  end

  @doc """
  Drops items at the beginning of `collection` while `fun` returns true.
  Expects an ordered collection.

  ## Examples

      iex> Enum.drop_while([1, 2, 3, 4, 5], fn(x) -> x < 3 end)
      [3,4,5]
  """
  @spec drop_while(t, (element -> as_boolean(term))) :: list
  def drop_while(collection, fun) when is_list(collection) do
    do_drop_while(collection, fun)
  end

  def drop_while(collection, fun) do
    Enumerable.reduce(collection, [], fn
      entry, []  -> if fun.(entry), do: [], else: [entry]
      entry, acc -> [entry|acc]
    end) |> :lists.reverse
  end

  @doc """
  Invokes the given `fun` for each item in the `collection`.
  Returns `:ok`. `fun` can take two parameters,
  in which case the second parameter will be the iteration index.

  ## Examples

      Enum.each(["some", "example"], fn(x) -> IO.puts x end)
      "some"
      "example"
      #=> :ok

  """
  @spec each(t, (element -> any) | (element, index -> any)) :: :ok
  def each(collection, fun) when is_list(collection) do
    cond do
      is_function(fun, 1) -> :lists.foreach(fun, collection)
      is_function(fun, 2) -> :lists.foldl(fn(h, idx) -> fun.(h, idx); idx + 1 end, 0, collection)
    end

    :ok
  end

  def each(collection, fun) do
    cond do
      is_function(fun, 1) ->
        Enumerable.reduce(collection, nil, fn(entry, _) ->
          fun.(entry)
          nil
        end)
{'fraction_non_alphanumeric': 0.13329137822529893, 'fraction_numerical': 0.013971050975456261, 'mean_word_length': 2.7434628975265016, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 123, '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': '7112494', 'n_tokens_mistral': 5465, 'n_tokens_neox': 5131, 'n_words': 1902}