Jak*_*kob 8 string list sequence iolanguage
例如,我想"hello"变成list(104, 101, 108, 108, 111)或list("h", "e", "l", "l", "o")
到目前为止,我已经创建了一个空列表,自己使用foreach并将每个项目附加到列表中,但这并不是一个简洁的方法.
我个人的建议:
Sequence asList := method(
result := list()
self foreach(x,
result append(x)
)
)
Run Code Online (Sandbox Code Playgroud)
没有在性能方面测试它,但避免正则表达式应该考虑到某些事情.
一种方法是使用 Regex 插件:
#!/usr/bin/env io
Regex
myList := "hello" allMatchesOfRegex(".") map (at(0))
Run Code Online (Sandbox Code Playgroud)
但我确信一定还有其他(也许甚至更好!)的方法。
更新-回复:我的评论有问题。如果能在 Sequence 对象中内置一些东西那就太好了。例如:
Sequence asList := method (
Regex
self allMatchesOfRegex(".") map (at(0))
)
# now its just
myList := "hello" asList
Run Code Online (Sandbox Code Playgroud)