Ambrose Bonnaire-Sergeant <abonnairesergeant@gmail.com> writes:
> This nicely cleaned up a conversion I'm working with> that has bor's nested 6-deep.
Thanks! Trying the tests here has made me realize that bnot doesn't work
with the --use-bit-lib flag, so I'm tracking that here:
https://todo.sr.ht/~technomancy/fennel/156
But that's unrelated to the issue at hand, so I'll just go ahead and
apply both your patches. (this one and the line number one).
> +(fn flatten-associative [associative-op-sym frm]> + (if (and (list? frm)> + (<= 3 (length frm))> + (= (. frm 1) associative-op-sym))> + (icollect [i frm (ipairs frm)]> + (when (not= 1 i)> + frm))> + [frm]))
This function can actually be written more succinctly as a pattern match:
(fn flatten-associative [op-sym form]
(match (list? form)
[op-sym a b &as op-call] (doto op-call (table.remove 1))
_ [form]))
This works because an idiosyncrasy in Fennel where the type predicates
return their argument rather than true, and by adding a and b to the
pattern, we ensure that the length is at least 3, because they're
guaranteed to not be nil.
I wonder if we can use this same logic to make it so that t[k][k2]
compiles to a single . call instead of (. (. t k) k2).
It's not exactly the same thing since they're not operators, but it's
pretty similar. Maybe I'll take a look at this later if no one beats me
to it.
-Phil
> I wonder if we can use this same logic to make it so that t[k][k2]> compiles to a single . call instead of (. (. t k) k2).>> It's not exactly the same thing since they're not operators, but it's> pretty similar. Maybe I'll take a look at this later if no one beats me> to it.
Never mind; figured it out. =)
https://git.sr.ht/~technomancy/antifennel/commit/31d79f28502b34fd7bbce957d391cfed15e0e395
-Phil
> This works because an idiosyncrasy in Fennel where the type predicates > return their argument rather than true
Ah! I noticed the same useful behavior with Lua's `assert`.
> Never mind; figured it out. =)
Nice!
Ambrose