From 9651d4521a88b274f92add5da3f1b66555d3b99a Mon Sep 17 00:00:00 2001 From: Hector Marin Date: Mon, 6 May 2019 23:51:29 -0500 Subject: [PATCH 1/4] added map to vector method to Daru::Vector to avoid doing breaking changes --- lib/daru/vector.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/daru/vector.rb b/lib/daru/vector.rb index 827800216..31b1818ed 100644 --- a/lib/daru/vector.rb +++ b/lib/daru/vector.rb @@ -116,6 +116,18 @@ def each_with_index &block self end + def map_to_vector(&block) + return to_enum(:map) unless block_given? + @data.map!(&block) + self + end + + def map_to_vector!(&block) + return to_enum(:map!) unless block_given? + @data.map!(&block) + self + end + def map!(&block) return to_enum(:map!) unless block_given? @data.map!(&block) From dab59c38c3110627dbf21ef74893e9c305af5bd5 Mon Sep 17 00:00:00 2001 From: Hector Marin Date: Mon, 6 May 2019 23:51:51 -0500 Subject: [PATCH 2/4] added test cases for map_to_vector --- spec/vector_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/vector_spec.rb b/spec/vector_spec.rb index 3206ee3a1..984b7c8d1 100644 --- a/spec/vector_spec.rb +++ b/spec/vector_spec.rb @@ -1334,6 +1334,29 @@ end end + context "#map_to_vector" do + it "maps to vector" do + a = @common_all_dtypes.map_to_vector { |v| v } + expect(a).to eq(Daru::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99])) + end + it "maps to vector with change" do + a = @common_all_dtypes.map_to_vector { |v| v + 1} + expect(a).to eq(Daru::Vector.new([6, 6, 6, 6, 6, 7, 7, 8, 9, 10, 11, 2, 3, 4, 5, 12, -98, -98])) + end + end + + context "#map_to_vector!" do + it "maps to vector destructive" do + @common_all_dtypes.map_to_vector! { |v| v } + expect(@common_all_dtypes).to eq(Daru::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99])) + end + + it "maps to vector destructive" do + @common_all_dtypes.map_to_vector! { |v| v + 1} + expect(@common_all_dtypes).to eq(Daru::Vector.new([6, 6, 6, 6, 6, 7, 7, 8, 9, 10, 11, 2, 3, 4, 5, 12, -98, -98])) + end + end + context "#recode" do it "maps and returns a vector of dtype of self by default" do a = @common_all_dtypes.recode { |v| v == -99 ? 1 : 0 } From 8292ba1a59d317154dbe0e691eb5019c07270f50 Mon Sep 17 00:00:00 2001 From: Hector Marin Date: Tue, 7 May 2019 00:07:02 -0500 Subject: [PATCH 3/4] fixed code smell of trailing whitespace --- lib/daru/vector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/daru/vector.rb b/lib/daru/vector.rb index 31b1818ed..b1e95985b 100644 --- a/lib/daru/vector.rb +++ b/lib/daru/vector.rb @@ -127,7 +127,7 @@ def map_to_vector!(&block) @data.map!(&block) self end - + def map!(&block) return to_enum(:map!) unless block_given? @data.map!(&block) From c5dc623ed19e8cba26b7174b08e4716c2a6fa429 Mon Sep 17 00:00:00 2001 From: Hector Marin Date: Tue, 7 May 2019 00:09:15 -0500 Subject: [PATCH 4/4] fixed trailing space --- spec/vector_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/vector_spec.rb b/spec/vector_spec.rb index 984b7c8d1..d2723b84c 100644 --- a/spec/vector_spec.rb +++ b/spec/vector_spec.rb @@ -1339,6 +1339,7 @@ a = @common_all_dtypes.map_to_vector { |v| v } expect(a).to eq(Daru::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99])) end + it "maps to vector with change" do a = @common_all_dtypes.map_to_vector { |v| v + 1} expect(a).to eq(Daru::Vector.new([6, 6, 6, 6, 6, 7, 7, 8, 9, 10, 11, 2, 3, 4, 5, 12, -98, -98])) @@ -1350,7 +1351,7 @@ @common_all_dtypes.map_to_vector! { |v| v } expect(@common_all_dtypes).to eq(Daru::Vector.new([5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99])) end - + it "maps to vector destructive" do @common_all_dtypes.map_to_vector! { |v| v + 1} expect(@common_all_dtypes).to eq(Daru::Vector.new([6, 6, 6, 6, 6, 7, 7, 8, 9, 10, 11, 2, 3, 4, 5, 12, -98, -98]))