Skip to content

Instantly share code, notes, and snippets.

View he9lin's full-sized avatar

Lin He he9lin

View GitHub Profile
@fxg42
fxg42 / optional.ex
Last active August 29, 2015 14:14
Maybe monad with Elixir
defmodule Optional do
def unit(nil), do: {:err, nil}
def unit(value), do: {:ok, value}
def lift(func) do
fn input -> unit(func.(input)) end
end
def bind({:ok, optional}, functor), do: functor.(optional)
def bind(err, _), do: err
@dblock
dblock / oauth_controller.rb
Created December 11, 2011 15:16
An updated OAuth2 controller for a Rails app (implies you have ClientApplication and AccessGrant)
class OauthController < ApplicationController
class ApiOAuthError < StandardError
attr_accessor :code, :description, :uri, :state
def initialize(code, description, uri = nil, state = nil)
@code = code
@description = description
@uri = uri
@jmont
jmont / maybe.m
Last active March 30, 2016 16:27
Swift Monads
// Swift Monads -- Maybe
// Juan C. Montemayor (@norsemelon)
// This operator can be used to chain Optional types like so:
// optionalVal >>= f1 >>= f2
// where f1 and f2 have type `Any -> Any?`
//
// If a value is ever nil, the chain short-circuits and will result in nil.
// This is a much neater way to do this than using the if syntax specified in
// the Swift iBook.
@swlaschin
swlaschin / NewsDomainDtoExample.fsx
Last active November 17, 2019 07:18
Demonstrates DTO validation for a news feed domain
(*
Demonstrates DTO validation for a news feed domain
*)
// "result.fsx" comes from https://github.com/swlaschin/DmmfWorkshop/blob/master/src/E-ModelingErrors/Result.fsx
#load "result.fsx"
module Domain =
type DocumentId = DocumentId of int
type EmailAddress = EmailAddress of string
@timruffles
timruffles / dyanmic_or_di_elixir.md
Last active June 11, 2020 04:23
Approaches to dependency-injection/dynamic dispatch in elixir

In many production systems you'll want to have one module capable of talking to many potential implementations of a collaborator module (e.g a in memory cache, a redis-based cache etc). While testing it's useful to control which module the module under test is talking to.

Here are the approaches I can see. The two points that seem to divide the approaches are their tool-ability (dialyzer) and their ability to handle stateful implementations (which need a pid).

Passing modules

Modules are first class, so you can pass them in. Used in EEx, where passed module must implement a behaviour.

(*
ParserLibrary_v1.fsx
Version 1 of the code for a parser library.
Related blog post: http://fsharpforfunandprofit.com/posts/understanding-parser-combinators/
*)
open System
@mattbaggott
mattbaggott / predicting_customer_behav_1.R
Last active September 15, 2020 22:16
Uses the BTYD package and Pareto/NBD model to predict customer behavior in R Slides are at: http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#
# PREDICTING LONG TERM CUSTOMER VALUE WITH BTYD PACKAGE
# Pareto/NBD (negative binomial distribution) modeling of
# repeat-buying behavior in a noncontractual setting
#
# Matthew Baggott, matt@baggott.net
#
# Accompanying slides at:
# http://www.slideshare.net/mattbagg/baggott-predict-customerinrpart1#
#
@chriseidhof
chriseidhof / TypedNotifications.swift
Created January 26, 2015 14:41
Typed Notifications
import Foundation
class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
}
struct Notification<A> {
let name: String
}
@he9lin
he9lin / deploy_rails_app_on_ubuntu.md
Created October 1, 2011 09:51
Setup Rails application production environment on Ubuntu

Setup Rails application production environment on Ubuntu

Add deploy user

ssh root@YOURDOMAIN
adduser deploy
visudo # Add deploy ALL=(ALL) ALL

Install necessary libraries

@ryanb
ryanb / chef_solo_bootstrap.sh
Created April 5, 2012 04:35
Bootstrap Chef Solo
#!/usr/bin/env bash
apt-get -y update
apt-get -y install build-essential zlib1g-dev libssl-dev libreadline5-dev libyaml-dev
cd /tmp
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p125.tar.gz
tar -xvzf ruby-1.9.3-p125.tar.gz
cd ruby-1.9.3-p125/
./configure --prefix=/usr/local
make
make install