What's that called?
What's the name of that operator/symbol/syntax/thing, for Scala.
General
Syntax | Example | Name |
---|---|---|
_* |
foo(myValues: _*) |
vararg expansion |
: |
def fun[A: Foo](a: A) |
context bound |
<: |
def fun[A <: B](a: A) |
upper bound |
>: |
def fun[A >: B](a: A) |
lower bound |
<% |
def fun[A <% B](a: A) |
view bound |
Operator | Example | Name | also known as... |
---|---|---|---|
== |
if (a == b) |
equals | |
>>= |
list1 >>= fun1 |
monad bind | flatMap |
>> |
list1 >> fun2 |
monad sequence | followedBy |
|@| |
(Some(3) |@| Some(5)) {_ + _} |
applicative builder | Cinnabon, Scream, Admiral Ackbar, Home Alone/Macaulay Culkin |
:: |
1 :: 2 :: 3 :: Nil |
cons |
SBT Keys
Operator | What?? |
---|---|
:= |
assign |
+= |
append |
++= |
append |
~= |
transform |
<<= |
compute |
<++= |
compute values then append |
--
General Usages
|@|
applicative builder
Using Cats:
import cats._
import cats.instances.option._
import cats.syntax.cartesian._
(Option(1) |@| Option(2)) map (_ + _)
// > res4: Option[Int] = Some(3)
::
cons
In standard 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)
>>=
monad bind
Using Cats:
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)
>>
monad sequence
This is very similar to a monad bind, except the result of the first action is discarded.
Using Cats:
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)
_*
vararg expansion
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
:=
assign key value
name := "fooproject"
The assignment operator can also be used to compute or transform values using the .value
macro.
organization := name.value
// or
organization := { "hello" + name.value }
+=
/ ++=
append key value
// 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
name ~= { _.toUpperCase }
<<=
compute new key value
name <<= (name, organization, version) { (n, o, v) => "project " + n + " from " + o + " version " + v }
<++=
compute values then append to key
// val watchSources: TaskKey[Seq[File]] = // ...
watchSources in ConfigGlobal <++= unmanagedSources
This is the same thing as:
watchSources in ConfigGlobal ++= unmanagedSources.value