Avatar of paulfioravanti

paulfioravanti's solution

to List Ops in the Ruby Track

Published at May 12 2019 · 0 comments
Instructions
Test suite
Solution

Implement basic list operations.

In functional languages list operations like length, map, and reduce are very common. Implement a series of basic list operations, without using existing functions.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby list_ops_test.rb

To include color from the command line:

ruby -r minitest/pride list_ops_test.rb

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

list_ops_test.rb

require 'minitest/autorun'
require_relative 'list_ops'

class ListOpsTest < Minitest::Test
  def test_count_empty
    assert_equal 0, ListOps.arrays([])
  end

  def test_count_normal
    skip
    assert_equal 5, ListOps.arrays(Array.new(5))
  end

  def test_count_gigantic
    skip
    assert_equal 1_000_000, ListOps.arrays(Array.new(1_000_000))
  end

  def test_reverse_empty
    skip
    assert_equal [], ListOps.reverser([])
  end

  def test_reverse_normal
    skip
    assert_equal [5, 4, 3, 2, 1], ListOps.reverser([1, 2, 3, 4, 5])
  end

  def test_reverse_gigantic
    skip
    expected = (1..1_000_000).to_a.reverse
    assert_equal expected, ListOps.reverser((1..1_000_000).to_a)
  end

  def test_concat_empty
    skip
    assert_equal [], ListOps.concatter([], [])
  end

  def test_concat_normal
    skip
    assert_equal [12, 34, 56, 78], ListOps.concatter([12, 34], [56, 78])
  end

  def test_concat_gigantic
    skip
    input1 = (1..1_000_000).to_a
    input2 = (1_000_001..2_000_000).to_a
    assert_equal (1..2_000_000).to_a, ListOps.concatter(input1, input2)
  end

  def test_mapper_empty
    skip
    assert_equal [], ListOps.mapper([])
  end

  def test_mapper_normal
    skip
    assert_equal [2, 3, 4, 5, 6], ListOps.mapper([1, 2, 3, 4, 5]) { |n| n + 1 }
  end

  def test_mapper_gigantic
    skip
    result = ListOps.mapper((1..1_000_000).to_a) { |n| n + 1 }
    assert_equal (2..1_000_001).to_a, result
  end

  def test_filterer_empty
    skip
    assert_equal [], ListOps.filterer([])
  end

  def test_filterer_normal
    skip
    result = ListOps.filterer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], &:odd?)
    assert_equal [1, 3, 5, 7, 9], result
  end

  def test_filterer_gigantic
    skip
    result = ListOps.filterer((1..10_000).to_a, &:even?)
    assert_equal (1..10_000).to_a.select(&:even?), result
  end

  def test_sum_reducer_empty
    skip
    assert_equal 0, ListOps.sum_reducer([])
  end

  def test_sum_reducer_normal
    skip
    assert_equal 55, ListOps.sum_reducer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  end

  def test_factorial_reducer_empty
    skip
    assert_equal 1, ListOps.factorial_reducer([])
  end

  def test_factorial_reducer_normal
    skip
    input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    assert_equal 3_628_800, ListOps.factorial_reducer(input)
  end
end
module ListOps
  INCREMENT = 1
  private_constant :INCREMENT
  INITIAL_ARRAY = [].freeze
  private_constant :INITIAL_ARRAY
  INITIAL_FACTORIAL = 1
  private_constant :INITIAL_FACTORIAL
  ZERO = 0
  private_constant :ZERO

  module_function

  def arrays(array)
    reducer(
      array,
      ZERO,
      ->(accumulator, _element) { accumulator + INCREMENT }
    )
  end

  def reverser(array)
    reducer(
      array,
      INITIAL_ARRAY.dup,
      ->(accumulator, element) { accumulator.prepend(element) }
    )
  end

  def concatter(array1, array2)
    reducer(
      array2,
      array1,
      ->(accumulator, element) { accumulator << element }
    )
  end

  def mapper(array)
    return array unless block_given?

    reducer(
      array,
      INITIAL_ARRAY.dup,
      ->(accumulator, element) { accumulator << yield(element) }
    )
  end

  def filterer(array)
    return array unless block_given?

    reducer(
      array,
      INITIAL_ARRAY.dup,
      lambda do |accumulator, element|
        accumulator.tap { |acc| acc << element if yield(element) }
      end
    )
  end

  def sum_reducer(array)
    reducer(
      array,
      ZERO,
      ->(accumulator, element) { accumulator + element }
    )
  end

  def factorial_reducer(array)
    reducer(
      array,
      INITIAL_FACTORIAL,
      ->(accumulator, element) { accumulator * element }
    )
  end

  # rubocop:disable Style/For
  def reducer(collection, accumulator, function)
    for element in collection
      accumulator = function.call(accumulator, element)
    end
    accumulator
  end
  # rubocop:enable Style/For
  private_class_method :reducer
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?