Skip to content

Instantly share code, notes, and snippets.

View vmarquez's full-sized avatar

Vincent Marquez vmarquez

  • Irvine, CA
View GitHub Profile
@vmarquez
vmarquez / Polymorphism.scala
Last active December 23, 2015 09:49
Two implementations of a Design by Contract Cache interface. Parametric Polymorphism is more flexible than subtype polymorphism.
case class GenericCache[A[_,_], B, C](instance: A[B,C], getf: B=>C, putf: (B,C)=>Unit) {
def retrieve(b: B) = getf(b)
def insert(b: B, c: C) = putf(b,c)
}
//Notice how neither cache implementations have to even be aware of the existnace of the typeclass. much more flexible than inheritance
class FastCache[A,B] {
private var m = Map[A,B]() //excuse mutability for illustration purposes
def add(a: A, b: B): Unit = {
m = m + (a->b)
@vmarquez
vmarquez / TxMapTest.scala
Last active November 28, 2021 12:33
A mini STM if you will. I've made a'Transactional' map that mutates in a referentially transparent way.
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.CountDownLatch
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
object TxMapTest {
/*
* Example Usage
* We want to show two threads working with the same data source having both of their effects succeed
@vmarquez
vmarquez / ExampleFutureUsage.cs
Last active December 17, 2015 15:29
Usage for monadic Future in C#
using System;
using System.Threading;
using XSharpx;
using System.Collections;
using System.Net;
namespace TestProj
{
class MainClass
{
@vmarquez
vmarquez / BrokenFor.scala
Last active December 14, 2015 15:18
Broken Scala For Comprehension
package vsxmpp
import scalaz.EitherT
import scalaz._
import Scalaz._
case class ABC(s:String)
object ABC {
@vmarquez
vmarquez / ReaderMonad.cs
Last active February 20, 2020 20:30
This is an example of using the Reader Monad for Dependency Injection using LINQ in C#. I learned about this from Tony Morris and Rúnar Bjarnason's video here: http://www.youtube.com/watch?v=ECPGTUa1WAI To figure out the (slightly weird) SelectMany peculiarities I found http://mikehadlow.blogspot.com/2011/01/monads-in-c-4-linq-loves-monads.html
//Reader Monad and its extension class to give it SelectMany(bind/flatMap) capabilities for use in LINQ queries
public static class ReaderMonadExt
{
public static ReaderMonad<T, C> SelectMany<T, A, B, C>(this ReaderMonad<T, A> rm, Func<A, ReaderMonad<T, B>> bindf, Func<A, B, C> select)
{
return new ReaderMonad<T, C>(t =>
{
var a = rm.Run(t);
return select(a, bindf(a).Run(t));
});
@vmarquez
vmarquez / BombGameNoActors.scala
Created October 5, 2012 00:13
This is a simple 'mulit-player, concurrent' game that attempts to reduce the number of mutable variables to a minimum.
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import akka.dispatch.Future
import akka.dispatch.Promise
import scala.collection.JavaConversions._
import scala.collection.JavaConversions
import akka.dispatch.ExecutionContext
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicReference
@vmarquez
vmarquez / BombGame.scala
Created September 12, 2012 00:05
Simple multiplayer, concurrent 'game' written in scala trying to minimize mutability
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import akka.actor.Actor
import akka.dispatch.Future
import akka.actor.ActorSystem
import akka.actor._
import scala.collection.JavaConversions._
import scala.collection.JavaConversions
import akka.pattern._
import akka.util.Timeout