Hi. It's been a pleasure to met with this great language.
So I'm trying to convert my old school Python code into Nimrod like as the followings:
import unicode
import strutils
# print s[2]
const s = "ニムロッド" # "Nimrod" in Japanese, 5 unicode characters length
var list: seq[string] = @[]
for x in runes(s):
list.add(toUTF8(x))
echo list[2]
# print s[1:4]
echo list[1..4].join
# print s[2:]
echo list[2..(-1)].join
However, it seems the first part is not clear as it should be. My intention is:
var list: seq[string] = reduce(toUTF8, runes(s))
Is there any way to do something like this? Thanks in advance.2. I think, I don't have to do it for syntax, but I have to, for semantics. I would like to write an Unicode library which supports all basic functions (replace, toupper, etc.):
import unicode
import strutils
proc u(s: string): seq[string] =
var list: seq[string] = @[]
for x in runes(s):
list.add(toUTF8(x))
return list
const s = "ニムロッド"
const v = u(s)
echo v[2]
echo v[1..4].join
echo v[2..(-1)].join
This one really works. But the following one didn't work, because adding every Unicode characters into one string variable makes one binary string (and result in binary outputs; not Unicode character sequences).
import unicode
proc u(s: string): string =
var r: string = ""
for x in runes(s):
r.add(toUTF8(x))
return r
const s = "ニムロッド"
const v = u(s)
echo v[2]
echo v[1..4]
echo v[2..(-1)]
It would be great if we can handle both Unicode string and character at once.
proc `{}`*(s: string, i: int): TRune = runeAt(s, i)
But I see runeAt doesn't implement what you want here since i is a byte index then. Well, feel free to add a proper implementation to unicode.nim and do a pull request. ;-)
3. Beware I didn't test:
proc foldl[U, V](fn: proc (a: V, b: U): V, s: openArray[U], z: V): V =
result = z
for i in 0..s.high:
result = fn(result, s[i])
Thank you so much for your help!
Wow, I didn't even imagine the handy 'result' exists. I'll check the manual first, and think how can I make a good contribution to unicode.nim (hopefully compatible way).