jim
04/28/2021, 6:14 PMit.
?
kotlin
val a = "1 2\n3 4".lines().map{CharSequence::split("\\s+".toRegex())}
log
Matrix.kt: (10, 59): This syntax is reserved for future use; to call a reference, enclose it in parentheses: (foo::bar)(args)
e: /home/a/github/exercism/kotlin/matrix/src/main/kotlin/Matrix.kt: (10, 54): Overload resolution ambiguity:
public fun CharSequence.split(regex: Pattern, limit: Int = ...): List<String> defined in kotlin.text
public fun CharSequence.split(vararg delimiters: String, ignoreCase: Boolean = ..., limit: Int = ...): List<String> defined in kotlin.text
public fun CharSequence.split(vararg delimiters: Char, ignoreCase: Boolean = ..., limit: Int = ...): List<String> defined in kotlin.text
@InlineOnly public inline fun CharSequence.split(regex: Regex, limit: Int = ...): List<String> defined in kotlin.text
nanodeath
04/28/2021, 6:15 PMjim
04/28/2021, 6:21 PMval a = "1 2\n3 4".lines().map(CharSequence::split("\\s+".toRegex()))
isn't working either.jim
04/28/2021, 6:22 PMfun scoreWord(word: String): Int {
if(word.isEmpty()) return 0
return word
.map(Character::toUpperCase)
.map { scores.getOrDefault(it, 0) }
.reduce(Int::plus)
}
nanodeath
04/28/2021, 6:26 PMjim
04/28/2021, 6:28 PMCharacter::toUpperCase
Class::method
?nanodeath
04/28/2021, 6:29 PM(
I added isn't a typonanodeath
04/28/2021, 6:30 PM.map(Character::toUpperCase)
is exactly equivalent to .map { it.toUpperCase() }
or, more verbosely, .map { c: Character -> c.toUpperCase() }
nanodeath
04/28/2021, 6:31 PMval spaces = "\\s+".toRegex()
val a = "blah".lines().map { it.split(spaces) }
jim
04/28/2021, 6:32 PMmap{it.split("\\s+".toRegex())}
.jim
04/28/2021, 6:35 PMnanodeath
04/28/2021, 6:37 PM(Character -> T)
instead of a String, you could probably say map(it.split("\s"))
nanodeath
04/28/2021, 6:38 PMsplit
accepts one parameter, and that parameter isn't the string being split; it's the pattern you want to split on, which is crucialQuincy
04/28/2021, 6:57 PMval spaces = "\\s+".toRegex()
val spliter = { s: String -> s.split(spaces) }
val a = "blah".lines().map(splitter)
But if the actual use case is not more complicated than what you've shown it's not really buying you anything.jim
04/28/2021, 6:59 PM.map(Character::toUpperCase)
gets translated to .map { c: Character -> c.toUpperCase() }
, why .map(CharSequence::split("\\s+".toRegex())
is not translated to .map{cs:CharSequence -> cs.split("\\s+".toRegex())}
. I just mark it as one parameter extra. Does not work. Thank you for the replies.nanodeath
04/28/2021, 6:59 PMnanodeath
04/28/2021, 7:00 PMsplitter
thing into an actual method; that might be more efficientQuincy
04/28/2021, 7:02 PMCharacter::toUppercase
is a way of referring to a specific function defined elsewhere which satisfies the type (Character) -> Character
Roukanken
04/28/2021, 7:22 PMmySplit
? ... well original split has like 4 overloads and compiler complains it can't chose. Afaik, doing this is stretching the limits of Kotlin's type system
anyways, to real question: Map can only handle if you pass it reference to function taking 1 argument - because it has 1 value (the it
) and can just chunk it there. But split needs at least 2 arguments - a.split(b)
both a
and b
are arguments, and map
only has 1 value.
And no, you can't just pass 1 argument to reference and expect a new function to be created (sadly)Nir
04/28/2021, 7:30 PMRoukanken
04/28/2021, 7:31 PMit
then easy solution: name itNir
04/28/2021, 7:31 PM"blah".lines().map { it.split("\\s+".toRegex()) }
Already works and is perfectly readable, the problem is that you'd be executing the toRegex over and overNir
04/28/2021, 7:31 PMNir
04/28/2021, 7:32 PMspaces
variable but it's not clear how you'd hope to avoid that in general.Roukanken
04/28/2021, 7:42 PMpartial
toRegex
over and over - it passes the function just once to map, with already "inlined" argument
but without managing to resolve that overload ambiguity, it's not really viable solutionNir
04/28/2021, 8:02 PMNir
04/28/2021, 8:19 PMval a = "1 2\n3 4"
.lines()
.map("\\s+".toRegex().let{ {line -> line.split(it)} })
wbertan
04/28/2021, 9:43 PMRegex
already has a split
. Can check if the output is desirable, but could try this:
val a = "1 2\n3 4".lines().map("\\s+".toRegex()::split)
The docs says:
Splits the input CharSequence around matches of this regular expression.So I believe it should work as expected.