Scala: Selling Ideas for Hands

I want to provide a Scala presentation, and I want to do this by taking the application and developing it from something that uses the java icon for something that uses the power of Scala (features, pattern matching, implicit conversions, functional programming).

I am particularly interested in what demonstrates a design change, not syntactic sugar. Something where the Scala end code is obviously easier to maintain and extend.

So any ideas? (I do not ask you to give code examples, as well as rough ideas about which example to use and what design principles can be demonstrated).

+6
design scala functional-programming
source share
4 answers

A great example is the creation of a small interpreter for a dynamic mini-language.

A basic Java implementation requires a classic design pattern, while functional scala-approach can use many great functional idioms such as

  • case classes
  • pattern matching
  • higher order functions

or even monads to create very clean and understandable code.

Just compare

class Number implements Expression { private int number; public Number(int number) { this.number = number; } public int interpret(HashMap<String,Integer> variables) { return number; } } 

from

 case NumberLiteral(i) => Integer(i) 

See examples of the interpreter on the scala page.

+4
source share

To help you choose among functions and create some good code examples, here are some ideas:

  • try to stick with your audienceโ€™s business domain. Try to take an example (even the basics) from their regular applications.
  • try to guess the main language of your audience (java, perl, C ++) and compare the syntax in your examples.
  • take a look: Browse scala and Scala and select the features that you like and are more convenient for you.
  • try to remember your early steps with scala and situations when you were impressed (or puzzled).
+3
source share

I think you may be too ambitious in your field. Just showing people a new and unfamiliar syntax is likely to โ€œloseโ€ them a bit, so adding even more drastic changes can be a big step. Do not forget: you can always make a second presentation if the first is popular!

I did this a lot in my company some time ago; it was only when I gave the presentation that I suddenly realized how strange and incomprehensible some kind of scala syntax appeared for the audience (or, perhaps, it was my delivery style!). I found that the following went well:

  • Iterative approach - take one Java method (and not the whole program), and then convert it to a really dumb scala-like (type in the declarations and that's it). Now apply one set of scala permutations (e.g. type inference), followed by another (e.g. type aliases), followed by another (e.g. close), etc. The end result will probably be about a third of the Java code, readable and concise, which can be contrasted because people are now familiar with what is happening. It's amazing to what extent Java is just a mixed bunch of types and keywords when browsing compared to something like scala.

  • Take the time to explain all the helper functions - for example, look at sample templates in detail and how it works. Do not disable extractors or case x :: xs . I found that people are really interested in this, and that was so great!

  • A functional programming style takes time to dive into. It is unreasonable to make people immediately begin to understand this material. I have been programming in scala for over a year now and am still confused by some of them.

  • Using REPL is really cool for some low-level things, such as showing how everything in scala (including synchronized, try-catch , etc.) is a type expression.

When you justify the awesomeness of lower-level material, I hope you will annoy people with an appetite for more. (Last week, I looked at implementations of a triple operator with an a-scala stranger-colleague, and he found it somewhat confusing. Trying to present the whole application for a bunch of newbies is too much!)

+3
source share

The spelling correcter example was used to explain Python in DevBack Stackoverflow. A short scala implementation may be the beginning:

 import util.matching.Regex.MatchIterator val alphabet = 'a' to 'z' toArray def train(features : MatchIterator) = (Map[String, Int]() /: features)((m, f) => m + ((f, m.getOrElse(f, 0) + 1))) def words(text : String) = ("[%s]+" format alphabet.mkString).r.findAllIn(text.toLowerCase) val dict = train(words(io.Source.fromFile("big.txt").mkString)) def edits(s : Seq[(String, String)]) = (for((a,b) <- s; if b.length > 0) yield a + b.substring(1)) ++ (for((a,b) <- s; if b.length > 1) yield a + b(1) + b(0) + b.substring(2)) ++ (for((a,b) <- s; c <- alphabet if b.length > 0) yield a + c + b.substring(1)) ++ (for((a,b) <- s; c <- alphabet) yield a + c + b) def edits1(word : String) = edits(for(i <- 0 to word.length) yield (word take i, word drop i)) def edits2(word : String) = for(e1 <- edits1(word); e2 <-edits1(e1)) yield e2 def known(words : Seq[String]) = for(w <- words; found <- dict.get(w)) yield w def or[T](candidates : Seq[T], other : => Seq[T]) = if(candidates.isEmpty) other else candidates def candidates(word: String) = or(known(List(word)), or(known(edits1(word)), known(edits2(word)))) def correct(word : String) = ((-1, word) /: candidates(word))( (max, word) => if(dict(word) > max._1) (dict(word), word) else max)._2 List("osters", "musters", "mixters") map correct foreach println 

It demonstrates higher order functions, tuple support, regular expression support, call by name for expressions. There are no OO functions (type system, traits, objects, packages, and visibility). (I am aiming for a brief implementation.) But you can do this to implement OO. For example, you can add some features, such as Dictionary, Corrector, etc.

+1
source share

All Articles