3 ms·
Ah, but can objects be used to implement Duff's Device?
by fallingfrog 8y ago
Ah, but can objects be used to implement Duff's Device?
- pmontra 8y agoInteresting technique https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
- jerf 8y agoKinda-sorta: function psuedoduff(count) { var x = 0; var n = Math.floor((count + 7) / 8); var o = { 0: function() { x++; o[7]() }, 7: function() { x++; o[6]() }, 6: function() { x++; o[5]() }, 5: function() { x++; o[4]() }, 4: function() { x++; o[3]() }, 3: function() { x++; o[2]() }, 2: function() { x++; o[1]() }, 1: function() { x++; n--; if (n > 0) { o[0]() }}, } if (count > 0) { o[count%8](); } return x; } That just increments to show the point, it would be easy to make it do some sort of real work. Of course you'll run out of recursion at some point. Trampolining could fix that. I leave it as an exercise for the reader whether that makes it more or less silly. I'm also of course aware that Duff's device doesn't make function calls, to which I vigorously handwave while chanting "Monads Are Programmable Semicolons" in my "Lambda The Ultimate" T-Shirt. Lambda.