Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikea committed Jun 17, 2024
1 parent 0713073 commit 1f25f5e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
14 changes: 4 additions & 10 deletions src/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ struct array {
}
}

inline array_p atom_i(size_t i) const { return atom(t, data_i(i)); }
inline array_p atom_i(size_t i) const {
if (t == T_ARR) return data<arr_t>()[i];
else return atom(t, data_i(i));
}
inline array_p tail() const { return create(t, n - 1, data_i(1)); }
};

Expand All @@ -204,12 +207,3 @@ using array_p = rc<array>;

#define DO_MUT_ARRAY(a, typ, i, p) _DO_ARRAY_IMPL(a, i, p, UNIQUE(__), auto& p = a->mut_data<typ>()[i])
#define DO_ARRAY(a, t, i, p) _DO_ARRAY_IMPL(a, i, p, UNIQUE(__), auto p = a->data<t>()[i])

template <typename Fn>
inline void array::for_each_atom(Fn callback) const {
if (t != T_ARR) {
DO(i, n) { callback(i, array::atom_i(i)); }
} else {
DO_ARRAY(this, arr_t, i, e) { callback(i, e); }
}
}
34 changes: 15 additions & 19 deletions src/words.c++
Original file line number Diff line number Diff line change
Expand Up @@ -666,51 +666,47 @@ DEF_WORD("\\s", slash_stack) {
DEF_WORD(",fold", fold) {
POP(y);
POP(x);
t_dict_entry e = as_dict_entry(y);
auto iter = [&](size_t i, array_p slice) mutable {
PUSH(slice);
t_dict_entry e = as_dict_entry(y);
DO(i, x->n) {
PUSH(x->atom_i(i));
if (i > 0) inter.entry(e);
};
x->for_each_atom(iter);
}
}

DEF_WORD(",scan", scan) {
POP(y);
POP(x);
t_dict_entry e = as_dict_entry(y);
auto iter = [&](size_t i, array_p slice) mutable {
t_dict_entry e = as_dict_entry(y);
DO(i, x->n) {
if (i > 0) DUP;
PUSH(slice);
PUSH(x->atom_i(i));
if (i > 0) inter.entry(e);
};
x->for_each_atom(iter);
}
array_p result = cat(stack, x->n);
PUSH(result);
}

DEF_WORD(",apply", apply) {
POP(y);
POP(x);
t_dict_entry e = as_dict_entry(y);
auto iter = [&](size_t i, array_p slice) mutable {
PUSH(slice);
t_dict_entry e = as_dict_entry(y);
DO(i, x->n) {
PUSH(x->atom_i(i));
inter.entry(e);
};
x->for_each_atom(iter);
array_p result = cat(stack, x->n);
PUSH(result);
}

DEF_WORD(",pairwise", pairwise) {
POP(y);
POP(x);
t_dict_entry e = as_dict_entry(y);
auto iter = [&](size_t i, array_p slice) mutable {
PUSH(slice);
t_dict_entry e = as_dict_entry(y);
DO(i, x->n) {
PUSH(x->atom_i(i));
if (i > 0) inter.entry(e);
PUSH(slice);
PUSH(x->atom_i(i));
};
x->for_each_atom(iter);
DROP;
array_p result = cat(stack, x->n);
PUSH(result);
Expand Down

0 comments on commit 1f25f5e

Please sign in to comment.