Repository: jtablesaw/tablesaw
Pull request #808 (2020-05-20 06:52:00)

Revert "Reduce code duplication"
This reverts commit 7516c614527e33699f9ced936f34387588c4ace0.

Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Commit 7516c614527e33699f9ced936f34387588c4ace0 changes the semantics of `map`. `map` appends while `mapInto` sets values.

In a project I have this pattern all over the place:

```java
DateTimeColumn update = t.dateTimeColumn("Last Update");
DateColumn date = update.map(d -> d.toLocalDate(), DateColumn::create);
```

the latest Tablesaw update breaks everything as it tries to set values into a column with no rows.

## Testing

None.


Patch:
diff --git a/core/src/main/java/tech/tablesaw/columns/Column.java b/core/src/main/java/tech/tablesaw/columns/Column.java
index 24f6e1ec..689a3d17 100644
--- a/core/src/main/java/tech/tablesaw/columns/Column.java
+++ b/core/src/main/java/tech/tablesaw/columns/Column.java
@@ -363,7 +363,15 @@ public interface Column<T> extends Iterable<T>, Comparator<T> {
    */
   default <R, C extends Column<R>> C map(
       Function<? super T, ? extends R> fun, Function<String, C> creator) {
-    return mapInto(fun, creator.apply(name()));
+    C into = creator.apply(name());
+    for (int i = 0; i < size(); i++) {
+      if (isMissing(i)) {
+        into.appendMissing();
+      } else {
+        into.append(fun.apply(get(i)));
+      }
+    }
+    return into;
   }
 
   Column<T> setMissing(int i);
diff --git a/jsplot/src/main/java/tech/tablesaw/plotly/components/Config.java b/jsplot/src/main/java/tech/tablesaw/plotly/components/Config.java
index 2f7a5c02..4ae413eb 100644
--- a/jsplot/src/main/java/tech/tablesaw/plotly/components/Config.java
+++ b/jsplot/src/main/java/tech/tablesaw/plotly/components/Config.java
@@ -6,9 +6,11 @@ import java.util.Map;
 public class Config extends Component {
 
   private final Boolean displayModeBar;
+  private final Boolean responsive;
 
   private Config(Builder builder) {
     this.displayModeBar = builder.displayModeBar;
+    this.responsive = builder.responsive;
   }
 
   public static Builder builder() {
@@ -17,19 +19,26 @@ public class Config extends Component {
 
   @Override
   public String asJavascript() {
-    return asJavascript("config_template.html");
+    return "var config = " + asJSON();
+  }
+
+  @Override
+  protected Map<String, Object> getJSONContext() {
+    return getContext();
   }
 
   @Override
   protected Map<String, Object> getContext() {
     Map<String, Object> context = new HashMap<>();
     context.put("displayModeBar", displayModeBar);
+    context.put("responsive", responsive);
     return context;
   }
 
   public static class Builder {
 
     Boolean displayModeBar;
+    Boolean responsive;
 
     private Builder() {}
 
@@ -38,6 +47,11 @@ public class Config extends Component {
       return this;
     }
 
+    public Builder responsive(boolean responsive) {
+      this.responsive = responsive;
+      return this;
+    }
+
     public Config build() {
       return new Config(this);
     }
diff --git a/jsplot/src/main/resources/config_template.html b/jsplot/src/main/resources/config_template.html
deleted file mode 100644
index 52564f2a..00000000
--- a/jsplot/src/main/resources/config_template.html
+++ /dev/null
@@ -1,5 +0,0 @@
-var config = {
-{% if displayModeBar is not null %}
-    displayModeBar: {{displayModeBar}}
-{% endif %}
-}
\ No newline at end of file


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/columns/ColumnTest.java b/core/src/test/java/tech/tablesaw/columns/ColumnTest.java
index 195fe731..dff370aa 100644
--- a/core/src/test/java/tech/tablesaw/columns/ColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/columns/ColumnTest.java
@@ -293,7 +293,7 @@ public class ColumnTest {
   public void testMap2() {
     StringColumn c =
         DoubleColumn.create("t1", new double[] {-1, 0, 1})
-            .map(String::valueOf, name -> StringColumn.create(name, 3));
+            .map(String::valueOf, StringColumn::create);
     assertContentEquals(c, "-1.0", "0.0", "1.0");
   }
 
diff --git a/jsplot/src/test/java/tech/tablesaw/plotly/components/ConfigTest.java b/jsplot/src/test/java/tech/tablesaw/plotly/components/ConfigTest.java
index 535db5fd..817acfd6 100644
--- a/jsplot/src/test/java/tech/tablesaw/plotly/components/ConfigTest.java
+++ b/jsplot/src/test/java/tech/tablesaw/plotly/components/ConfigTest.java
@@ -9,17 +9,33 @@ public class ConfigTest {
 
   @Test
   public void testJavascript() {
+    {
+      Config config = Config.builder().build();
+      assertTrue(config.asJavascript().startsWith("var config"));
+    }
     {
       Config config = Config.builder().displayModeBar(true).build();
-      assertTrue(config.asJavascript().contains("displayModeBar: true"));
+      assertTrue(config.asJavascript().contains("\"displayModeBar\" : true"));
     }
     {
       Config config = Config.builder().displayModeBar(false).build();
-      assertTrue(config.asJavascript().contains("displayModeBar: false"));
+      assertTrue(config.asJavascript().contains("\"displayModeBar\" : false"));
     }
     {
       Config config = Config.builder().build();
       assertFalse(config.asJavascript().contains("displayModeBar"));
     }
+    {
+      Config config = Config.builder().responsive(true).build();
+      assertTrue(config.asJavascript().contains("\"responsive\" : true"));
+    }
+    {
+      Config config = Config.builder().responsive(false).build();
+      assertTrue(config.asJavascript().contains("\"responsive\" : false"));
+    }
+    {
+      Config config = Config.builder().build();
+      assertFalse(config.asJavascript().contains("responsive"));
+    }
   }
 }