What's the name of that operator/symbol/syntax/thing, for Scala.
Syntax | Example | Name |
---|---|---|
_* |
foo(myValues: _*) |
vararg expansion |
: |
def fun[A: Foo](a: A) |
context bound |
<: |
def fun[A <: B](a: A) |
lower bound |
>: |
def fun[A >: B](a: A) |
upper 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) |
:: |
1 :: 2 :: 3 :: Nil |
cons |
Operator | What?? |
---|---|
:= |
assign |
+= |
append |
++= |
append |
~= |
transform |
<<= |
compute |
<++= |
compute values then append |
Using Cats:
import cats._
import cats.std.option._
import cats.syntax.cartesian._
(Option(1) |@| Option(2)) map (_ + _)
// > res4: Option[Int] = Some(3)
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)
Using Cats:
import cats.std.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:
import cats.std.all._
import cats.syntax.flatMap._
List(1, 2, 3) >> { List(2, 2) }
// > res0: List[Int] = List(2, 2, 2, 2, 2, 2)
def foo(args: String*) = args.map(_.length)
val input = List("hello", "world")
foo(input: _*)
// > res0: Seq[Int] = List(5, 5)
TODO
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 }
// 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"))
name ~= { _.toUpperCase }
name <<= (name, organization, version) { (n, o, v) => "project " + n + " from " + o + " version " + v }
// val watchSources: TaskKey[Seq[File]] = // ...
watchSources in ConfigGlobal <++= unmanagedSources
This is the same thing as:
watchSources in ConfigGlobal ++= unmanagedSources.value