Skip to content

Instantly share code, notes, and snippets.

View samgj18's full-sized avatar
🦒

Samuel samgj18

🦒
View GitHub Profile
use crate::{
domain::{Error, Unit},
service::Cache as CacheConfig,
};
use async_trait::async_trait;
use deadpool_redis::{
redis::{
cmd, from_redis_value, AsyncCommands, FromRedisValue, JsonAsyncCommands, RedisError,
RedisResult, RedisWrite, ToRedisArgs,
},
version: "3.9"
services:
postgres:
user: root
image: postgres:16.1-alpine
restart: unless-stopped
networks:
- absenty
ports:
@samgj18
samgj18 / DirectoryIterator.rs
Created October 22, 2023 12:00
Solution: Safe FFI Wrapper
// Exercise: https://google.github.io/comprehensive-rust/exercises/day-3/safe-ffi-wrapper.html
mod ffi {
use std::os::raw::{c_char, c_int};
#[cfg(not(target_os = "macos"))]
use std::os::raw::{c_long, c_uchar, c_ulong, c_ushort};
// Opaque type. See https://doc.rust-lang.org/nomicon/ffi.html.
#[repr(C)]
pub struct DIR {
@samgj18
samgj18 / NoughtsDeterminer.scala
Created May 2, 2023 22:43
Super convoluted NoughtsDeterminer in Scala, but cooler
object Main extends App {
// It is assumed that the board is always square, and that
// there's only one possible move to win the game.
import Board._
import syntax._
type Matrix = Array[Array[Board]]
val board2: Matrix = Array(
Array(X, O, Empty),
@samgj18
samgj18 / fibers.md
Created February 12, 2023 11:42 — forked from djspiewak/fibers.md

Fibers

Fibers are an abstraction over sequential computation, similar to threads but at a higher level. There are two ways to think about this model: by example, and abstractly from first principles. We'll start with the example.

(credit here is very much due to Fabio Labella, who's incredible Scala World talk describes these ideas far better than I can)

Callback Sequentialization

Consider the following three functions

@samgj18
samgj18 / EventStoreDB.yml
Last active March 1, 2024 23:18
EventStoreDB docker compose for M1 Mac
version: "3.8"
services:
eventstore.db:
restart: always
image: "ghcr.io/eventstore/eventstore:23.6.0-alpha-arm64v8"
environment:
- EVENTSTORE_CLUSTER_SIZE=1
- EVENTSTORE_RUN_PROJECTIONS=All
- EVENTSTORE_START_STANDARD_PROJECTIONS=true
- EVENTSTORE_EXT_TCP_PORT=1113
// Contravariant Functors
//Formal definition says that a F[_] functor is contravariant if, instead of having the map method, it has a contramap method defined:
trait Contravariant[F[_]] {
def contramap[A, B](fA: F[A])(f: B => A): F[B]
}
// Example:
type Comparison = Int
val Greater = 1
sealed trait MyList[A]
object MyList {
def empty[A]: MyList[A] = Empty()
/// A* means zero or more (variadic)
def apply[A](head: A, tail: A*): MyList[A] =
(head +: tail).foldRight(empty[A])(Cons(_, _))
case class Empty[A]() extends MyList[A]
case class Cons[A](head: A, tail: MyList[A]) extends MyList[A]
case class Column[A](name: String)
object Column {
def string(name: String): Column[String] = Column[String](name)
def int(name: String): Column[Int] = Column[Int](name)
}
// Type arguments are unchecked because they are eliminated by erasure at runtime
def select[A](c: Column[A]): A = c match {
override def mergeSort[S >: T](implicit ordering: Ordering[S]): RList[S] = {
/**
* [3, 1, 2, 5, 4] => [[3], [1], [2], [5], [4]]
* merge([[3], [1], [2], [5], [4]], []) // Pick two elements
* merge ([[2], [5], [4]], [[1], [3]])
* merge ([[4]], [[2, 5], [1, 3]]) // Now there are no two elements to pick I just prepend
* merge ([], [[4], [2, 5], [1, 3]]) // Once getting to the empty list I call with swapped inputs
* merge ([[4], [2, 5], [1, 3]], [])
* merge ([[1, 3]], [[2, 4, 5]])