-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Fix unicode.split with seperators examples #17176
Conversation
https://nim-lang.org/docs/unicode.html#split.i%2Cstring%2CRune%2Cint didn't work with the set constructor `{}` so replaced it with a string that's converted to an openArray compatible type containing `Rune`s.
@timotheecour using import sugar
let splittedBySequence = newSeq[string]()
let separators = collect(newSeq):
for separator in @[";", ":", "$"]:
separator.toRunes
for word in split("this:is;an$example", separators):
splittedBySequence.add(word)
doAssert splittedBySequence = @["this", "is", "an", "example"] |
I currently have this, is this a decent example? # And the following code splits a string using a sequence of Runes.
import sugar
var splittedBySequence = newSeq[string]()
let separators = collect(newSeq):
for separator in ";:$".runes:
separator
for word in split("this:is;an$example", separators):
splittedBySequence.add(word)
doAssert splittedBySequence == @["this", "is", "an", "example"] |
Looks good! nit: import std/sugar
var splittedBySequence = newSeq[string]()
let separators = collect:
for separator in ";:$".runes:
separator
for word in split("this:is;an$example", separators):
splittedBySequence.add(word)
# use assert
assert splittedBySequence == @["this", "is", "an", "example"] Or even one line let separators = collect(for separator in ";:$".runes: separator) |
I didn't know you could use collect like that, the examples didn't mention it! :D, awesome thanks. |
Last question is it better to group the imports? import std/sequtils
assert toSeq("hÃllo\lthis\lis an\texample\l是".split) ==
@["hÃllo", "this", "is", "an", "example", "是"]
# And the following code splits the same string using a sequence of Runes.
import std/sugar
var splittedBySequence = newSeq[string]()
let separators = collect(for separator in ";:$".runes: separator) Turn the imports into a top level: |
yes but turns out you don't even need |
LGTM, thanks! |
I would love to do more of these! I am gonna ask a lot of questions though since I'm not familiar with all of the stdlib modules, if that's okay haha! |
* Fix unicode.split with seperators examples https://nim-lang.org/docs/unicode.html#split.i%2Cstring%2CRune%2Cint didn't work with the set constructor `{}` so replaced it with a string that's converted to an openArray compatible type containing `Rune`s. * Add runnableExamples to unicode.split * Add runnableExamples to split with single separator too * Simplify runnableExamples unicode.split * Improve the rest of the runnableExamples with the simplified code * Simplify runnableExamples of unicode.split even more * Formatted unicode.split example * Update lib/pure/unicode.nim Co-authored-by: zetashift <rishi2@laptop.localdomain> Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
* Fix unicode.split with seperators examples https://nim-lang.org/docs/unicode.html#split.i%2Cstring%2CRune%2Cint didn't work with the set constructor `{}` so replaced it with a string that's converted to an openArray compatible type containing `Rune`s. * Add runnableExamples to unicode.split * Add runnableExamples to split with single separator too * Simplify runnableExamples unicode.split * Improve the rest of the runnableExamples with the simplified code * Simplify runnableExamples of unicode.split even more * Formatted unicode.split example * Update lib/pure/unicode.nim Co-authored-by: zetashift <rishi2@laptop.localdomain> Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Fixes: #17174
https://nim-lang.org/docs/unicode.html#split.i%2Cstring%2CRune%2Cint didn't work with the set constructor
{}
so replaced it with a string that's converted to an openArray compatible type containingRune
s.If there is a better/more idiomatic way to get Runes from a set/string, then please do tell, first time interacting with Unicode module.