|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/BinarySearch1.lua.html
-- http://angg.twu.net/LUA/BinarySearch1.lua
-- (find-angg "LUA/BinarySearch1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/BinarySearch1.lua"))
BinarySearch = Class {
type = "BinarySearch",
__index = {
middle = function (bs) return (bs.a + bs.b) / 2 end,
newinterval = function (bs, newa, newb)
return BinarySearch {a=newa, b=newb, f=bs.f}
end,
left = function (bs) return bs:newinterval(bs.a, bs:middle()) end,
right = function (bs) return bs:newinterval(bs:middle(), bs.b) end,
halve = function (bs, ntimes)
for i=1,ntimes do
if bs.f(bs:middle()) == bs.f(bs.a)
then bs = bs:right()
else bs = bs:left()
end
end
return bs
end,
},
}
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "BinarySearch1.lua"
bs = BinarySearch {a=0, b=10, f = function (x) return x^2 < 2 end}
= bs:halve(10):middle()
= bs:halve(100):middle()
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: