summaryrefslogtreecommitdiff
path: root/functions.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-29 23:50:30 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-29 23:50:30 +0000
commit3ccc3ac8b0cc090f5315b23effd4ce6c8cad783d (patch)
tree3dbf07ea2f6897e80fce694d3e35600be5bf2bfa /functions.c
parent6b775194811823b84e2694c20c036506d5855215 (diff)
Add monadic format ⍕. It doesn't work for nested arrays yet.
Diffstat (limited to 'functions.c')
-rw-r--r--functions.c42
1 files changed, 41 insertions, 1 deletions
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();