首页 \ 问答 \ Scala函数定义和用法(Scala function definitions and usage)

Scala函数定义和用法(Scala function definitions and usage)

基于以下定义函数的示例,在不同场景中2和3的最佳用途是什么?

  1. def sum(x: Int, y: Int): Int = { x+y }这是一个带参数,返回类型和函数体的函数定义

  2. val sum = (x: Int, y: Int) => { x+y }这看起来像是一个lambda函数赋值给变量,为什么返回类型永远不会在这里定义?

  3. 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?

  1. def sum(x: Int, y: Int): Int = { x+y } This is a function definition with arguments, return type and function body

  2. 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?

  3. 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
更新时间:2021-10-13 22:10

最满意答案

我假设你正在使用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)  # 扎加拉

相关问答

更多
  • 这两种写法是一样的 不论 是编译时的参数 , 还是 在 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 ...
  • 数据存储 : 在数据库中的所有表和文本列上指定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 ...
  • 最后,我确定了所有正在分解的字符并用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 ...
  • 解决了: 在我的连接脚本中,我改变了这个: $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 ...
  • 根据文件: 字符串文字,缺少字节级转义,始终包含有效的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(" ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)