如何在scala中编写可读的字符串

发布于 2024-10-20 15:50:36 字数 799 浏览 1 评论 0原文

在 scala 中,当您将字符串“Hello World”写入文件时,它会写入

Hello World

(注意:没有双引号)。

Lisp 有打印和写入的概念。一种不带双引号,另一种则包含双引号,以便轻松写出数据结构并稍后使用标准阅读器读回它们。

无论如何,在 Scala 中可以做到这一点吗?

对于一个字符串来说,格式化它是很容易的——但是对于许多深度嵌套的结构来说,这几乎是不可能的。

例如,假设我要

sealed trait PathSegment
case class P(x:String) extends PathSegment
case class V(x:Int)    extends PathSegment

创建一个 dos:

P("X")

V(0)

这些 PathSegments 的列表打印为:

List(P(paths), P(/pets), P(get), P(responses), V(200))

我希望将其打印为:

List(P("paths"), P("/pets"), P("get"), P("responses"), V(200))

换句话说,我想要字符串(和字符),无论在哪里出现在结构中并打印为 "foo"'c'

In scala, when you write out a string "Hello World" to a file it writes

Hello World

(note: no double quotes).

Lisp has a concept of print and write. One writes without the double quotes, the other includes them to make it easy to write out data structures and read them back later using the standard reader.

Is there anyway to do this in Scala?

With one string it is easy enough to format it - but with many deeply nested structures, it is nearly impossible.

For example, say I have

sealed trait PathSegment
case class P(x:String) extends PathSegment
case class V(x:Int)    extends PathSegment

To create one does:

P("X")

or

V(0)

a list of these PathSegments prints as:

List(P(paths), P(/pets), P(get), P(responses), V(200))

I want this to print out as:

List(P("paths"), P("/pets"), P("get"), P("responses"), V(200))

In other words, I want strings (and characters), no matter where to occur in a structure to print out as "foo" or 'c'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再可℃爱ぅ一点好了 2024-10-27 15:50:36

这就是序列化的意义所在。另外,为什么 JSON 很受欢迎。

That's what Serialization is about. Also, why JSON is popular.

半窗疏影 2024-10-27 15:50:36

查看 lift-json ( https://github. com/lift/lift/tree/master/framework/lift-base/lift-json/ )用于写出将由另一种语言解析和读取的数据。 JSON 是 Web 服务领域请求/响应序列化的相当标准,几乎每种语言都有 JSON 库。

要从字面上写出包含双引号的字符串,您还可以执行以下操作:

"""
The word "apple" is in double quotes.
"""

我发现像 JSON 这样稍微结构化的格式更有用,并且像 lift-json 这样的库在引用字符串而不是引用整数方面做了正确的事情, ETC。

Check out lift-json ( https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/ ) for writing data out that will be parsed and read by another language. JSON is pretty standard in the web services world for request/response serialization and there are JSON libraries in just about every language.

To literally write out a string including double quotes, you can also do something like this:

"""
The word "apple" is in double quotes.
"""

I find a slightly more structured format like JSON more useful, and a library like lift-json does the right thing in terms of quoting Strings and not quoting Ints, etc.

时光无声 2024-10-27 15:50:36

我认为您正在寻找类似于 Javascript 的 eval() + JSON 以及 Python 的 eval()str()repr 的东西()。本质上,您需要 Lispy 对称元循环评估。这意味着您可以将数据转换为源代码,并评估该源代码并返回相同的数据,对吧?

AFAIK,Scala 中没有等效的 eval() 。 Daniel Spiewak 之前此处讨论过这个问题。然而,如果你真的想要的话。我建议如下:

  1. 每个集合对象都有 3 个方法,允许您将其数据转换为您想要的字符串表示形式。有mkStringaddStringstringPrefix。对它们进行一些巧妙的处理(想象将内存中的 ADT“反编译”回源代码形式),然后您将到达步骤 2)。本质上,您可以将 List(1,2,3) 创建的整数列表转换回字符串 "List(1,2,3)"。对于更基本的文字(例如简单的字符串或整数),您需要使用隐式修饰内置类型,为它们提供这些 toString(我在这里重载了该术语)辅助方法。
  2. 现在您有了字符串表示形式,您可以考虑如何“解释”或“评估”它们。您将需要一个 eval() 函数来创建解析器组合器的新实例,该解析器组合器可以理解 Scala 的文字并为您重新组装数据结构。

实现这个实际上听起来很有趣。如果您已成功实施,请不要忘记在此处发帖。 :)

I think you are looking for something like Javascript's eval() + JSON, and Python's eval(), str() and repr(). Essentially, you want Lispy symmetric meta-circular evaluation. Meaning you can transform data into source code, and evaluating that source code with give you back the same data, right?

AFAIK, there's no equivalent of eval() in Scala. Daniel Spiewak has talked about this here before. However, if you reeeeeealy want to. I suggest the following things:

  1. Every collection object has 3 methods that will allow you to transform its data to a string representation anyway you want. There are mkString, addString and stringPrefix. Do something clever with them (think "decompiling" your in-memory ADTs back to source-code form) and you shall arrive to step 2). Essentially, you can transform a list of integers created by List(1,2,3) back to a string "List(1,2,3)". For more basic literals like a simple string or integer, you'll need to pimp the built-in types using implicits to provide them with these toString (I'm overloading the term here) helper methods.
  2. Now you have your string representation, you can think about how to "interpret" or "evaluate" them. You will need an eval() function that create a new instance of a parser combinator that understands Scala's literals and reassemble the data structure for you.

Implementing this actually sounds fun. Don't forget to post back here if you've successfully implementing it. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文