Scala:替代列表语法(如果可能的话,带方括号)
Scala 中有替代的“列表”语法吗? 是否可以定义一个名为“[”和“]”的附加类/类型/运算符*?
我知道“方括号”用于指示类型,但它们非常适合声明列表的重复任务。
一个“;”或者 '?'作为最后的资源也很好。
谢谢
观察: 经过多次搜索,我发现的唯一替代方法是使用“cons”:
val list = 1 :: 2 :: 3 :: Nil
但它根本不会减少任何按键输入。
- 我仍在 Scala 中学习这些东西
编辑: 只是澄清一下:就我而言,性能不是优先考虑的事情。是的,转变是不受欢迎的。 :P 幕后动机:我喜欢 Haskell 风格,但不能直接在 Java 中使用它。
编辑2: 基于
实现对象类型的 Rex Kerr 解决方案的最终解决方案:
package a
object Types {
type \[z] = List[z]
implicit def make_lists[A](z: A) = new ListMaker(z)
class ListMaker[A](a0: A) {
private[this] val buffer = List.newBuilder[A]
buffer += a0
def \(z: A) = {
buffer += z;
this
}
def \\ = buffer.result
}
}
使用对象类型:
package a
import a.Types._
object Whatever {
def listInListOut (l: \[Int]) = {
1\2\\
}
}
is there an alternative 'List' syntax in Scala?
Is it possible to define one aditional class/type/operator* called '[' and ']'?
I know 'square brackets' are used to indicate Type, but they are perfect to the repetitive task of declaring lists.
A ';' or '?' would be good also, as a last resource.
Thanks
obs.:
after much search the only alternative I found was to use 'cons':
val list = 1 :: 2 :: 3 :: Nil
but it doesn't reduce any key typing at all.
- I am still learning those things in Scala
EDIT:
Just to clarify: Performance is not a priority in my case. And yes, shift is not welcome. :P
Motivation behind the scenes: I like Haskell style, but cannot use it directly with Java.
EDIT 2:
Final solution based on both Rex Kerr solutions
implementing object Types:
package a
object Types {
type \[z] = List[z]
implicit def make_lists[A](z: A) = new ListMaker(z)
class ListMaker[A](a0: A) {
private[this] val buffer = List.newBuilder[A]
buffer += a0
def \(z: A) = {
buffer += z;
this
}
def \\ = buffer.result
}
}
using object Types:
package a
import a.Types._
object Whatever {
def listInListOut (l: \[Int]) = {
1\2\\
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
[
和]
是 Scala 中的保留符号,用于类型注释。您不能将它们用于列表。;
保留用于行尾。在很多情况下你可以使用?
,但这会很尴尬。我建议您学习使用
::
表示法(并习惯于连续两次快速键入:
符号),因为它确实使列表操作在视觉上更加清晰,而且这是一个很好的语法提醒,列表很奇怪,因为你把东西放在列表的头部。但是,如果您不能容忍这一点,最好的选择可能是定义一个字母列表符号。例如,
是从 1 到 4 的数字列表。如果您只需键入
L
而不是List
会怎样?事实证明您可以,因为这不是一个花哨的构造函数或静态方法,而是类List
的单例伴生对象。所以你只是比你的括号建议更糟糕的一个角色。
[
and]
are reserved symbols in Scala which are used for type annotations. You can't use them for lists.;
is reserved for end of line. You could use?
in many cases, but it would be awkward.I recommend that you learn to use the
::
notation (and get used to typing the:
symbol fast twice in succession) because it really makes the list operations visually clear, plus it is a great syntactic reminder that lists are weird because you put things on the head of the list.However, if you cannot tolerate this, your best option is probably to define a one-letter list symbol. For example,
is a list of the numbers from 1 to 4. What if you could just type
L
instead ofList
? It turns out that you can, since this is not a fancy constructor or static method, but a singleton companion object to the classList
. So you justand you are just one character worse off than your suggestion of brackets.
定义
然后你可以
只比
[1,2,3]
多一个字符Define
Then you can do
which is only one character more than
[1,2,3]
如果你真的讨厌 Shift 键并且不在乎其他人是否能理解你的代码,我忍不住指出另一种方法到这里来获取所有元素都是相同类型的列表:
现在你可以列出你内心的想法内容,无需触摸 Shift 键!
这使用了与括号一样多的字符。 (然而,它的嵌套不太好。)
I can't help pointing out another way to go here for lists where all the elements are the same type, if you really hate the shift key and don't care if other people can understand your code:
Now you can list to your heart's content, without ever touching the shift key!
This uses exactly as many characters as brackets would. (It doesn't nest well, however.)
不完全是替代语法,但它是迄今为止最可移植的解决方案:
在 Intellij IDEA 中,可以创建“实时模板”;
按 Ctrl+Alt+s;搜索“模板”;转到“实时模板”部分;
只需在 Scala 条目中添加一个名为“l”的新项目,添加随机描述和以下代码:
按 Enter,转到编辑器,按 L,然后按 Tab。
这是您打字的痛苦的结束。
对数组执行相同的操作。
Not exactly an alternative syntax, but it is by far the most portable solution:
In Intellij IDEA it is possible to create "Live Templates";
press Ctrl+Alt+s; search for "template"; go to "Live Templates" section;
just add one new item named "l" inside Scala entry, add a random description and the following code:
Press Enter, go to the editor, press L followed by Tab.
It is the end of your typing pains.
Do the same for Arrays.