title: Storing a value without using val
body: <p>Suppose have 3 numbers:</p>
  
  <pre><code>val x = 10
  val y = 5
  val z = 14
  </code></pre>
  
  <p>and we want to do some logic like:</p>
  
  <pre><code>if (x + y &gt; z) {
    println(x + y)
  } else if (x + y &lt; z) {
    println(-1)
  } else {
    println(0)
  }
  </code></pre>
  
  <p>If our "z + y" operation is expensive we must calculate it exactly once:</p>
  
  <pre><code>val sum = x + y
  
  if (sum &gt; z) {
    println(sum)
  } else if (sum &lt; z) {
    println(-1)
  } else {
    println(0)
  }
  </code></pre>
  
  <p>but I want more functional way like:</p>
  
  <pre><code>if (x + y &gt; z) =&gt; sum { //This is will not compile :)
    println(sum)
  } else if (sum &lt; z) {
    println(-1)
  } else {
    println(0)
  }
  </code></pre>
  
  <p>Something without needing another statement to store the result.
  Something that I can compound with another function, like:</p>
  
  <pre><code>if(x + y &gt; z) sum {
    if(sum + 10 &gt; 100) other_sum {
  ... etc
  </code></pre>
  
  <p>PS. Match does not help:</p>
  
  <pre><code>x + y match {
    case result if result &gt; z =&gt; println(result)
    case result if result &lt; z =&gt; println(-1)
    case _ =&gt; println(0)
  }
  </code></pre>
  
  <p>or </p>
  
  <pre><code>val sum = x + y
  sum match {
    case _ if sum &gt; z =&gt; println(sum)
    case _ if sum &lt; z =&gt; println(-1)
    case _ =&gt; println(0)
  }
  </code></pre>
  
  <p>It still looks bad.</p>