Example #1
0
static VALUE
lazy_drop_while(VALUE obj)
{
    NODE *memo;

    memo = NEW_MEMO(0, 0, FALSE);
    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
					 lazy_drop_while_func, (VALUE) memo),
			   Qnil);
}
Example #2
0
/*
 * call-seq:
 *   e.with_index(offset = 0) {|(*args), idx| ... }
 *   e.with_index(offset = 0)
 *
 * Iterates the given block for each element with an index, which
 * starts from +offset+.  If no block is given, returns a new Enumerator
 * that includes the index, starting from +offset+
 *
 * +offset+:: the starting index to use
 *
 */
static VALUE
enumerator_with_index(int argc, VALUE *argv, VALUE obj)
{
    VALUE memo;

    rb_scan_args(argc, argv, "01", &memo);
    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_size);
    if (NIL_P(memo))
	memo = INT2FIX(0);
    else
	memo = rb_to_int(memo);
    return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)NEW_MEMO(memo, 0, 0));
}
Example #3
0
static int
register_init_ext(st_data_t *key, st_data_t *value, st_data_t init, int existing)
{
    const char *name = (char *)*key;
    if (existing) {
	/* already registered */
	rb_warn("%s is already registered", name);
    }
    else {
	*value = (st_data_t)NEW_MEMO(init, 0, 0);
	*key = (st_data_t)ruby_strdup(name);
    }
    return ST_CONTINUE;
}
Example #4
0
static VALUE
lazy_drop(VALUE obj, VALUE n)
{
    NODE *memo;
    long len = NUM2LONG(n);

    if (len < 0) {
	rb_raise(rb_eArgError, "attempt to drop negative size");
    }
    memo = NEW_MEMO(0, 0, len);
    return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
					 lazy_drop_func, (VALUE) memo),
			   rb_ary_new3(1, n));
}
Example #5
0
static VALUE
lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
{
    VALUE result = rb_yield_values2(argc - 1, &argv[1]);
    if (TYPE(result) == T_ARRAY) {
	long i;
	for (i = 0; i < RARRAY_LEN(result); i++) {
	    rb_funcall(argv[0], id_yield, 1, RARRAY_PTR(result)[i]);
	}
    }
    else {
	NODE *memo;
	memo = NEW_MEMO(result, argv[0], 0);
	rb_rescue2(lazy_flat_map_each, (VALUE) memo,
		   lazy_flat_map_to_ary, (VALUE) memo,
		   rb_eNoMethodError, (VALUE)0);
    }
    return Qnil;
}
Example #6
0
static VALUE
lazy_take(VALUE obj, VALUE n)
{
    NODE *memo;
    long len = NUM2LONG(n);
    int argc = 1;
    VALUE argv[3];

    if (len < 0) {
	rb_raise(rb_eArgError, "attempt to take negative size");
    }
    argv[0] = obj;
    if (len == 0) {
	argv[1] = sym_cycle;
	argv[2] = INT2NUM(0);
	argc = 3;
    }
    memo = NEW_MEMO(0, 0, len);
    return lazy_set_method(rb_block_call(rb_cLazy, id_new, argc, argv,
					 lazy_take_func, (VALUE) memo),
			   rb_ary_new3(1, n));
}