withCategoricColumnFromTemplate method

DataFrame withCategoricColumnFromTemplate({
  1. required String name,
  2. required String template,
  3. String startQuote = '{',
  4. String endQuote = '}',
  5. String generator(
    1. String
    )?,
})

A new data frame with a categoric column generated from a template.

Example:

print(petals.withHead(5));
print(petals.withHead(5).withCategoricColumnFromTemplate(
  name: "marker",
  template: "{species}-{id}",
));

.--.------------.-----------.------------.
|id|petal_length|petal_width|species     |
:--+------------+-----------+------------:
|1 |1.4         |0.2        |setosa      |
|2 |1.4         |0.2        |setosa      |
|3 |1.3         |0.2        |setosa      |
|4 |1.5         |0.2        |setosa      |
|51|4.7         |1.4        |versicolor  |
'--'------------'-----------'------------'

.--.------------.-----------.------------.---------------.
|id|petal_length|petal_width|species     |marker         |
:--+------------+-----------+------------+---------------:
|1 |1.4         |0.2        |setosa      |setosa-1       |
|2 |1.4         |0.2        |setosa      |setosa-2       |
|3 |1.3         |0.2        |setosa      |setosa-3       |
|4 |1.5         |0.2        |setosa      |setosa-4       |
|51|4.7         |1.4        |versicolor  |versicolor-51  |
'--'------------'-----------'------------'---------------'

Implementation

DataFrame withCategoricColumnFromTemplate({
  required String name,
  required String template,
  String startQuote = '{',
  String endQuote = '}',
  String Function(String)? generator,
}) {
  if (!_columnNameOkay(name)) {
    throw PackhorseError.badColumnName(name);
  }
  generator = generator ?? (x) => x;
  return copy
    ..categoricColumns[name] = CategoricColumn(
      _templateResults(
        template,
        startQuote: startQuote,
        endQuote: endQuote,
      ).map(generator),
    );
}