ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

lua程序设计(第四版)练习答案自做(第二十章)

2021-02-28 15:31:12  阅读:265  来源: 互联网

标签:function Set end 自做 mt lua 第四版 return local


文章目录

仓库

20.1

#!/usr/bin/lua
local Set={}
local mt={}
function Set.new(l)
	local set={}
	setmetatable(set,mt)
	for _,v in pairs(l) do
		set[v]=true
	end
	return set
end
function Set.union(a,b)
	if getmetatable(a)~=mt or getmetatable(b)~=mt then
		error("attempt to 'add' a set with a non-set value",2)
	end
	local res=Set.new{}
	for k in pairs(a) do
		res[k]=true
	end
	for k in pairs(b) do
		res[k]=true
	end
	return res
end
function Set.intersection(a,b)
	if getmetatable(a)~=mt or getmetatable(b)~=mt then
		error("attempt to 'multiply' a set with a non-set value",2)
	end
	local res=Set.new{}
	for k in pairs(a) do
		res[k]=b[k]
	end
	return res
end
function Set.subtraction(a,b)
	if getmetatable(a)~=mt or getmetatable(b)~=mt then
		error("attempt to 'subtract' a set with a non-set value",2)
	end
	local res=Set.new{}
	for k in pairs(a) do
		res[k]=true
	end
	for k in pairs(b) do
		res[k]=nil
	end
	return res
end
function Set.tostring(set)
	local l={}
	for e in pairs(set) do
		l[#l+1]=e
	end
	return "{"..table.concat(l,",").."}"
end
function Set.length(set)
	local count=0
	for i in pairs(set) do
		count=count+1
	end
	return count
end
mt.__add=Set.union
mt.__mul=Set.intersection
mt.__sub=Set.subtraction
mt.__len=Set.length
mt.__tostring=Set.tostring
mt.__le=function (a,b)
	for k in pairs(a) do
		if not b[k] then
			return false
		end
	end
		return true
end
mt.__lt=function (a,b)
	return a<=b and not(b<=a)
end
mt.__eq=function (a,b)
	return a<=b and b<=a
end
return Set
for test
#!/usr/bin/lua
t=require("20-1and20-2")
a=t.new({1,2,3,4})
b=t.new{3,5}
print(a-b)
print(a+b)
print(a*b)
print("#a="..#a)

20.2

同上

20.3

#!/usr/bin/lua
local proxy={}
local mt={}
function readOnly(t)
	mt.__index=function (_,k) return rawget(t,k) end
	mt.__newindex=function (t,k,v) error("attempt to update a read-only table",2) end
	setmetatable(proxy,mt)
	return proxy
end
days={"Sunday","Monday","Tuesday","Wednesday","Tursday","Friday","Saturday"}
days=readOnly(days)
print(days[1])
days[2]="Noday"

20.4

#!/usr/bin/lua
function fileAsArray(filename)
	local proxy={}

	local filearray={}
	local file=assert(io.open(filename,"r"))
	for block in file:lines(1) do
		filearray[#filearray+1]=block
	end
	file:close()
	
	local mt={
		__index=function (_,k) return filearray[k] end,
		__newindex=function (_,k,v)
			filearray[k]=v
			local file=assert(io.open(filename,"w"))
			for _,w in ipairs(filearray) do
				file:write(w)
			end
			file:close()
		end,
		__pairs=function ()
			return function (_,k)
				local nextkey,nextvalue=next(filearray,k)
				return nextkey,nextvalue
			end
		end,
		__len=function ()
			return #filearray
		end
	}


	setmetatable(proxy,mt)
	return proxy
end


--the content under the line are for test 
f=fileAsArray("text")
print(f[2])
f[2]="i"
for x,y in pairs(f) do
	print(x,y)
end
print(#f)

20.5

同上

标签:function,Set,end,自做,mt,lua,第四版,return,local
来源: https://blog.csdn.net/fuluoyide312/article/details/114219240

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有