写现代JS还是比lua舒服
写新机器框架,把hclua中的ring组件迁移到了js,实在是差距太大lua代码:
local M = {}
M.Ring = {}
M.Ring.__index = M.Ring
function M.Ring:new()
local r = {
_next = nil,
_prev = nil,
_value = nil,
}
setmetatable(r, self)
return r
end
function M.Ring:_init()
self._next = self
self._prev = self
return self
end
function M.Ring:next()
if self._next == nil then
return self:_init()
end
return self._next
end
function M.Ring:prev()
if self._next == nil then
return self:_init()
end
return self._prev
end
function M.Ring:value()
return self._value
end
function M.Ring:withValue(v)
self._value = v
return self
end
function M.Ring:move(n)
if self._next == nil then
return self:_init()
end
local r = self
if n < 0 then
while n < 0 do
r = r:prev()
n = n + 1
end
elseif n > 0 then
while n > 0 do
r = r:next()
n = n - 1
end
end
return r
end
function M.Ring:link(r)
local n = self:next()
if (r ~= nil) then
local p = r:prev()
self._next = r
r._prev = self
n._prev = p
p._next = n
end
return n
end
function M.Ring:unlink(n)
if n <= 0 then
return nil
end
return self:link(self:move(n + 1))
end
function M.Ring:len()
local n = 0
if (self ~= nil) then
n = 1
local p = self:next()
while p ~= self do
n = n + 1
p = p:next()
end
end
return n
end
function M.Ring:apply(fn)
if (self ~= nil) then
fn(self:value())
local p = self:next()
while p ~= self do
fn(p:value())
p = p:next()
end
end
end
function M.new(n)
if (n <= 0) then
return nil
end
local r = M.Ring:new()
local p = r
local i = 1
while i < n do
local newp = M.Ring:new()
newp._prev = p
p._next = newp
p = newp
i = i + 1
end
p._next = r
r._prev = p
return r
end
return M
js代码
(function () {
class Ring {
constructor() {
}
#next = null
#prev = null
#value = null
#init() {
this.#next = this
this.#prev = this
return this
}
Next() {
if (this.#next == null) {
return this.#init()
}
return this.#next
}
Prev() {
if (this.#next == null) {
return this.#init()
}
return this.#prev
}
Value() {
return this.#value
}
WithValue(v) {
this.#value = v
return this
}
Move(n) {
if (this.#next == null) {
return this.#init()
}
let r = this
if (n < 0) {
while (n < 0) {
r = r.Prev()
n = n + 1
}
} else if (n > 0) {
while (n > 0) {
r = r.Next()
n = n - 1
}
}
return r
}
Link(r) {
let n = this.Next()
if (r != null) {
let p = r.Prev()
this.#next = r
r.#prev = this
n.#prev = p
p.#next = n
}
return n
}
Unlink(n) {
if (n <= 0) {
return null
}
return this.Link(this.Move(n + 1))
}
Len() {
let n = 0
if (this != null) {
n = 1
let p = this.Next()
while (p != this) {
n = n + 1
p = p.Next()
}
}
return n
}
Apply(fn) {
if (this != null) {
let p = this.Next()
while (p != this) {
fn(p.Value())
p = p.Next()
}
}
}
static New(n) {
if (n <= 0) {
return null
}
let r = new Ring()
let p = r
let i = 1
while (i < n) {
let newp = new Ring()
newp.#prev = p
p.#next = newp
p = newp
i = i + 1
}
p.#next = r
r.#prev = p
return r
}
}
return Ring
})()
js的真的赏心悦目多了。
最关键用起来也方便,不用考虑倒地是对象还是table,一个点全打通
(function(app){
let ring=app.Include("helllibjs/lib/container/ring.js")
class History{
constructor(size){
this.Lines=ring.New(size)
this.Size=size
}
OnEvent(event){
if (event.Name=="line"){
this.Current=event.Data.Output
this.CurrentOutput=JSON.parse(DumpOutput(1))
this.Lines=this.Lines.Next().WithValue({
Line:this.Current,
Output:this.CurrentOutput,
})
}
}
GetLast(n){
let result=[]
if (n>this.Size){
n=this.Size
}
let r=this.Lines
for (let i=0;i<n;i++){
let v=r.Value()
if (v!=null){
result.unshift(v)
}
r=r.Prev()
}
return result
}
static Install(size){
app.History=new History(size)
app.Engine.BindEventHandler(function(event){
app.History.OnEvent(event)
})
}
Size=0
Lines=null
Current=""
CurrentOutput=null
}
return History
})
虽然一直觉得JS不好用,但也要看对比。
星爷的唐伯虎点秋香诚不欺我。
杰哥 你这是要重新开发mud客户端了吗 许愿杰哥设计
一款现在化的mud 客户端
基于electron 的一款跨平台GUI客户端,抛弃lua全部应用js技术栈.
函数命令封装要全面,
地图绘画简洁,调用地图简单,封装寻路遍历算法.
要做到不是程序员出身的新手小白也能上手使用;注重逻辑实现,轻算法. cmfu 发表于 2024-9-30 03:39 AM
许愿杰哥设计
一款现在化的mud 客户端
基于electron 的一款跨平台GUI客户端,抛弃lua全部应用js技术栈.
我的客户端本身就是js/lua双持啊。
页:
[1]