Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split example from std/unicode doesn't run #17174

Closed
AFaurholt opened this issue Feb 24, 2021 · 3 comments · Fixed by #17176
Closed

split example from std/unicode doesn't run #17174

AFaurholt opened this issue Feb 24, 2021 · 3 comments · Fixed by #17176

Comments

@AFaurholt
Copy link
Contributor

The example at https://nim-lang.org/docs/unicode.html#split.i%2Cstring%2CRune%2Cint does not run.
https://play.nim-lang.org/#ix=2QI0

Example

import std/unicode
for word in split("this:is;an$example", {';', ':', '$'}):
  writeLine(stdout, word)

Current Output

/usercode/in.nim(2, 18) Error: type mismatch: got <string, set[char]>
but expected one of: 
iterator split(s: string; sep: Rune; maxsplit: int = -1): string
  first type mismatch at position: 2
  required type for sep: Rune
  but expression '{';', ':', '$'}' is of type: set[char]
iterator split(s: string; seps: openArray[Rune] = unicodeSpaces;
               maxsplit: int = -1): string
  first type mismatch at position: 2
  required type for seps: openArray[Rune]
  but expression '{';', ':', '$'}' is of type: set[char]
proc split(s: string; sep: Rune; maxsplit: int = -1): seq[string]
  first type mismatch at position: 2
  required type for sep: Rune
  but expression '{';', ':', '$'}' is of type: set[char]
proc split(s: string; seps: openArray[Rune] = unicodeSpaces; maxsplit: int = -1): seq[
    string]
  first type mismatch at position: 2
  required type for seps: openArray[Rune]
  but expression '{';', ':', '$'}' is of type: set[char]

expression: split("this:is;an$example", {';', ':', '$'})

Expected Output

"this"
"is"
"an"
"example"

Possible Solution

I have no idea, sorry, I'm a noob

Additional Information

I switched to using strutils and that works fine with the above example.

$ nim -v
Nim Compiler Version 1.4.4
@AFaurholt AFaurholt changed the title Think about the title, twice split example from std/unicode doesn't run Feb 24, 2021
@zetashift
Copy link
Contributor

This is how I got the example working:

import std/unicode
let splitters = ";:$"
for word in split("this:is;an$example", splitters.toRunes):
  writeLine(stdout, word)

But it's cumbersome, what is the easiest way to make a set/seq/openarrray of Runes?

@SolitudeSF
Copy link
Contributor

But it's cumbersome

looks almost identical to the example. if you inline the variable its even the same line count.

@zetashift
Copy link
Contributor

But it's cumbersome

looks almost identical to the example. if you inline the variable its even the same line count.

Okay cumbersome might be the wrong word :P, it's just a bad example, like is anyone ever gonna use a string made of characters to split a word?

import std/unicode

for word in split("this:is;an$example", ";:$".toRunes):
  writeLine(stdout, word)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants