Skip to content

Instantly share code, notes, and snippets.

class NumericColumns(TransformerMixin):
def __init__(self):
self.columns = [column names]
def fit(self, X):
cols = []
for col in self.columns:
try:
X[col].astype('float')
@nsadeh
nsadeh / BasicWebServer.scala
Last active August 8, 2020 22:59
Basic Web Server in Akka HTTP/Scala
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import scala.io.StdIn
object WebServer extends App {
// implicit values required by the server machinery
implicit val actorSystem = akka.actor.ActorSystem("messaging-actorsystem")
@nsadeh
nsadeh / BasicWebSocketsServer.scala
Last active August 9, 2020 17:07
Basic Web Server with an Affirming WebSockets endpoint
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import scala.io.StdIn
import akka.stream.scaladsl.Flow
import akka.http.scaladsl.model.ws.{ Message, TextMessage }
object WebServer extends App {
@nsadeh
nsadeh / ChatSessionMap.scala
Last active August 8, 2020 23:12
Record of chat sessions
import akka.actor.typed.{ ActorSystem, SpawnProtocol }
object ChatSessionMap {
// mutable data structure holding all the chat sessions. Definition to follow
private var sessions: Map[String, ChatSession] = Map.empty[String, ChatSession]
// only function of this class is to keep track of chat sessions
def findOrCreate(userId: String)(implicit system: ActorSystem[SpawnProtocol.Command]): ChatSession = sessions.getOrElse(userId, create(userId))
@nsadeh
nsadeh / ChatSessionActor.scala
Created August 8, 2020 23:24
Describes a chat session
import akka.actor.typed.{ Behavior, SpawnProtocol, ActorRef }
import akka.actor.typed.scaladsl.Behaviors
object SessionActor {
// protocol - see gist below
import CoreChatEvents._
import WebSocketsEvents._
def receive(websocket: Option[ActorRef[WebSocketsEvent]]): Behavior[CoreChatEvent] = Behaviors.receiveMessage {
import akka.actor.typed.ActorRef
object CoreChatEvents {
import WebSocketsEvents._
sealed trait CoreChatEvent
final case class UserMessage(message: String, phoneNumber: String) extends CoreChatEvent
final case class SMSMessage(sender: String, message: String) extends CoreChatEvent
final case class Connected(websocket: ActorRef[WebSocketsEvent]) extends CoreChatEvent
final case object Disconnected extends CoreChatEvent
import scala.concurrent.{ Future, ExecutionContext }
import akka.actor.typed.{ ActorRef, ActorSystem, SpawnProtocol, Props, DispatcherSelector }
import akka.actor.typed.SpawnProtocol.Spawn
import akka.http.scaladsl.model.ws.{ TextMessage, Message }
import akka.stream.{ FlowShape, OverflowStrategy }
import akka.stream.scaladsl._
import akka.stream.typed.scaladsl.{ ActorSink, ActorSource }
import akka.util.Timeout
implicit val spawnSystem = ActorSystem(SpawnProtocol(), "spawn")
def messageRoute =
// pattern matching on the URL
pathPrefix("message" / Segment) { trainerId =>
// await on the webflow materialization pending session actor creation by the spawnSystem
Await.ready(ChatSessionMap.findOrCreate(trainerId).webflow(), Duration.Inf).value.get match {
case Success(value) => handleWebSocketMessages(value)
case Failure(exception) =>
println(exception.getMessage())
@nsadeh
nsadeh / EventSubscription.scala
Last active February 5, 2022 23:33
Subscribing to events from another aggregate
// defining the parent entity
object Parent {
sealed trait Command
sealed trait Event
final case class State(...)
val empty: State = ???
def apply(parentId: String) = EventSourcedBehavior(
persistenceId = PersistenceId(parentId),
emptyState = empty,
@nsadeh
nsadeh / Makefile
Last active February 18, 2024 17:43
Elm <> Tailwind Makefile with elm-watch
PATH := $(PWD)/node_modules/.bin:$(PATH)
SHELL := /bin/bash
# Assumes the following structure:
# source-dir
# - public
# - styles.css (main CSS file with Tailwind directives)
# - index.html (entry index.html with css and javascript links
# - index.js (main JS file that inits Elm app and implements ports
# - src/App.elm (elm entry file)