Contents
- Akka Cheat Sheet
- Robot Cheat Sheet
- Scala Cheat Sheet (List handling)
Akka Cheat Sheet
Creating an actor system (usually one per machine)
val system = ActorSystem("cluster")
Defining an actor:
class MyActor(myArg1: String) extends Actor {
// initial actions here
...
def receive = {
case x: Robot => ...
}
}
Creating an instance of an actor with one constructor argument:
val myActorRef =
system.actorOf(Props(classOf[MyActor], "arg1"), "main")
Sending a message to an actor (ActorRef):
myActorRef ! "string message"
Configure your cluster node (use ipconfig/ifconfig to get your LAN ip address)
in src/main/resources/application.conf:
akka {
...
remote {
netty.tcp {
hostname = "X.X.X.X" # your LAN ip address
...
# add your own host and some other nodes to the initial cluster
cluster {
seed-nodes = [
"akka.tcp://cluster@X.X.X.X:110",
"akka.tcp://cluster@Z.Z.Z.Z:110"]
...
}
Creating a broadcast router actor:
val broadcastRouter = system.actorOf(Props.empty.withRouter(
ClusterRouterConfig(
BroadcastRouter(),
ClusterRouterSettings(totalInstances = 100, routeesPath = "/user/main", allowLocalRoutees = true, useRole = None))),
name = "router")
How to let an actor do something every 10 seconds:
import scala.concurrent.duration.FiniteDuration
import java.util.concurrent.TimeUnit
import scala.concurrent.ExecutionContext.Implicits._
context.system.scheduler.schedule(FiniteDuration(10, TimeUnit.SECONDS), FiniteDuration(10, TimeUnit.SECONDS)) {
self ! "every 10 seconds" // send message to myself every 10 seconds
}
Robot Cheat Sheet
Creating a random robot code
val robotCode = RobotCode.createRandomCode("your name here")
Creating a robot from a robot code
val robot = robotCode.evaluate
Performance optimization: Evaluating a list of robot codes using multiple cores:
import scala.concurrent.ExecutionContext.Implicits._ val myRobots = myRobotCodes.par.map(_.evaluate)
Print some debug info about a collection of robots
robot.DebugHelper.print(candidates: Seq[Robot])
Scala Cheat Sheet (List handling)
Concatenate two lists
val both = list1 ++ list2
Shuffling a list
import scala.util.Random val shuffled = Random.shuffle(List(1, 2, 3))
Get a random number between 0 and 100 (not including 100):
Random.nextInt(100)
Sorting a list of objects by a numeric property
val sorted = myList.sortBy(_.myProperty)
Take first 100 elements of a list
val part = list.take(100)
Take everything except first 100 elements of a list
val part = list.drop(100)