diff options
author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-29 23:50:30 +0000 |
---|---|---|
committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-29 23:50:30 +0000 |
commit | 3ccc3ac8b0cc090f5315b23effd4ce6c8cad783d (patch) | |
tree | 3dbf07ea2f6897e80fce694d3e35600be5bf2bfa | |
parent | 6b775194811823b84e2694c20c036506d5855215 (diff) |
Add monadic format ⍕. It doesn't work for nested arrays yet.
-rw-r--r-- | apl9.h | 1 | ||||
-rw-r--r-- | functions.c | 42 |
2 files changed, 42 insertions, 1 deletions
@@ -318,6 +318,7 @@ Array *fnReverseLast(Array *); Array *fnReverseFirst(Array *); Array *fnTranspose(Array *); Array *fnExecute(Array *); +Array *fnFormat(Array *); Array *fnSelfReference1(Array *); /* Dyadic functions from function.c */ diff --git a/functions.c b/functions.c index 0f40a9a..a0090a9 100644 --- a/functions.c +++ b/functions.c @@ -58,7 +58,7 @@ fnmonad monadfunctiondefs[] = { fnReverseFirst, /* ⊖ */ fnTranspose, /* ⍉ */ fnExecute, /* ⍎ */ - 0, /* ⍕ */ + fnFormat, /* ⍕ */ fnSelfReference1, /* ∇ */ }; @@ -841,6 +841,46 @@ fnExecute(Array *right) } Array * +fnFormat(Array *right) +{ + if(right->type == AtypeArray) + throwerror(L"Format for nested arrays", ENotImplemented); + /* TODO add support for formatting nested arrays */ + + right = right->rank == 0 ? fnRavel(right) : fnSame(right); + + Rune *str = pparray(right); + int maxwidth = 0; + int tmpwidth = 0; + for(int i = 0; str[i] != 0; i++){ + if(str[i] != '\n') + tmpwidth++; + if(str[i] == '\n' || str[i+1] == 0){ + if(tmpwidth > maxwidth) + maxwidth = tmpwidth; + tmpwidth = 0; + } + } + vlong size; + if(right->shape[right->rank-1] == 0) + size = 0; + else + size = maxwidth * right->size / right->shape[right->rank-1]; + + Array *result = allocarray(AtypeRune, right->rank, size); + result->shape[result->rank-1] = maxwidth; + for(int i = 0; i < result->rank-1; i++) + result->shape[i] = right->shape[i]; + int offset = 0; + for(int i = 0; str[i] != 0; i++) + if(str[i] != '\n') + result->runedata[offset++] = str[i]; + free(str); + freearray(right); + return result; +} + +Array * fnSelfReference1(Array *right) { DfnFrame *dfn = getcurrentdfn(); |