Permalink
Browse files

* core/number.c: the `times` method.

  • Loading branch information...
_why
_why committed Jun 25, 2009
1 parent bf4a4c0 commit ddbd6ed62037203abae4d5062ea0254eede3f242
Showing with 27 additions and 0 deletions.
  1. +8 −0 core/number.c
  2. +9 −0 doc/start.textile
  3. +5 −0 test/numbers/times.pn
  4. +5 −0 test/tables/each.pn
View
@@ -61,6 +61,13 @@ static PN potion_num_string(Potion *P, PN closure, PN self) {
return str;
}
+static PN potion_num_times(Potion *P, PN cl, PN self, PN block) {
+ int i, j = PN_INT(self);
+ for (i = 0; i < j; i++) {
+ PN_CLOSURE(block)->method(P, block, self, PN_NUM(i));
+ }
+}
+
PN potion_decimal(Potion *P, int len, int intg, char *str) {
int i, rlen = intg + PN_PREC;
vPN(Decimal) n = PN_ALLOC_N(PN_TNUMBER, struct PNDecimal, sizeof(PN) * rlen);
@@ -87,4 +94,5 @@ void potion_num_init(Potion *P) {
potion_method(num_vt, "/", potion_div, "value=N");
potion_method(num_vt, "number", potion_num_number, 0);
potion_method(num_vt, "string", potion_num_string, 0);
+ potion_method(num_vt, "times", potion_num_times, 0);
}
View
@@ -79,6 +79,15 @@ This one illustrates a bit better how tables get used as argument lists. We have
Here's a case where a list is being called as a function. Yes, everything is a function! We could also have called: @foods (index=2)@.
+Functions can also be attached to methods, for use as anonymous blocks (as in Ruby.)
+
+<pre>
+<code>
+ (dog="canine", cat="feline", fox="vulpine") each (key, val):
+ (key, " is a ", val) join print.
+</code>
+</pre>
+
h2. The Object-Oriented Side
<pre>
View
@@ -0,0 +1,5 @@
+5 times: "Odelay" print.
+
+10 times (i): i string print.
+
+# OdelayOdelayOdelayOdelayOdelay0123456789nil
View
@@ -0,0 +1,5 @@
+("cheese", "bread", "mayo") each (food):
+ food length string print.
+0
+
+# 6540

0 comments on commit ddbd6ed

Please sign in to comment.