Scala函数定义和用法(Scala function definitions and usage)
基于以下定义函数的示例,在不同场景中2和3的最佳用途是什么?
def sum(x: Int, y: Int): Int = { x+y }
这是一个带参数,返回类型和函数体的函数定义
val sum = (x: Int, y: Int) => { x+y }
这看起来像是一个lambda函数赋值给变量,为什么返回类型永远不会在这里定义?
val sum: (Int, Int) => Int = (x,y) => { x+y }
这是将函数定义为一个类型? 我不明白这是怎么回事!调用时所有3个函数将产生相同的结果:
scala> sum(1,2)
Int = 3
Based on below examples of defining functions what is the best use of 2 and 3 in different scenarios?
def sum(x: Int, y: Int): Int = { x+y }
This is a function definition with arguments, return type and function body
val sum = (x: Int, y: Int) => { x+y }
This seems like an assignment of lambda function to a variable, why return type is never defined here?
val sum: (Int, Int) => Int = (x,y) => { x+y }
This is defining a function as a type? I don't understand how this works!All 3 functions when invoked will yield the same result:
scala> sum(1,2)
Int = 3
原文:https://stackoverflow.com/questions/22747631
最满意答案
我假设你正在使用Python 3.在Python 2中,字符串默认是字节,所以它只会适合你。 但在Python 3中,字符串是unicode并且被解释为unicode,如果您将字节字符串读为unicode,则会导致此问题更难。
这个解决方案受到了mgilson的回答的启发。 我们可以通过使用
literal_eval
从字面上评估您的unicode字符串为字节字符串:from ast import literal_eval with open('source.txt', 'r', encoding='utf-8') as f_open: source = f_open.read() string = literal_eval("b'{}'".format(source)).decode('utf-8') print(string) # 扎加拉
I'm assuming you're using Python 3. In Python 2, strings are bytes by default, so it would just work for you. But in Python 3, strings are unicode and interpretted as unicode, which is what makes this problem harder if you have a byte string being read as unicode.
This solution was inspired by mgilson's answer. We can literally evaluate your unicode string as a byte string by using
literal_eval
:from ast import literal_eval with open('source.txt', 'r', encoding='utf-8') as f_open: source = f_open.read() string = literal_eval("b'{}'".format(source)).decode('utf-8') print(string) # 扎加拉
相关问答
更多-
ant javac encoding utf8 utf-8[2023-12-04]
这两种写法是一样的 不论 是编译时的参数 , 还是 在 java 内部 使用 ,utf8 和 utf-8 是一样的 其实你自己试一下就知道 ,不支持的编码 会报错的 -
以下MySQL函数将在双重编码后返回正确的utf8字符串: CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8) 它可以与UPDATE语句一起使用以更正字段: UPDATE tablename SET field = CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8); The following MySQL function will retur ...
-
一直到UTF-8(UTF-8 all the way through)[2022-02-19]
数据存储 : 在数据库中的所有表和文本列上指定utf8mb4字符集。 这使得MySQL在UTF-8中物理存储和检索本地编码的值。 请注意,如果指定了utf8mb4_* collation(没有任何显式字符集),MySQL将隐式使用utf8mb4编码。 在旧版本的MySQL(<5.5.3)中,您不幸被迫使用简单的utf8 ,它只支持Unicode字符的一个子集。 我希望我在开玩笑。 数据访问 : 在您的应用程序代码(例如PHP)中,无论使用utf8mb4数据库访问方法,都需要将连接字符集设置为utf8mb4 ... -
é 是é字符的HTML实体引用,而不是UTF-8编码的字符串。 要解码它,你可以使用Commons Lang的org.apache.commons.lang.StringEscapeUtils : String decodedStr = StringEscapeUtils.unescapeHtml("é"); é is the HTML entity reference for the é character, not the UTF-8 encoded stri ...
-
UTF-8编码问题(UTF-8 encoding issues)[2022-09-02]
最后,我确定了所有正在分解的字符并用Find-Replaces替换它们,然后将数据重新导入到utf-8表中。 一时兴起,我试图在实时环境中使用这些数据,即使修改后的数据在开发过程中看起来很棒,实时环境显示也会以类似的方式破坏。 区分旧数据和新数据揭示了数据的混乱程度: ½†x 12†Rebar 变 ½\" x 12\" Rebar 我猜测Rails 2使用的编码不是UTF-8。 In the end I identified all the characters ... -
欢迎来到令人困惑的编码世界! 至少要处理文件编码,终端编码和文件名编码,这三者可能不同。 在Python 2.x中,目标是从编码的str获取Unicode字符串(与str不同)。 问题是你并不总是知道用于str的编码,所以很难解码它。 当使用listdir()获取文件名时,有一个记录但经常被忽略的怪癖 - 如果你将str传递给listdir()你就会得到编码的str 。 这些将根据您的区域设置进行编码。 在Windows上,这些将是一个8位字符集,如windows-1252 。 或者,将listdir()传 ...
-
我假设你正在使用Python 3.在Python 2中,字符串默认是字节,所以它只会适合你。 但在Python 3中,字符串是unicode并且被解释为unicode,如果您将字节字符串读为unicode,则会导致此问题更难。 这个解决方案受到了mgilson的回答的启发。 我们可以通过使用literal_eval从字面上评估您的unicode字符串为字节字符串: from ast import literal_eval with open('source.txt', 'r', encoding='utf ...
-
UTF-8编码问题(UTF-8 encoding problems)[2023-08-19]
解决了: 在我的连接脚本中,我改变了这个: $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8'); 对此: $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass, array(PDO::MYSQ ... -
使用Go编码UTF-8(Encoding UTF-8 with Go)[2023-03-24]
根据文件: 字符串文字,缺少字节级转义,始终包含有效的UTF-8序列。 因此,如果在Golang源代码中,则不需要将字符串编码为utf8。 但是,如果字符串来自输入,则utf8包是您的朋友。 According to the documentation: A string literal, absent byte-level escapes, always holds valid UTF-8 sequences. So you don't need to encode into utf8 the strin ... -
我在过去遇到过类似的问题,这对我来说是个诡计。 mysql_query("SET names UTF8"); Okay, i found the answer!! I searched a bit more, (have been doing so for hours) and found this question: Whether to use “SET NAMES” Using the answer from that question i ran this query: mysql_query(" ...