From 6e3f0e6f1910c60d98ac20791d9f639fce553f07 Mon Sep 17 00:00:00 2001 From: Michel Belleville Date: Mon, 22 Feb 2016 23:26:32 +0100 Subject: [PATCH 1/5] Fixing the Many monad, respecting the specs --- spec/monads/many_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/monads/many_spec.rb b/spec/monads/many_spec.rb index bc14795..4ba2224 100644 --- a/spec/monads/many_spec.rb +++ b/spec/monads/many_spec.rb @@ -126,5 +126,31 @@ module Monads end end end + + describe "unpacking nested many values" do + let(:expected_values) do + 2.times.map do |i| + 3.times.map do |j| + 4.times.map do |k| + "#{i}#{j}#{k}" + end + end + end.flatten + end + + let(:values) do + 2.times.map do |i| + double(:"root#{i}", branches: 3.times.map do |j| + double(:"branch#{i}#{j}", leaves: 4.times.map do |k| + "#{i}#{j}#{k}" + end) + end) + end + end + + it "retrieves leaves two levels deep" do + expect(many.branches.leaves.values).to eq expected_values + end + end end end From fae57e34594bdfa8cca17f97c9547a84368fa6af Mon Sep 17 00:00:00 2001 From: Michel Belleville Date: Sun, 11 Oct 2015 21:34:16 +0200 Subject: [PATCH 2/5] Fixing the Many monad, respecting the specs --- lib/monads/many.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/monads/many.rb b/lib/monads/many.rb index f49a5b5..e914e87 100644 --- a/lib/monads/many.rb +++ b/lib/monads/many.rb @@ -13,7 +13,7 @@ def initialize(values) def and_then(&block) block = ensure_monadic_result(&block) - Many.new(values.map(&block).flat_map(&:values)) + Many.new(values.map(&block).map(&:values).flatten) end def respond_to_missing?(method_name, include_private = false) From f4f1f977aeda0968140321e87d1a888bbcc4c48f Mon Sep 17 00:00:00 2001 From: Michel Belleville Date: Wed, 24 Feb 2016 17:32:55 +0100 Subject: [PATCH 3/5] Fixing the Many monad, respecting the specs --- spec/monads/many_spec.rb | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/spec/monads/many_spec.rb b/spec/monads/many_spec.rb index 4ba2224..0e78722 100644 --- a/spec/monads/many_spec.rb +++ b/spec/monads/many_spec.rb @@ -128,25 +128,9 @@ module Monads end describe "unpacking nested many values" do - let(:expected_values) do - 2.times.map do |i| - 3.times.map do |j| - 4.times.map do |k| - "#{i}#{j}#{k}" - end - end - end.flatten - end - - let(:values) do - 2.times.map do |i| - double(:"root#{i}", branches: 3.times.map do |j| - double(:"branch#{i}#{j}", leaves: 4.times.map do |k| - "#{i}#{j}#{k}" - end) - end) - end - end + let(:expected_values) { %w(tiger lion hamster) } + let(:branches) { [double(:branch, leaves: expected_values)] } + let(:values) { [double(:root, branches: branches)] } it "retrieves leaves two levels deep" do expect(many.branches.leaves.values).to eq expected_values From 9587747b2669223f75d5b9e067f4c01d6b7343d0 Mon Sep 17 00:00:00 2001 From: Michel Belleville Date: Thu, 25 Feb 2016 14:31:17 +0100 Subject: [PATCH 4/5] Fixing the Many monad, respecting the specs --- spec/monads/many_spec.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/monads/many_spec.rb b/spec/monads/many_spec.rb index 0e78722..edb017d 100644 --- a/spec/monads/many_spec.rb +++ b/spec/monads/many_spec.rb @@ -128,13 +128,12 @@ module Monads end describe "unpacking nested many values" do - let(:expected_values) { %w(tiger lion hamster) } - let(:branches) { [double(:branch, leaves: expected_values)] } - let(:values) { [double(:root, branches: branches)] } + let(:expected_values) { %w(1984 Hamlet Macbeth) } + let(:orwel_books) { [double(:book, title: '1984')] } + let(:shakespeare_books) { [double(:book, title: 'Hamlet'), double(:book, title: 'Macbeth')] } + let(:values) { [double(:orwell, books: orwel_books), double(:shakespeare, books: shakespeare_books)] } - it "retrieves leaves two levels deep" do - expect(many.branches.leaves.values).to eq expected_values - end + it { expect(many.books.title.values).to eq expected_values } end end end From dcf1dab3c1b9088f3c9799e221979101761f3e8b Mon Sep 17 00:00:00 2001 From: Michel Belleville Date: Sun, 28 Feb 2016 21:01:06 +0100 Subject: [PATCH 5/5] Fixing the Many monad, respecting the specs --- lib/monads/many.rb | 8 ++++++-- spec/monads/many_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/monads/many.rb b/lib/monads/many.rb index e914e87..8d1af3e 100644 --- a/lib/monads/many.rb +++ b/lib/monads/many.rb @@ -13,7 +13,7 @@ def initialize(values) def and_then(&block) block = ensure_monadic_result(&block) - Many.new(values.map(&block).map(&:values).flatten) + Many.new values.map(&block).map(&:values).flatten(1) end def respond_to_missing?(method_name, include_private = false) @@ -21,7 +21,11 @@ def respond_to_missing?(method_name, include_private = false) end def self.from_value(value) - Many.new([value]) + if value.respond_to? :each + Many.new(value) + else + Many.new([value]) + end end end end diff --git a/spec/monads/many_spec.rb b/spec/monads/many_spec.rb index edb017d..a5493c0 100644 --- a/spec/monads/many_spec.rb +++ b/spec/monads/many_spec.rb @@ -135,5 +135,12 @@ module Monads it { expect(many.books.title.values).to eq expected_values } end + + describe "respecting deeply nested arrays when unpacking only a few levels" do + let(:expected_values) { [:a, [:b, :c], :d, [:e, :f]] } + let(:values) { [[1, :a], [2, [:b, :c]], [3, :d], [4, [:e, :f]]] } + + it { expect(many.slice(1, 1).values).to eq expected_values } + end end end