Skip to content

Instantly share code, notes, and snippets.

View Fristi's full-sized avatar

Mark Fristi

View GitHub Profile
@Fristi
Fristi / gist:2384518
Created April 14, 2012 13:37
Autocomplete jQuery + RxJS + CoffeeScript
@searchBox.keyupAsObservable()
.select((ev) -> $(ev.target).val())
.where((text) -> text.length > 2)
.throttle(500)
.distinctUntilChanged()
.select(@searchItems)
.switchLatest()
.subscribe(@handleItemSearchAutoCompleteSuccess)
@Fristi
Fristi / AutofacScopedHubActivator.cs
Created October 25, 2012 19:16
Hub activation with metadata and decoupling of the hub deactivation
public class AutofacScopedHubActivator : IHubActivator
{
private readonly ILifetimeScope rootScope;
public AutofacScopedHubActivator(ILifetimeScope rootScope)
{
this.rootScope = rootScope;
}
public HubActivationResult Create(HubDescriptor descriptor)
@Fristi
Fristi / CsvParser.cs
Last active August 10, 2021 20:24
CSV Parser in C#, mapping via CSV headers and C# attributes.
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class CsvEntryAttribute : Attribute
{
public string HeaderName { get; set; }
}
public class CsvParser
{
private class CsvDescriptor
{
@Fristi
Fristi / SystemTime.cs
Last active December 14, 2015 10:59
Time windows in C# - A time window is a constrained period of time.
public class SystemTime
{
private static Func<DateTime> now = () => DateTime.Now;
public static Func<DateTime> Now
{
get { return now; }
set { now = value; }
}
}
module Utils =
let private rng = new Random()
let rec PickRandomPair<'TKey when 'TKey : equality> (pool:('TKey * int) list) =
let decreasePair(needle:'TKey, pool:('TKey * int) list) =
let decrease (key:'TKey, value:int) = if key = needle then (key, value - 1) else (key, value)
pool |> Seq.fold (fun acc r -> (decrease(r) :: acc)) []
module BaseTest =
[<TestFixture>]
[<AbstractClass>]
type public AggregateTest<'TState, 'TCommand, 'TEvent>() =
abstract member For: Aggregate<'TState, 'TCommand, 'TEvent>
abstract member Given: 'TEvent list
abstract member When: 'TCommand
abstract member Then: ('TState -> unit)
@Fristi
Fristi / StmExample.fs
Created April 1, 2013 12:57
Simple example of Stm in F#
let spawn (f:(unit -> unit)) = let t = new Thread(f) in t.Start(); t
let valueStm = newTVar 0
let incStm() = stm {
let! current = readTVar valueStm
let newValue = current + 1
do! writeTVar valueStm newValue
}
let funcStm() = for _ in 1..10000 do incStm() |> atomically |> ignore
@Fristi
Fristi / Maybe.hs
Last active December 20, 2015 09:49
Optional types in Java, Haskell and F#
data Maybe a = Just a | Nothing
@Fristi
Fristi / Aggregate.hs
Last active November 6, 2022 20:50
DDD/Event Sourcing in Haskell. Implemented an aggregate as a type class and type families to couple event, command and error types specific to the aggregate. Errors are returned by using Either (Error e) (Event e). Applying Applicative Functors fits here well to sequentially check if the command suffice.
{-# LANGUAGE TypeFamilies #-}
import Data.Function (on)
import Control.Applicative
data EventData e = EventData {
eventId :: Int,
body :: Event e
}
@Fristi
Fristi / ApplicativeFuture.scala
Last active August 29, 2015 14:01
scalaz: Applicative for Future[T]. Does not sequentially await, but executes futures concurrently
import java.util.concurrent.{Executors, TimeUnit}
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
import scalaz.Applicative
import scalaz.syntax.applicative._
object Main extends App {
implicit def applicativeFuture(implicit executionContext:ExecutionContext) = new Applicative[Future] {
override def point[A](a: => A): Future[A] = Future.successful(a)