summaryrefslogtreecommitdiff
path: root/operators.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-14 01:06:14 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-14 01:06:14 +0000
commit396543790dc1c844c726b77a95c6180978232abd (patch)
treeb5051f5afba137e499b883b637ce6fc436805ec5 /operators.c
parent07082593ab4abfbf9a3dd6729cb2e548ec303115 (diff)
Add each (¨) operator
Diffstat (limited to 'operators.c')
-rw-r--r--operators.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/operators.c b/operators.c
index ba999bd..226f27b 100644
--- a/operators.c
+++ b/operators.c
@@ -8,7 +8,7 @@ Rune primmonopnames[] = L"¨⍨⌸⌶&";
Rune primdyadopnames[] = L"⍣.∘⍤⍥@⍠⌺";
opmonad monadoperatordefs[] = {
- 0, /* ¨ */
+ opEach, /* ¨ */
opSwitch, /* ⍨ */
0, /* ⌸ */
0, /* ⌶ */
@@ -28,6 +28,41 @@ opdyad dyadoperatordefs[] = {
/* Monadic operators */
Array *
+opEach(Datum *lefto, Array *left, Array *right)
+{
+ int i;
+ if(lefto->tag != FunctionTag)
+ return nil;
+ Array *leftarr;
+ Array *rightarr;
+ if(left){
+
+ int rankok = scalarextend(left, right, &leftarr, &rightarr);
+ if(!rankok){
+ print("Ranks don't match lol\n");
+ exits(nil);
+ }
+ }else{
+ leftarr = nil;
+ rightarr = fnSame(right);
+ }
+
+ Array *result = allocarray(AtypeArray, rightarr->rank, rightarr->size);
+ for(i = 0; i < rightarr->rank; i++)
+ result->shape[i] = rightarr->shape[i];
+ for(i = 0; i < rightarr->size; i++){
+ Array *elem1 = leftarr ? arrayitem(leftarr, i) : nil;
+ Array *elem2 = arrayitem(rightarr, i);
+ result->arraydata[i] = runfunc(lefto->func, elem1, elem2);
+ freearray(elem1);
+ freearray(elem2);
+ }
+ freearray(leftarr);
+ freearray(rightarr);
+ return result;
+}
+
+Array *
opSwitch(Datum *lefto, Array *left, Array *right)
{
if(lefto->tag == ArrayTag){