Scala:替代列表语法(如果可能的话,带方括号)

发布于 2024-11-10 09:42:45 字数 897 浏览 4 评论 0原文

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 技术交流群。

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

发布评论

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

评论(5

柠檬色的秋千 2024-11-17 09:42:45

[] 是 Scala 中的保留符号,用于类型注释。您不能将它们用于列表。 ; 保留用于行尾。在很多情况下你可以使用?,但这会很尴尬。

我建议您学习使用 :: 表示法(并习惯于连续两次快速键入 : 符号),因为它确实使列表操作在视觉上更加清晰,而且这是一个很好的语法提醒,列表很奇怪,因为你把东西放在列表的头部。

但是,如果您不能容忍这一点,最好的选择可能是定义一个字母列表符号。例如,

List(1,2,3,4)

是从 1 到 4 的数字列表。如果您只需键入 L 而不是 List 会怎样?事实证明您可以,因为这不是一个花哨的构造函数或静态方法,而是类 List 的单例伴生对象。所以你只是

val L = List
L(1,2,3,4)

比你的括号建议更糟糕的一个角色。

[ 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,

List(1,2,3,4)

is a list of the numbers from 1 to 4. What if you could just type L instead of List? It turns out that you can, since this is not a fancy constructor or static method, but a singleton companion object to the class List. So you just

val L = List
L(1,2,3,4)

and you are just one character worse off than your suggestion of brackets.

一向肩并 2024-11-17 09:42:45

定义

def l[A](a:A*) = List(a:_*)

然后你可以

l(1,2,3)

只比 [1,2,3] 多一个字符

Define

def l[A](a:A*) = List(a:_*)

Then you can do

l(1,2,3)

which is only one character more than [1,2,3]

鲸落 2024-11-17 09:42:45

如果你真的讨厌 Shift 键并且不在乎其他人是否能理解你的代码,我忍不住指出另一种方法到这里来获取所有元素都是相同类型的列表:

class ListMaker[A](a0: A) {
  private[this] val buffer = List.newBuilder[A]
  buffer += a0
  def \(a: A) = { buffer += a; this }
  def \\ = buffer.result
}
implicit def make_lists[A](a: A) = new ListMaker(a)

现在你可以列出你内心的想法内容,无需触摸 Shift 键!

scala> val a = 1\2\3\4\5\\
a: List[Int] = List(1, 2, 3, 4, 5)

scala> val b = 'a'\'b'\\
b: List[Char] = List(a, b)

scala> val c = false\true\false\false\false\false\true\\
c: List[Boolean] = List(false, true, false, false, false, false, true)

这使用了与括号一样多的字符。 (然而,它的嵌套不太好。)

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:

class ListMaker[A](a0: A) {
  private[this] val buffer = List.newBuilder[A]
  buffer += a0
  def \(a: A) = { buffer += a; this }
  def \\ = buffer.result
}
implicit def make_lists[A](a: A) = new ListMaker(a)

Now you can list to your heart's content, without ever touching the shift key!

scala> val a = 1\2\3\4\5\\
a: List[Int] = List(1, 2, 3, 4, 5)

scala> val b = 'a'\'b'\\
b: List[Char] = List(a, b)

scala> val c = false\true\false\false\false\false\true\\
c: List[Boolean] = List(false, true, false, false, false, false, true)

This uses exactly as many characters as brackets would. (It doesn't nest well, however.)

逆光飞翔i 2024-11-17 09:42:45

不完全是替代语法,但它是迄今为止最可移植的解决方案:
在 Intellij IDEA 中,可以创建“实时模板”;
按 Ctrl+Alt+s;搜索“模板”;转到“实时模板”部分;
只需在 Scala 条目中添加一个名为“l”的新项目,添加随机描述和以下代码:

List($END$)

按 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:

List($END$)

Press Enter, go to the editor, press L followed by Tab.
It is the end of your typing pains.
Do the same for Arrays.

标点 2024-11-17 09:42:45
Welcome to Scala version 2.10.0.r24777-b20110419020105 (Java HotSpot(TM) Client VM, Java 1.6.0
Type in expressions to have them evaluated.
Type :help for more information.

scala> class LM[A](x: A) {
     |   def \(y: A) = List(x,y)
     | }
defined class LM

scala> implicit def a2l[A](x: A): LM[A] = new LM(x)
a2l: [A](x: A)LM[A]

scala> class LX[A](xs: List[A]) {
     |   def \(y: A) = xs:::List(y)
     | }
defined class LX

scala> implicit def l2lx[A](xs: List[A]): LX[A] = new LX(xs)
l2lx: [A](xs: List[A])LX[A]

scala> 1\2
res0: List[Int] = List(1, 2)

scala> 1\2\3
res1: List[Int] = List(1, 2, 3)

scala>
Welcome to Scala version 2.10.0.r24777-b20110419020105 (Java HotSpot(TM) Client VM, Java 1.6.0
Type in expressions to have them evaluated.
Type :help for more information.

scala> class LM[A](x: A) {
     |   def \(y: A) = List(x,y)
     | }
defined class LM

scala> implicit def a2l[A](x: A): LM[A] = new LM(x)
a2l: [A](x: A)LM[A]

scala> class LX[A](xs: List[A]) {
     |   def \(y: A) = xs:::List(y)
     | }
defined class LX

scala> implicit def l2lx[A](xs: List[A]): LX[A] = new LX(xs)
l2lx: [A](xs: List[A])LX[A]

scala> 1\2
res0: List[Int] = List(1, 2)

scala> 1\2\3
res1: List[Int] = List(1, 2, 3)

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