Skip to content

Instantly share code, notes, and snippets.

View owainlewis's full-sized avatar

Owain Lewis owainlewis

View GitHub Profile
@lrvick
lrvick / controllers.js
Last active December 14, 2015 19:09
Websocket AngularJS controller/services pattern
app.controller( 'AppCtrl', function ($scope, socket) {
socket.onopen(
function(){
console.log('Socket is connected :D')
}
)
socket.onclose(
function(){
console.log('Socket is disconnected :(')
}
@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));
});
@koddo
koddo / gist:4555655
Last active December 11, 2015 05:58
for Coursera / Programming Languages
;; for Coursera / Programming Languages
;; in emacs sml-mode: single keystroke for restarting repl and loading current file
(require 'cl)
(add-hook 'sml-mode-hook
(lambda ()
(define-key sml-mode-map (kbd "C-c C-v") 'my-sml-restart-repl-and-load-current-file)
(defun my-sml-restart-repl-and-load-current-file ()
(interactive)
(ignore-errors (with-current-buffer "*sml*"
(comint-interrupt-subjob)
@hyone
hyone / gist:3950460
Created October 25, 2012 04:49
Multiple async HTTP requests by Haskell
{-# LANGUAGE FlexibleContexts #-}
import Data.Conduit
import qualified Data.Conduit.List as CL
import Network.HTTP.Conduit
import Control.Concurrent.Async (mapConcurrently)
import Control.Concurrent.MVar
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Control (MonadBaseControl)
@richhickey
richhickey / thread.clj
Created October 13, 2012 17:43
new thread macros draft
(defmacro test->
"Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test expression (not threaded) is true."
[expr
& clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[test step]] `(if ~test (-> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
(ns dojo.core
(:require
[clojure.string :as str]))
;;;
(def digits
[[" _ " "| |" "|_|"]
[" " " |" " |"]
[" _ " " _|" "|_ "]
@channingwalton
channingwalton / gist:3230464
Created August 1, 2012 20:31
Example of Kleisli composition of Validation
object KleisliValidation extends App {
import scalaz._
import Scalaz._
import scala.util.control.Exception._
type EValidation[+T] = Validation[String, T]
implicit val binding = new Bind[EValidation] {
def map[A, B](fa: EValidation[A])(f: A => B): EValidation[B] = fa.map(f)
def bind[A, B](fa: EValidation[A])(f: A => EValidation[B]): EValidation[B] = fa.flatMap(f)
}
@riipandi
riipandi / linux-cmd-cheatsheet.md
Created July 12, 2012 12:16
Linux Command Cheat Sheet

#Linux Cheat Sheet

##File Commands:

  • ls – directory listing
  • ls -al – formatted listing with hidden files
  • cd dir - change directory to dir
  • cd – change to home
  • pwd – show current directory
  • mkdir dir – create a directory dir
  • rm file – delete file
@ghoseb
ghoseb / cut.clj
Created April 16, 2012 09:32
Cut macro from SRFI-26 in Clojure
;;; http://srfi.schemers.org/srfi-26/srfi-26.html
(defn ^:private cut*
[[a f] form]
(cond
(nil? form) [a f]
(seq? (first form))
(let [[arg-list xform] (cut* [[] '()] (first form))]
(recur [(reduce conj a arg-list) (concat f (list xform))] (next form)))
@sw17ch
sw17ch / indented_parsec_example.lhs
Created March 16, 2012 04:31
A full example demonstrating the use of the indentation parser provided by the 'indents' package: http://hackage.haskell.org/package/indents
> module Main where
First, import all the needed modules.
> import Text.Parsec hiding (State)
> import Text.Parsec.Indent
> import Control.Monad.State
Next, define our new Parser type. This replaces the Identity monad
with the (State SourcePos) monad.