|
|
On 6/17/24 23:27, Ilya Razmanov wrote:
> Ok, since my previous silly question was about piecewise functions
> rather than #switch per se (that is, case was supposed to be used to
> make function piecewise), I think I'll publish a solution for initial
> problem here, although it's not directly related to #switch.
Thanks for posting your solution. It's good to be reminded of useful
approaches to need.
On your original question about #switch inside a function{} block, I saw
your switch by multiplication as good as anything else for clarity. As
others said, using select()s is the more common function{} block switch
alternative.
It is perhaps useful to add that there is a performance trade-off
between the two approaches. The select()s are often faster, though it
depends some on the details.
Bill P.
// Example requires the yuqk fork for f_boom() which throws after
// printing six values. It's a user function debugging aid.
#include "functions.inc"
#declare Fn_switch =
function (_V,_Default) {
select(-(_V=1),f_boom(1,0,0,0,0,0),
select(-(_V=2),f_boom(2,0,0,0,0,0),
select(-(_V=3),f_boom(3,0,0,0,0,0),
select(-(_V=4),f_boom(4,0,0,0,0,0),
_Default)
)
)
)
}
#declare Fn_switchByMult =
function (_V,_Default) {
(_V=1)*f_boom(1,0,0,0,0,0) +
(_V=2)*f_boom(2,0,0,0,0,0) +
(_V=3)*f_boom(3,0,0,0,0,0) +
(_V=4)*f_boom(4,0,0,0,0,0) +
((_V<1)|(_V>4))*_Default
}
#debug concat("\nFn_switch returned <",
str(Fn_switch(5,0),1,0),">\n\n")
// --- Fn_switchByMult always throws as f_boom(1,0,0,0,0,0)
// always runs. Everything is evaluated in a 'switch by
// multiplication'
// #debug concat("\nFn_switchByMult returned <",
// str(Fn_switchByMult(5,0),1,0),">\n\n")
#error "Stop early\n"
//---
Post a reply to this message
|
|