Skip to content

Instantly share code, notes, and snippets.

View JohnMurray's full-sized avatar

John Murray JohnMurray

View GitHub Profile
template<BehaviorType T>
class RoundRobinRouter
: public Actor
, public T
{
const std::vector<actor_ref<T>> m_children;
std::vector<actor_ref<T>>::iterator m_it;
public:
RoundRobinRouter(std::vector<actor_ref<T>> const& actors)
void network_receive(MessageWrapper const& wrapper) {
if (wrapper.type_id == get_message_type_id<std::string>()) {
this->receive(*dynamic_cast<std::string *>(wrapper.value));
}
else if (wrapper.type_id == get_message_type_id<SerializableHello>()) {
this->receive(*dynamic_cast<SerializableHello *>(wrapper.value));
}
else {
// unknown
this->send_dead_letters(wrapper);
class SerializabelHello {};
class NonSerializableHello {};
class HelloBehavior: public Behavior {
virtual void receive(SerializableHello const& msg) = 0;
virtual void receive(NonSerializableHello const& msg) = 0;
};
class HelloActor: public Actor, public HelloBehavior { ... };
auto ref = make_actor<HelloActor>(system, "Greetings");
auto generic_ref = actor_ref_cast<HelloBehavior>(ref);
generic_ref.send("Bob"); // still works!
generic_ref.send(false); // still an error!
void main() {
auto system = ActorSystem{}
auto ref = make_actor<HelloActor>(system, "Greetings");
ref.send("Bob");
ref.send(false); // compile error!
}
class HelloBehavior: public Behavior {
virtual void receive(std::string const& msg) = 0;
};
class HelloActor
: public Actor
, public HelloBehavior {
std::string m_greeting;
public:
HelloActor(std::string const& greeting): m_greeting(greeting){};
function addX(x) {
return (n) => {
return n + x;
}
}
const addFive = addX(5);
addFive(10);
// returns 15
#include <algorithm>
#include <functional>
#include <vector>
int main() {
std::function<int(int)> addOne = [](int x) -> int {
return x + 1;
};
addOne(5);
const addOne = (x) => x + 1;
}
addOne(5);
// returns 6
// use lambda as a value
[1, 2, 3, 4, 5].map(addOne)
// returns [2, 3, 4, 5, 6]
package scratchspace
import java.time.LocalDateTime
import java.util.{Deque, LinkedList}
import scala.concurrent.{Future, Promise, TimeoutException}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
import java.util.concurrent.{ScheduledFuture, ScheduledThreadPoolExecutor, TimeUnit}