https://github.com/andyscott/scala-whats-that-called
What do you call that?... for Scala
https://github.com/andyscott/scala-whats-that-called
guide scala
Last synced: about 2 months ago
JSON representation
What do you call that?... for Scala
- Host: GitHub
- URL: https://github.com/andyscott/scala-whats-that-called
- Owner: andyscott
- Created: 2016-03-29T17:36:40.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-11T17:38:21.000Z (over 7 years ago)
- Last Synced: 2025-02-27T12:23:44.694Z (2 months ago)
- Topics: guide, scala
- Homepage:
- Size: 8.79 KB
- Stars: 173
- Watchers: 7
- Forks: 13
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# What's that called?
What's the name of that operator/symbol/syntax/thing, for Scala.
### General
| Syntax | Example | Name
|------------|-------------------------|------------
| `_*` | `foo(myValues: _*)` | [vararg expansion](#vararg-expansion) |
| `:` | `def fun[A: Foo](a: A)` | [context bound](#scala-bounds) |
| `<:` | `def fun[A <: B](a: A)` | [upper bound](#scala-bounds) |
| `>:` | `def fun[A >: B](a: A)` | [lower bound](#scala-bounds) |
| `<%` | `def fun[A <% B](a: A)` | [view bound](#scala-bounds) || Operator | Example | Name | also known as...
|----------------|---------------------------------|---------------------------------------------|---
| `==` | `if (a == b)` | equals |
| `>>=` | `list1 >>= fun1` | [monad bind](#monad-bind) | flatMap
| `>>` | `list1 >> fun2` | [monad sequence](#monad-sequence) | followedBy
| `\|@\|` | `(Some(3) \|@\| Some(5)) {_ + _}` | [applicative builder](#applicative-builder) | [Cinnabon][cinnabon], [Scream][scream], [Admiral Ackbar][admiral-ackbar],
[Home Alone/Macaulay Culkin][home-alone]
| `::` | `1 :: 2 :: 3 :: Nil` | [cons](#cons) |[cinnabon]: https://en.wikipedia.org/wiki/Cinnabon
[scream]: https://en.wikipedia.org/wiki/The_Scream
[home-alone]: https://en.wikipedia.org/wiki/Home_Alone
[admiral-ackbar]: https://en.wikipedia.org/wiki/Admiral_Ackbar### SBT Keys
| Operator | What??
|----------|-------------------------
| `:=` | [assign](#sbt-key-assign)
| `+=` | [append](#sbt-key-appends)
| `++=` | [append](#sbt-key-appends)
| `~=` | [transform](#sbt-key-transform)
| `<<=` | [compute](#sbt-key-compute)
| `<++=` | [compute values then append](#sbt-key-compute-then-append)--
--### General Usages
#### `|@|` applicative builder
Using Cats:
```scala
import cats._
import cats.instances.option._
import cats.syntax.cartesian._(Option(1) |@| Option(2)) map (_ + _)
// > res4: Option[Int] = Some(3)
```In standard Scala:
```scala
val list1 = List(1, 2, 3)
// > list1: List[Int] = List(1, 2, 3)
val list2 = 1 :: 2 :: 3 :: Nil
// > list2: List[Int] = List(1, 2, 3)
```Using Cats:
```scala
import cats.instances.all._
import cats.syntax.flatMap._List(1, 2, 3) >>= { (x: Int) => List(x, x + 1) }
// > res0: List[Int] = List(1, 2, 2, 3, 3, 4)
```This is very similar to a monad bind, except the result of the
first action is discarded.Using Cats:
```scala
import cats.instances.all._
import cats.syntax.flatMap._List(1, 2, 3) >> { List(2, 2) }
// > res0: List[Int] = List(2, 2, 2, 2, 2, 2)
``````scala
def foo(args: String*) = args.map(_.length)
val input = List("hello", "world")foo(input: _*)
// > res0: Seq[Int] = List(5, 5)
```#### `:` `<:` `>:` `<%` context/lower/upper/view bounds
TODO
--
--### SBT Key Usages
```scala
name := "fooproject"
```The assignment operator can also be used to compute or transform values using the `.value` macro.
```scala
organization := name.value
// or
organization := { "hello" + name.value }
```#### `+=` / `++=` append key value
```scala
// appending one item to a key
sourceDirectories in Compile += new File("source")
// appending several items to a key
sourceDirectories in Compile ++= Seq(file("sources1"), file("sources2"))
```
#### `~=` transform key value```scala
name ~= { _.toUpperCase }
```#### `<<=` compute new key value
```scala
name <<= (name, organization, version) { (n, o, v) => "project " + n + " from " + o + " version " + v }
```#### `<++=` compute values then append to key
```scala
// val watchSources: TaskKey[Seq[File]] = // ...
watchSources in ConfigGlobal <++= unmanagedSources
```
This is the same thing as:
```scala
watchSources in ConfigGlobal ++= unmanagedSources.value
```