如何将字符串转换为Io中的列表?

Jak*_*kob 8 string list sequence iolanguage

例如,我想"hello"变成list(104, 101, 108, 108, 111)list("h", "e", "l", "l", "o")

到目前为止,我已经创建了一个空列表,自己使用foreach并将每个项目附加到列表中,但这并不是一个简洁的方法.

Jak*_*kob 5

我个人的建议:

Sequence asList := method(
  result := list()
  self foreach(x,
    result append(x)
  )
)
Run Code Online (Sandbox Code Playgroud)

没有在性能方面测试它,但避免正则表达式应该考虑到某些事情.

  • 在其中一个测试中使用类似于此方法的东西(参见byteList):https://github.com/stevedekorte/io/blob/master/libs/iovm/tests/correctness/SequenceBitTest.io (2认同)

dra*_*tun 2

一种方法是使用 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)