Scala: alternative List syntax (with square brackets, if possible)?

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.

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 ListMakerA(a0: A) { privatethis val buffer = List. NewBuilderA buffer += a0 def \(a: A) = { buffer += a; this } def \\ = buffer. Result } implicit def make_listsA(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: ListInt = List(1, 2, 3, 4, 5) scala> val be = 'a'\'b'\\ b: ListChar = List(a, b) scala> val c = false\true\false\false\false\false\true\\ c: ListBoolean = List(false, true, false, false, false, false, true) This uses exactly as many characters as brackets would. (It doesn't nest well, however. ).

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.

Define def lA(a:A*) = List(a:_*) Then you can do l(1,2,3) which is only one character more than 1,2,3.

Ah--you beat me by 9 seconds! :) – Rex Kerr May 30 at 3:24 3 My solution is clearly better because he will not have to use the shift key to type it. ;) – Kim Stebel May 30 at 3:26 But Rexs implementation with L being a function valued val is much nicer.

– Jens Schauder May 30 at 6:51 2 @Jens Schauder - It's not a function (i.e. Doesn't derive from Function1); it's just an alias to the List companion object. – Rex Kerr May 30 at 7:17 Correct.

Still nicer. – Jens Schauder May 30 at 9:57.

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 LMA(x: A) { | def \(y: A) = List(x,y) | } defined class LM scala> implicit def a2lA(x: A): LMA = new LM(x) a2l: A(x: A)LMA scala> class LXA(xs: ListA) { | def \(y: A) = xs:::List(y) | } defined class LX scala> implicit def l2lxA(xs: ListA): LXA = new LX(xs) l2lx: A(xs: ListA)LXA scala> 1\2 res0: ListInt = List(1, 2) scala> 1\2\3 res1: ListInt = List(1, 2, 3) scala.

Well, yes--but you're building up your list from the wrong end. That's why I didn't do it this way. Then again, maybe O(N^2) list creation is worth saving two more characters, as long as we're going that way :) – Rex Kerr May 30 at 7:14 It didn't work for me.Is it only for Scala 2.10?

– davips May 30 at 19:52 scala> 1\2 :12: error: type mismatch; found : Int required:? {val \(x$1:? >: Int(2) : Int(2).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions