Sha256: d10e02e0448e76bce2ae2ef2949d934d9f68c80d5272ddc807a552306b6bdb36

Contents?: true

Size: 1.75 KB

Versions: 370

Compression:

Stored size: 1.75 KB

Contents

## Hints
`Option` is used to indicate a computation that may possibly have no useful result
(for example due to an error or invalid input).
If you are unfamiliar with `Option` you may read [this tutorial](http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html).
`Option` is a so-called [Monad](https://en.wikipedia.org/wiki/Monad_(functional_programming)) which covers a "computational aspect", in this case possible absence of a value.
Proper use of Monads can result in very concise yet elegant
and readable code. Improper use can easily result in the contrary.
Watch [this video](https://www.youtube.com/watch?v=Mw_Jnn_Y5iA) to learn more.
#### Common pitfalls that you should avoid
There are a few rules of thumbs for `Option`:
1. If you don't need it don't use it. Instead of
```scala
def add1(x: Int): Option[Int] = Some(x + 1)
```
better have
```scala
def add1(x: Int): Int = x + 1
```
(there is `Option.map` to apply such simple functions,
so you don't have to clutter them with `Option`).
2. Don't "unwrap" if you don't really need to.
Often there are built-in functions for your purpose. Indicators of premature
unwrapping are `isDefined/isEmpty` or pattern matching. Instead of
```scala
val x: Option[Int] = ...

if (x.isDefined) x.get + 1 else 0
// or
x match {
  case Some(n) => n + 1
  case None => 0
}
```
better have
```scala
x map (_ + 1) getOrElse 0
```
3. Monads can be used inside a for-comprehension FTW.
This is advisable when you want to "compose" several `Option` instances. Instead of
```scala
val xo: Option[Int] = ...
val yo: Option[Int] = ...
val zo: Option[Int] = ...

xo.flatMap(x =>
  yo.flatMap(y =>
    zo.map(z =>
	  x + y + z)))
```
better have
```scala
for {
  x <- xo
  y <- yo
  z <- zo
} yield x + y + z
```

Version data entries

370 entries across 370 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.158 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.157 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.156 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.155 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.154 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.153 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.152 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.151 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.150 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.149 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.148 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.147 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.146 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.145 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.144 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.143 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.142 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.141 tracks/scala/exercises/hamming/.meta/hints.md
trackler-2.2.1.140 tracks/scala/exercises/hamming/.meta/hints.md