Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's start with the <strong>"Singleton pattern"</strong>:</p> <pre><code>object SomeSingleton //That's it </code></pre> <hr> <p>I would additionally propose the <strong>"Using-functions-of-higher-order pattern"</strong>. Instead of e. g. iterating through a collection by yourself you supply functions to the methods the classes provide.</p> <p>In Scala you basically say what you intend to do: </p> <pre><code>//declare some example class case class Person(name: String, age: Int) //create some example persons val persons = List(Person("Joe", 42), Person("Jane", 30), Person("Alice", 14), Person("Bob", 12)) //"Are there any persons in this List, which are older than 18?" persons.exists(_.age &gt; 18) // =&gt; Boolean = true //"Is every person's name longer than 4 characters?" persons.forall(_.name.length &gt; 4) // =&gt; Boolean = false //"I need a List of only the adult persons!" persons.filter(_.age &gt;= 18) // =&gt; List[Person] = List(Person(Joe,42), Person(Jane,30)) //"Actually I need both, a list with the adults and a list of the minors!" persons.partition(_.age &gt;= 18) // =&gt; (List[Person], List[Person]) = (List(Person(Joe,42), Person(Jane,30)),List(Person(Alice,14), Person(Bob,12))) //"A List with the names, please!" persons.map(_.name) // =&gt; List[String] = List(Joe, Jane, Alice, Bob) //"I would like to know how old all persons are all together!" persons.foldLeft(0)(_ + _.age) // =&gt; Int = 98 </code></pre> <p>Doing this in Java would have meant touching the elements of a collection yourself and mix your application logic with flow control code.</p> <p><a href="http://www.scala-lang.org/docu/files/collections-api/collections.html" rel="noreferrer">More information</a> about the Collection classes.</p> <hr> <p>This nice <a href="http://lamp.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf" rel="noreferrer">EPFL paper</a> about <strong>Deprecating the Observer Pattern</strong> might be of interest, too.</p> <hr> <p><strong><a href="http://lambda-the-ultimate.org/node/4039" rel="noreferrer">Typeclasses</a></strong> are one approach to structure common features of classes where inheritance doesn't really fit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload