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 / 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
date cases county
02-01-2020 1 Orange
02-02-2020 0 Orange
02-03-2020 0 Orange
02-04-2020 0 Orange
02-05-2020 0 Orange
02-06-2020 0 Orange
02-07-2020 0 Orange
02-08-2020 0 Orange
02-09-2020 0 Orange
@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));
});
public class Event {
@PartitionKey(0) public UUID accountId
@PartitionKey(1)public String yearMonthDay;
@ClusteringKey public UUID eventId;
//other data...
}
public static void sampleUsage() {
//we want to ONLY query data from three years ago for a set of accounts, so we will generate that somehow.
//Also note that one token will likely generate many Events...
object Test {
import scalaz.\/
import scala.concurrent.{ExecutionContext, Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scalaz.syntax.std.option._
def flipOption[A](l: List[Option[A]]): Option[List[A]] =
l.foldLeft(List[A]().some)
((accumulator: Option[List[A]], optiona: Option[A]) => accumulator.flatMap((la: List[A]) => optiona.map((a: A) => a :: la)))
import scalaz._
import Scalaz._
import IndexedStateT._
type ParserM[S, A] = EitherT[({ type l[a] = StateT[Trampoline, S, a]})#l, String, A]
def getVerbose[S]: ParserM[S, S] = {
//https://github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/MonadState.scala
val monadState = stateTMonadState[S, Trampoline]
val state: StateT[Trampoline, S, S] = monadState.get
As a past and future speaker of LambdaConf, I wanted to share my thoughts on the Curtis Yarvin controversy. I do not agree
with excluding attendees nor speakers because of their beliefs or writings. However, it has become clear to me that Curtis Yarvin
has been and continues to be disingenuous when it comes to his claim of separating himself from his 'Moldbug' persona. This, combined
with his large sphere of influence in a repgunant political movement leads me to believe having Curtis at LambdaConf will detract from
the overall goal of spreading functional programming as well as reaching under represented groups. I do not support Lambda Conf's
decision to allow Curtis Yarvin to speak in 2016. I coninue to support LambdaConf's overall mission of inclusion, am happy to see they
continue to offer divirsity scholarships, and plan on speaking in 2016 despite Curtis Yarvin's attendance.
@vmarquez
vmarquez / SeparateTasks.scala
Last active January 13, 2016 05:23
SeparateTasks.scala
def taskSeparation[F[_], A](fa: List[F[A]])(implicit M: Catchable[F], N: Nondeterminism[F]): F[(List[Throwable], List[A])] =
N.gatherUnordered(fa.map(f => M.attempt(f))).map(_.separate)
@vmarquez
vmarquez / Writer.cs
Created December 1, 2013 20:48
Writer Monad in C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace XSharpx {
public struct Writer<A, B>
{
public readonly A a;
public readonly B b;
public readonly Semigroup<A> semigroup;
def liftT[G[_[_], _], M[_]: Monad, A](a: M[A])(implicit ML: MonadLiftT[G, F], M: Monad[M]): G[M, A] = ML.liftT[M, A](M.map(a)(aa => point(aa)))