英文:
Semantic operator of dot in Kotlin
问题
fun binaryStringOf(message: String): String {
var s: String
s = (message)
.encodeToByteArray()
.joinToString("") { byte -> binaryStringOf(byte) }
return s
}
fun binaryStringOf(b: Byte): String {
return b.toString(2).padStart(8, '0')
}
英文:
I'd like to understand a bit better the 2 functions below. I know it is very compact and understand more or less what it does: it converts each characters of a string into string of '0' and '1'. But...
How does the dot(in front of encodeToByteArray) connect the 's' to encodeToByteArray()?
Where can I find more info about what dot represents?
Also, how and why the code { byte -> binaryStringOf(byte) }
can do the job in that place?
How does it "know" that there is a byte with which it calls the function binaryStringOf(byte)
Where can I find more info about it, too?
fun binaryStringOf(message: String): String {
var s: String
s = (message)
.encodeToByteArray()
.joinToString("") { byte -> binaryStringOf(byte) }
return s
}
fun binaryStringOf(b: Byte): String {
return b.toString(2).padStart(8, '0')
}
答案1
得分: 1
上述格式使事情变得有点混乱,但让我尝试解释发生了什么。
=
是一个赋值操作符。它表示"将变量s
分配给右侧表达式的结果"。
现在我们看到 message
是 binaryStringOf
函数的参数,类型为 String
。String
是一个类,其中包含一个函数(当它是类的成员时也称为方法),名为 encodeToByteArray
,返回一个ByteArray
。
ByteArray
又有一个名为 joinToString
的函数,我们向它传递了两个参数:一个是 String
类型的,另一个是 ((Byte) -> CharSequence)
类型的(即,该函数本身作为变量传递,使用 lambda 语法)。Kotlin 有一些语法糖,使 lambda 是最后一个参数时看起来更漂亮。
因此,语句
s = (message)
.encodeToByteArray()
.joinToString("") { byte -> binaryStringOf(byte) }
意思是"变量 s
被赋予在调用 encodeToByteArray
在 message
上的结果上调用 joinToString
后得到的值"。
然后 return s
表示 binaryStringOf
的返回值应该是分配给 s
的任何值。
英文:
The formatting above makes things a little bit more confusing, but let me try to explain what is going on.
The =
is an assignment operator. It says "assign the variable s
to the result of the expression on the right side".
Now we see that message
is a parameter in the binaryStringOf
function of type String
. String
is a class which contains a function (also called a method when it is a member of a class) called encodeToByteArray
which returns a ByteArray
.
ByteArray
in turn has a function called joinToString
which we're giving two parameters: one of type String
, and one of type ((Byte) -> CharSequence)
(ie, the function is itself being passed in as a variable, using lambda syntax). Kotlin has some syntactic sugar to make this look nicer when the lambda is the last argument.
So, the statement
s = (message)
.encodeToByteArray()
.joinToString("") { byte -> binaryStringOf(byte) }
means "the variable s
is assigned the value that results from calling joinToString
on the result of calling encodeToByteArray
on message
.
Then return s
says that the return value from the binaryStringOf
should be whatever value was assigned to s
.
答案2
得分: 0
.encodeToByteArray()
作用于传入的字符串(在这种情况下为message
)。它返回一个ByteArray
,即表示字节值数组的东西。
然后,在该数组对象上调用joinToString()
方法。该方法接收各种参数,但只提供了分隔字符串(""
)和transform
参数。
现在:transform
是一个函数。它是可以带参数调用的东西,并且必须返回特定的结果。
理解的关键部分是{ byte -> ... }
就是transform
函数参数。
英文:
.encodeToByteArray()
works on the incoming string (message
in this case). It returns a ByteArray; so something that represents an array of Byte values.
And on that array object, it invokes the joinToString() method. That method receives various arguments, but only the separator string (""
) is provided, and the transform
parameter.
Now: transform
is a function. It is something that can be invoked, with parameters, and that has to return a specific result.
The key part to understand is that { byte -> ... }
is that transform
function parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论