Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions _includes/bullet-pattern-matching.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<figure class="code pattern-matching" style="width: 500px; float: right;">
<figcaption>Pattern matching</figcaption>
<pre><code>// Define a set of case classes for representing binary trees.
sealed abstract class Tree[+A]
case class Node[+A](elem: A, left: Tree[A], right: Tree[A]) extends Tree[A]
case object Leaf extends Tree[Nothing]
sealed abstract class Tree
case class Node(elem: Int, left: Tree, right: Tree) extends Tree
case object Leaf extends Tree

// Return the in-order traversal sequence of a given tree.
def inOrder[A](t: Tree[A]): List[A] = t match {
def inOrder(t: Tree): List[Int] = t match {
case Node(e, l, r) => inOrder(l) ::: List(e) ::: inOrder(r)
case Leaf => List()
}</code></pre>
Expand All @@ -20,7 +20,7 @@ <h3>Switch on the structure of your data</h3>
ability to be deconstructed with <em>pattern matching</em>.</p>
<p>
In this example, we define a small set of case classes that represent binary
trees of a generic type <code>A</code>.
trees of integers (the generic version is omitted for simplicity here).
In <code>inOrder</code>, the <code>match</code> construct chooses the right
branch, depending on the type of <code>t</code>, and at the same time
deconstructs the arguments of a <code>Node</code>.
Expand Down