Created
October 2, 2016 20:12
-
-
Save mooreniemi/a893862abd63f4206d224a330e3d7723 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: Compose.c | |
* | |
* Description: Proc composition | |
* | |
* Version: 1.0 | |
* Created: 10/01/2016 16:14:22 | |
* Revision: none | |
* Compiler: gcc | |
* | |
* Author: Alex Moore-Niemi | |
* | |
* ===================================================================================== | |
*/ | |
#include <ruby.h> | |
// Defining a space for information and references about the module to be stored internally | |
VALUE Compose = Qnil; | |
// Prototype for the initialization method - Ruby calls this, not you | |
void Init_compose(); | |
VALUE compose(VALUE arg, VALUE procs) { | |
VALUE first = rb_ary_shift(procs); | |
VALUE second = rb_ary_shift(procs); | |
return rb_proc_call(second, rb_proc_call(first, arg)); | |
} | |
VALUE method_compose(VALUE self, VALUE other_proc) { | |
VALUE procs = other_proc; | |
procs = rb_ary_push(procs, self); | |
VALUE result = rb_proc_new(compose, procs); | |
FILE *f = fopen("clog.txt", "w"); | |
fprintf(f, "procs: %s\n", RSTRING_PTR(rb_ary_to_s(procs))); | |
return result; | |
} | |
// The initialization method for this module | |
void Init_compose() { | |
rb_define_method(rb_cProc, "compose", method_compose, -2); | |
rb_define_method(rb_cProc, "*", method_compose, -2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment