8

For example, I'd like to turn "hello" into list(104, 101, 108, 108, 111) or list("h", "e", "l", "l", "o")

So far I've created an empty list, used foreach and appended every item to the list myself, but that's not really a concise way to do it.

jer
  • 20,094
  • 5
  • 45
  • 69
Jakob
  • 24,154
  • 8
  • 46
  • 57
  • You **really** don't want to know what you have to go through to convert a string to a list in the hel-language. ;-) – T.E.D. Nov 23 '10 at 10:37
  • lol.. if I ever create a fairly serious programming language I promise to name it Hel :) – Jakob Nov 23 '10 at 10:44
  • Off topic but relevant to Io questions here on SO. I've rollbacked you tag edits you recently did because `iolanguage` is the canonical tag for Io. – draegtun Nov 23 '10 at 12:30
  • 2
    BTW, great question! Had me scratching my head for a while. I think an `asList` would be good addition to the core `Sequence` object. – draegtun Nov 23 '10 at 12:42
  • Unfortunately `"hello" split` and `"world" split("")` both do not work as expected. – Tino Jul 14 '11 at 12:53
  • To the down voters of my two answers: The question was about trying to give a concise answer without using empty list/foreach :( However for performance using empty list/foreach is clearly optimal :) – draegtun Dec 01 '11 at 09:01

4 Answers4

5

My own suggestion:

Sequence asList := method(
  result := list()
  self foreach(x,
    result append(x)
  )
)

Haven't tested it performance-wise but avoiding the regexp should account for something.

Jakob
  • 24,154
  • 8
  • 46
  • 57
  • Yes `foreach` will be much more performant. However it is less concise. Hopefully a concise performant solution is out there? – draegtun Nov 23 '10 at 13:29
  • 2
    Something similar to this approach is used in one of the tests (see byteList): https://github.com/stevedekorte/io/blob/master/libs/iovm/tests/correctness/SequenceBitTest.io – dstnbrkr Feb 04 '11 at 18:10
  • 1
    A little speedup: `result := list() preallocateToSize(self size)` – Tino Jul 14 '11 at 12:44
  • for me this does not execute, I changed it to `Sequence asList := method( result := List; self foreach(x, result append(x)))`. the difference is not huge but for someone like me who isn't familiar to the Io syntax and run time error messages, it was very difficult to find why it fails... – Pascal Mar 05 '14 at 20:24
4

Another nicely concise but still unfortunately slower than the foreach solution is:

Sequence asList := method (
    Range 0 to(self size - 1) map (v, self at(v) asCharacter)
)
AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71
2

One way would be to use Regex addon:

#!/usr/bin/env io

Regex

myList := "hello" allMatchesOfRegex(".") map (at(0))

But I'm sure there must be other (and perhaps even better!) ways.


Update - re: my comment in question. It would be nice to have something built into Sequence object. For eg:

Sequence asList := method (
    Regex
    self allMatchesOfRegex(".") map (at(0))
)

# now its just
myList := "hello" asList
AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71
  • That's a creative solution, but not really better no :) – Jakob Nov 23 '10 at 12:43
  • Its better than `foreach` because no kittens^h^h^h^h^h^h^h variables were harmed or killed in its construction :) – draegtun Nov 23 '10 at 12:57
  • 1
    Not many io-folks hanging around on stackoverflow apparently (or anywhere for that matter). +1 for the enthusiasm :) – Jakob Nov 25 '10 at 22:43
  • This is beyond old. Though, I can't find any answers in how to actually include an add on. How exactly do I add the Regex module. – Rig Jan 24 '16 at 17:28
  • 1
    @Rig - Regex Add-on comes with Io. There have been issues in the past getting it to compile but I thought it was fixed (though I haven't tried for a while!). – draegtun Jan 26 '16 at 19:33
  • Yeah, I'm not sure. I haven't been able to figure out how to get it available for use. All I really have is their site and the 7 languages book as a guide. Maybe there is some sort of import statement I haven't really seen listed? – Rig Jan 27 '16 at 16:46
  • @Rig - `Regex` imports the add-on (ie. adds the necessary methods to Sequence object). Try it in REPL and you should see something like this - https://github.com/stevedekorte/io/issues/210#issuecomment-20550324 NB. If you're getting an error whilst using a Regex method then check full thread in that link. – draegtun Jan 27 '16 at 18:14
  • I was just going to ask where do we find the up to date list of addons but alas just look on GitHub! Thanks ya'll. – Lorena Nicole Aug 05 '16 at 14:32
2

I have Io Programming Language, v. 20110905 and method asList is defined by default, so you can use it without any new definitions. Then you can get char codes with map method.

Io 20110905
Io> a := "Hello World"
==> Hello World
Io> a asList
==> list(H, e, l, l, o,  , W, o, r, l, d)
Io> a asList map(at(0))
==> list(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100)
Alex Kosh
  • 2,206
  • 2
  • 19
  • 18