+struct casereader_append_numeric
+{
+ struct caseproto *proto;
+ casenumber n;
+ new_value_func *func;
+ void *aux;
+ void (*destroy) (void *aux);
+};
+
+static bool can_destroy (void *can_);
+
+static struct ccase *can_translate (struct ccase *, void *can_);
+
+/* Creates and returns a new casereader whose cases are produced
+ by reading from SUBREADER and appending an additional value,
+ generated by FUNC. AUX is an optional parameter which
+ gets passed to FUNC. FUNC will also receive N as it, which is
+ the ordinal number of the case in the reader. DESTROY is an
+ optional parameter used to destroy AUX.
+
+ After this function is called, SUBREADER must not ever again
+ be referenced directly. It will be destroyed automatically
+ when the translating casereader is destroyed. */
+struct casereader *
+casereader_create_append_numeric (struct casereader *subreader,
+ new_value_func func, void *aux,
+ void (*destroy) (void *aux))
+{
+ struct casereader_append_numeric *can = xmalloc (sizeof *can);
+ can->proto = caseproto_ref (casereader_get_proto (subreader));
+ can->proto = caseproto_add_width (can->proto, 0);
+ can->n = 0;
+ can->aux = aux;
+ can->func = func;
+ can->destroy = destroy;
+ return casereader_create_translator (subreader, can->proto,
+ can_translate, can_destroy, can);
+}
+
+
+static struct ccase *
+can_translate (struct ccase *c, void *can_)
+{
+ struct casereader_append_numeric *can = can_;
+ double new_value = can->func (c, can->n++, can->aux);
+ c = case_unshare_and_resize (c, can->proto);
+ case_data_rw_idx (c, caseproto_get_n_widths (can->proto) - 1)->f = new_value;
+ return c;
+}
+
+static bool
+can_destroy (void *can_)
+{
+ struct casereader_append_numeric *can = can_;
+ if (can->destroy)
+ can->destroy (can->aux);
+ caseproto_unref (can->proto);
+ free (can);
+ return true;
+}
+
+\f
+
+struct arithmetic_sequence
+{
+ double first;
+ double increment;
+};
+
+static double
+next_arithmetic (const struct ccase *c UNUSED,
+ casenumber n,
+ void *aux)
+{
+ struct arithmetic_sequence *as = aux;
+ return n * as->increment + as->first;
+}