Skip to content

Instantly share code, notes, and snippets.

Avatar

John Murray JohnMurray

View GitHub Profile
View router_actor.cpp
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)
View generated_network_receive.cpp
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);
View (de)serialization.cpp
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 { ... };
View actor_ref_cast.cpp
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!
View create_and_send.cpp
void main() {
auto system = ActorSystem{}
auto ref = make_actor<HelloActor>(system, "Greetings");
ref.send("Bob");
ref.send(false); // compile error!
}
View defining_the_actor.cpp
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){};
View cpp-vs-js-lambda-2.js
function addX(x) {
return (n) => {
return n + x;
}
}
const addFive = addX(5);
addFive(10);
// returns 15
View cpp-vs-js-lambda-1.cc
#include <algorithm>
#include <functional>
#include <vector>
int main() {
std::function<int(int)> addOne = [](int x) -> int {
return x + 1;
};
addOne(5);
View cpp-vs-js-lambda-1.js
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]
View ratelimit.scala
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}