如何在scala中编写可读的字符串
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是序列化的意义所在。另外,为什么 JSON 很受欢迎。
That's what Serialization is about. Also, why JSON is popular.
查看 lift-json ( https://github. com/lift/lift/tree/master/framework/lift-base/lift-json/ )用于写出将由另一种语言解析和读取的数据。 JSON 是 Web 服务领域请求/响应序列化的相当标准,几乎每种语言都有 JSON 库。
要从字面上写出包含双引号的字符串,您还可以执行以下操作:
我发现像 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:
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.
我认为您正在寻找类似于 Javascript 的
eval()
+ JSON 以及 Python 的eval()
、str()
和repr 的东西()
。本质上,您需要 Lispy 对称元循环评估。这意味着您可以将数据转换为源代码,并评估该源代码并返回相同的数据,对吧?AFAIK,Scala 中没有等效的
eval()
。 Daniel Spiewak 之前此处讨论过这个问题。然而,如果你真的想要的话。我建议如下:mkString
、addString
和stringPrefix
。对它们进行一些巧妙的处理(想象将内存中的 ADT“反编译”回源代码形式),然后您将到达步骤 2)。本质上,您可以将List(1,2,3)
创建的整数列表转换回字符串"List(1,2,3)"
。对于更基本的文字(例如简单的字符串或整数),您需要使用隐式修饰内置类型,为它们提供这些 toString(我在这里重载了该术语)辅助方法。eval()
函数来创建解析器组合器的新实例,该解析器组合器可以理解 Scala 的文字并为您重新组装数据结构。实现这个实际上听起来很有趣。如果您已成功实施,请不要忘记在此处发帖。 :)
I think you are looking for something like Javascript's
eval()
+ JSON, and Python'seval()
,str()
andrepr()
. 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:mkString
,addString
andstringPrefix
. 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 byList(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 thesetoString
(I'm overloading the term here) helper methods.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. :)