static int exec_seq(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { strm_int start=1, end=-1, inc=1; struct seq_data* d; switch (argc) { case 0: break; case 1: end = strm_value_int(args[0]); break; case 2: start = strm_value_int(args[0]); end = strm_value_int(args[1]); break; case 3: start = strm_value_int(args[0]); inc = strm_value_int(args[1]); end = strm_value_int(args[2]); break; default: strm_raise(strm, "wrong number of arguments"); return STRM_NG; } d = malloc(sizeof(struct seq_data)); d->n = start; d->inc = inc; d->end = end; *ret = strm_stream_value(strm_stream_new(strm_producer, gen_seq, NULL, (void*)d)); return STRM_OK; }
static int exec_seq(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { double start=1, end=-1, inc=1, tmp; struct seq_data* d; strm_get_args(strm, argc, args, "|fff", &start, &end, &tmp); switch (argc) { case 1: end = start; start = 1; break; case 3: inc = end; end = tmp; break; default: break; } d = malloc(sizeof(*d)); d->n = start; d->inc = inc; d->end = end; *ret = strm_stream_value(strm_stream_new(strm_producer, gen_seq, NULL, (void*)d)); return STRM_OK; }
static int exec_filter(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct map_data* d = malloc(sizeof(*d)); strm_get_args(strm, argc, args, "v", &d->func); *ret = strm_stream_value(strm_stream_new(strm_filter, iter_filter, NULL, (void*)d)); return STRM_OK; }
static int exec_count(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct count_data* d; strm_get_args(strm, argc, args, ""); d = malloc(sizeof(*d)); d->count = 0; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_count, count_finish, (void*)d)); return STRM_OK; }
static int exec_var_stdev(strm_stream* strm, int argc, strm_value* args, strm_value* ret, int stdev) { struct stdev_data* d; strm_value func; strm_get_args(strm, argc, args, "|v", &func); d = malloc(sizeof(struct stdev_data)); if (!d) return STRM_NG; d->num = 0; d->s1 = d->s2 = 0.0; if (argc == 0) { *ret = strm_stream_value(strm_stream_new(strm_filter, iter_stdev, stdev ? stdev_finish : var_finish, (void*)d)); } else { d->func = func; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_stdevf, stdev ? stdev_finish : var_finish, (void*)d)); } return STRM_OK; }
static int exec_flatmap(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct map_data* d; strm_value func; strm_get_args(strm, argc, args, "v", &func); d = malloc(sizeof(*d)); if (!d) return STRM_NG; d->func = func; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_flatmap, NULL, (void*)d)); return STRM_OK; }
static int exec_correl(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct correl_data* d; strm_get_args(strm, argc, args, ""); d = malloc(sizeof(struct correl_data)); if (!d) return STRM_NG; d->n = 0; d->sx = d->sy = d->sxx = d->syy = d->sxy = 0; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_correl, correl_finish, (void*)d)); return STRM_OK; }
static int exec_sample(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct sample_data* d; strm_int len; strm_get_args(strm, argc, args, "i", &len); d = malloc(sizeof(struct sample_data)+sizeof(strm_value)*len); if (!d) return STRM_NG; d->len = len; d->i = 0; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_sample, finish_sample, (void*)d)); return STRM_OK; }
static int exec_sum_avg(strm_stream* strm, int argc, strm_value* args, strm_value* ret, int avg) { struct sum_data* d; strm_value func; strm_get_args(strm, argc, args, "|v", &func); d = malloc(sizeof(struct sum_data)); if (!d) return STRM_NG; d->sum = 0; d->c = 0; d->num = 0; if (argc == 0) { d->func = strm_nil_value(); *ret = strm_stream_value(strm_stream_new(strm_filter, iter_sum, avg ? avg_finish : sum_finish, (void*)d)); } else { d->func = func; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_sumf, avg ? avg_finish : sum_finish, (void*)d)); } return STRM_OK; }
static int csv(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { strm_stream *t; struct csv_data *cd = malloc(sizeof(struct csv_data)); if (!cd) return STRM_NG; cd->headers = strm_ary_null; cd->types = NULL; cd->prev = strm_str_null; cd->n = 0; t = strm_stream_new(strm_filter, csv_accept, csv_finish, (void*)cd); *ret = strm_stream_value(t); return STRM_OK; }
static int exec_rbk(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct rbk_data *d; khash_t(rbk) *t; strm_value func; strm_get_args(strm, argc, args, "v", &func); t = kh_init(rbk); if (!t) return STRM_NG; d = malloc(sizeof(*d)); d->tbl = t; d->func = func; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_rbk, rbk_finish, (void*)d)); return STRM_OK; }
static int exec_uniq(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct uniq_data* d; strm_value func = strm_nil_value(); strm_get_args(strm, argc, args, "|v", &func); d = malloc(sizeof(*d)); if (!d) return STRM_NG; d->last = strm_nil_value(); d->func = func; d->init = FALSE; *ret = strm_stream_value(strm_stream_new(strm_filter, strm_nil_p(func) ? iter_uniq : iter_uniqf, NULL, (void*)d)); return STRM_OK; }
static int exec_minmax(strm_stream* strm, int argc, strm_value* args, strm_value* ret, int min) { struct minmax_data* d; strm_value func = strm_nil_value(); strm_get_args(strm, argc, args, "|v", &func); d = malloc(sizeof(*d)); if (!d) return STRM_NG; d->start = TRUE; d->min = min; d->num = 0; d->data = strm_nil_value(); d->func = func; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_minmax, minmax_finish, (void*)d)); return STRM_OK; }
static int exec_drop(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct take_data* d; strm_int n; strm_get_args(strm, argc, args, "i", &n); if (n < 0) { strm_raise(strm, "negative iteration"); return STRM_NG; } d = malloc(sizeof(*d)); if (!d) return STRM_NG; d->n = n; *ret = strm_stream_value(strm_stream_new(strm_filter, iter_drop, NULL, (void*)d)); return STRM_OK; }
static int exec_repeat(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { strm_value v; strm_int n = -1; struct repeat_data *d; strm_get_args(strm, argc, args, "v|i", &v, &n); if (argc == 2 && n <= 0) { strm_raise(strm, "invalid count number"); return STRM_NG; } d = malloc(sizeof(*d)); d->v = v; d->count = n; *ret = strm_stream_value(strm_stream_new(strm_producer, gen_repeat, fin_repeat, (void*)d)); return STRM_OK; }
static int exec_cycle(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { strm_array a; strm_int n = -1; struct cycle_data *d; strm_get_args(strm, argc, args, "A|i", &a, &n); if (argc == 2 && n <= 0) { strm_raise(strm, "invalid count number"); return STRM_NG; } d = malloc(sizeof(*d)); d->ary = a; d->count = n; *ret = strm_stream_value(strm_stream_new(strm_producer, gen_cycle, fin_cycle, (void*)d)); return STRM_OK; }
static int exec_consec(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct slice_data* d; strm_int n; strm_get_args(strm, argc, args, "i", &n); d = malloc(sizeof(*d)); if (!d) return STRM_NG; d->n = n; d->i = 0; d->buf = malloc(n*sizeof(strm_value)); if (!d->buf) { free(d); return STRM_NG; } *ret = strm_stream_value(strm_stream_new(strm_filter, iter_consec, finish_consec, (void*)d)); return STRM_OK; }
static int exec_reduce(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct reduce_data* d; strm_value v1, v2; strm_get_args(strm, argc, args, "v|v", &v1, &v2); d = malloc(sizeof(*d)); if (!d) return STRM_NG; if (argc == 2) { d->init = TRUE; d->acc = v1; d->func = v2; } else { d->init = FALSE; d->acc = strm_nil_value(); d->func = v1; } *ret = strm_stream_value(strm_stream_new(strm_filter, iter_reduce, reduce_finish, (void*)d)); return STRM_OK; }
static int exec_bgraph(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { struct bar_data* d; char* title = NULL; strm_int tlen = 0; strm_get_args(strm, argc, args, "|s", &title, &tlen); d = malloc(sizeof(struct bar_data)); if (!d) return STRM_NG; d->title = malloc(tlen); memcpy((void*)d->title, title, tlen); d->tlen = tlen; if (refcnt == 0) { strm_atomic_inc(refcnt); strm_signal(SIGWINCH, sigupdate, &winch); strm_signal(SIGINT, sigupdate, &interrupt); } if (init_bar(d) == STRM_NG) return STRM_NG; *ret = strm_stream_value(strm_stream_new(strm_consumer, iter_bar, fin_bar, (void*)d)); return STRM_OK; }
static int exec_bar(strm_stream* strm, int argc, strm_value* args, strm_value* ret) { strm_value lhs, rhs; assert(argc == 2); /* int x int */ if (strm_int_p(args[0]) && strm_int_p(args[1])) { *ret = strm_int_value(strm_value_int(args[0])|strm_value_int(args[1])); return STRM_OK; } lhs = args[0]; /* lhs: io */ if (strm_io_p(lhs)) { lhs = strm_stream_value(strm_io_stream(lhs, STRM_IO_READ)); } /* lhs: lambda */ else if (strm_lambda_p(lhs)) { strm_lambda lmbd = strm_value_lambda(lhs); lhs = strm_stream_value(strm_stream_new(strm_filter, blk_exec, NULL, (void*)lmbd)); } /* lhs: array */ else if (strm_array_p(lhs)) { struct array_data *arrd = malloc(sizeof(struct array_data)); arrd->arr = strm_value_ary(lhs); arrd->n = 0; lhs = strm_stream_value(strm_stream_new(strm_producer, arr_exec, NULL, (void*)arrd)); } /* lhs: should be stream */ rhs = args[1]; /* rhs: io */ if (strm_io_p(rhs)) { rhs = strm_stream_value(strm_io_stream(rhs, STRM_IO_WRITE)); } /* rhs: lambda */ else if (strm_lambda_p(rhs)) { strm_lambda lmbd = strm_value_lambda(rhs); rhs = strm_stream_value(strm_stream_new(strm_filter, blk_exec, NULL, (void*)lmbd)); } /* rhs: cfunc */ else if (strm_cfunc_p(rhs)) { strm_cfunc func = strm_value_cfunc(rhs); rhs = strm_stream_value(strm_stream_new(strm_filter, cfunc_exec, NULL, func)); } /* stream x stream */ if (strm_stream_p(lhs) && strm_stream_p(rhs)) { strm_stream* lstrm = strm_value_stream(lhs); strm_stream* rstrm = strm_value_stream(rhs); if (lstrm == NULL || rstrm == NULL || lstrm->mode == strm_consumer || rstrm->mode == strm_producer) { strm_raise(strm, "stream error"); return STRM_NG; } strm_stream_connect(strm_value_stream(lhs), strm_value_stream(rhs)); *ret = rhs; return STRM_OK; } strm_raise(strm, "type error"); return STRM_NG; }