七周七语言:Io Day 3
来源:互联网 发布:剑网3a卡优化 编辑:程序博客网 时间:2024/05/16 00:29
第三天
做
改进本节生成的XML程序,增加空格以显示缩进结构。
Builder := Object cloneBuilder depth ::= 0Builder forward := method(#depth ::= 0prefix := (" " repeated (self depth))writeln(prefix, "<", call message name, ">")#depth = (depth +1)self setDepth(self depth + 1)call message arguments foreach( arg, content := self doMessage(arg); if(content type == "Sequence", writeln(prefix, " ",content))) #depth = (depth - 1) self setDepth(self depth - 1)writeln(prefix, "</", call message name, ">"))Builder ul(li("Io"),li("Lua"),li("JavaScript"))
创建一种使用括号的列表语法
看到这道题目的时候比较奇怪……感觉没有表达清楚让我们要创建的是什么?乍一看,还以为要写一个类似Lisp的语法(难道是我理解的问题ORZ)?后来看到别人写的才知道是什么(还是有点奇怪- -)……
curlyBrackets := method( mapList := call message arguments map( value, self doMessage(value); ) return(mapList))example := { "Item one", "Item two", "Item three", "Final item"}("Size:" .. example size()) printlnexample printlnexample at( example size() - 1) println
改进本节生成的XML程序,使其可处理属性:如果第一个参数是映射(用大括号表示语法),则为XML程序添加属性。例如:
book({"author": "Tate"}...))将打印出< book author="Tate"; >先贴一个实现的比较全面的:addr
然后是我写的……不幸的是怎么都显示不标签的属性来:(
上面那个很全面,可是对于这题而言又太过复杂了(话说那位同学写的注释是真多啊!),看来对于Io的很多内容真的是知之略少啊:(newXML := Object clonenewXML depth ::= 0OperatorTable addAssignOperator(":", "atPutNumber")curlyBrackets := method( attributes := list() call message arguments foreach( attributePair,attributes append( self doString(attributePair asString)) ) return attributes)atPutNumber := method(name, value, attribute := Map clone attribute atPut("name", name) attribute atPut("value", value) return attribute #self atPut( #call evalArgAt(0) atMutable removePrefix("\"") removeSuffix("\"")#call evalArgAt(1) #))atPut := method( name, value, if(list("Sequence", "Number") contains( value type), super(atPut(name, value))) return self)newXML forward := method( prefix := (" " repeated (self depth)) writeln(prefix, "<", call message name, ">") self setDepth(self depth + 1) call message arguments foreach( arg,content := self doMessage(arg);if(content type == "Sequence", writeln(prefix, " ", content)) ) self setDepth(self depth - 1) writeln(prefix, "</", call message name, ">"))newXML ul( li("Io"), li(book({"author": "Tate"})), li("JS"))
- 七周七语言:Io Day 3
- 七周七语言:Io Day 1
- 七周七语言:Io Day 2
- 七周七语言:Prolog Day 3
- 七周七语言:Ruby Day 1
- 七周七语言:Ruby Day 2
- 七周七语言:Prolog Day 1
- 七周七语言:Prolog Day 2
- 七周七语言之IO语言代码学习
- Day-3
- day 3
- Day 3
- [Day 3]
- Day 3
- day-10-【IO【File、递归】)
- day-11-【IO【字节流、高效流】
- Day Three: Perl语言基本概念
- Day-1:C语言基本知识:
- 并发控制
- HTML5游戏开发 之 循环的控制(3)
- CString.Format的详细用法
- 解读IBM InfoSphere大数据分析平台
- string,CString,char*之间的转化
- 七周七语言:Io Day 3
- Android 开发基础知识 Timer
- mini usb 接口定义 5针usb接口定义图
- 循环串问题
- The output of training C-SVM is like the following. What do they mean?
- 地雷游戏结题报告
- 成员管理和授权
- the difference cv value in libsvm
- x264中“!!”用法解析