Examples of creative and useful use of the operator in Scala

After discussing with a friend about operator overload. I am considering a creative and / or useful example of using operator overloading in Scala. I have interesting illustrations:

  • Mathematical constructions such as linear algebra or complex numbers ( + * - )
  • Shell simulation and redirection ( | > < >> )
  • Alternative grammar expressions ( | )

Do you have other nice examples?

+7
source share
4 answers

I don't mind using it from time to time:

  • General-purpose pipe |> (for example, someValue |> println )
  • := some structures, such as observable value holders to simulate assignment
  • Data binding in user interface programming using <=> (two-way), <== (one-way) (e.g. field.enabled <=> options.isEditable
+7
source

I find useful two “built-in” statements in Scala: :: and -> .

In creating maps, you can write Map("a" -> 1) and Scala translate "a" -> 1 into a tuple ("a", 1) . :: used to add before the list, it is also very convenient.

Also the += and -= operators for collections are great, especially because they apply to both mutable and immutable.

+4
source

This is a bit of a controversial topic in the scala community. On the one hand, people are afraid of abuse and can lead to incomprehensible programs. If you think seriously about it, it already exists. Because what kind of operators feel “natural?” So let's take the mathematical operators: + - * / So, why is' defined in the Lines in most programming languages ​​+?

On the other hand, well-known operators can lead to shorter and more understandable codes. Scala supports Unicode characters, so you can use characters like / u + 2211. Example: List (1,2,3) .sum // would give us 6, using the sum instead of / u + 2211, because there is no Unicode keyboard support.

So, instead of 3 characters (s, u, m) we have one (/ u + 2211). Is it good or bad? In my opinion, the scala community will find common sense in this topic, but it will take some time.

Other programming languages ​​that support Unicode expressions include fortress.

+2
source

Some time ago, I showed how to create DSLs with statements to manage JCR nodes and properties. If you correctly lay out the code, it resembles an ASCII drawing for the actual content tree. Next Scala code

 root ¦- "movies" -+ { n: Node => n ¦= ("title", "Night on Earth") n ¦= ("length", 123L) n ¦= ("ratings", 9L::8L::5L::Nil) n ¦= ("languages", "en"::"it"::"fi"::"fr"::Nil) n ¦- "cast" -+ { n: Node => n ¦= ("Gena Rowlands", "Victoria Snelling") n ¦= ("Winona Ryder", "Corky") n ¦= ("Roberto Benigni", "Taxi Driver") }} 

equivalent to this version in Java:

  Node movies = root.addNode("movies"); movies.setProperty("title", "Night on Earth"); movies.setProperty("length", 123L); movies.setProperty("ratings", new String[]{"9", "8", "5"}, PropertyType.LONG); movies.setProperty("languages", new String[]{"en", "it", "fi", "fr"}, PropertyType.STRING); Node cast = movies.addNode("cast"); cast.setProperty("Gena Rowlands", "Victoria Snelling"); cast.setProperty("Winona Ryder", "Corky"); cast.setProperty("Roberto Benigni", "Taxi Driver"); 

Despite the fact that it is not always useful, it may deserve attention for creativity.

+2
source

All Articles