Skip to content

Instantly share code, notes, and snippets.

@caiorss
caiorss / gist:6a0f3fcbbea329931dca58217e3ece97
Created March 10, 2017 16:54 — forked from chrislewis/gist:3416494
reader monad for java.util.Properties example
/* Start by creating a reader for some fake yet conceivable type for executing SQL queries. */
val dbReader: Reader[Properties, JdbcExecutor] =
for {
driver <- read[String]("db.driver")
uri <- read[String]("db.uri")
user <- read[String]("db.username")
password <- read[String]("db.password")
name <- read[String]("db.pool.name")
minCons <- read[Int]("db.pool.minConnections")
@caiorss
caiorss / reader-resources.scala
Created March 10, 2017 16:51 — forked from balajisivaraman/reader-resources.scala
Reader Monad Resource Management
val getJDBCConnection: Reader[JDBCConf, Connection] = ???
val getStatement: Reader[Connection, PreparedStatement] = ???
val executeQuery: Reader[PreparedStatement, ResultSet] = ???
val queryExecutor: Reader[JDBCConf, ResultSet] = getJDBCConnection.andThen(getStatement).andThen(executeQuery)
Monad | effect | sequences the effect as | M[A] | bind: M[A] => (f: A => M[B]) => M[B]
Identity | nothing | continue | Id[A] | f(a)
Option | zero or one value (anonymous exception) | halt if None | Option[A] | if Some(a) f(a)
Either | exception with error type or one value | halt if Left | Either[L, A] | if Right(a) f(a)
List | any # of values (non-determinism) | halt if empty | List[A] | join(each f(a))
Reader | an environment; dependency-injection | function composition | R => A | (r: R) => f(a(r))
Writer | logging | append log value W | Writer[W, A](log: W, value: A) | k = f(a.value); Writer(a.log |+| k.log, k.value)
State | state | new state from old | State[S, A](s: S => (S, A)) |
Responder | continuation-passing
@caiorss
caiorss / Example.scala
Last active March 10, 2017 16:47 — forked from echeipesh/Reader.scala
Reader Monad
/// REPL Testing
//
// -----------------------------------------
$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions for evaluation. Or try :help.
scala> case class Reader[C, A](g: C => A) {
| def apply(c: C) = g(c)
@caiorss
caiorss / Reader.scala
Created March 10, 2017 15:42 — forked from blouerat/Reader.scala
Reader Monad
/*
Based on the first part of the talk "Dead-Simple Dependency Injection in Scala" by @runarorama at NEScala 2012
http://marakana.com/s/dependency_injection_in_scala,1108/index.html
*/
class Connection {
def prepareStatement(query: String) = new Statement()
}
class Statement {
@caiorss
caiorss / ps.el
Created February 28, 2017 00:00 — forked from kurohuku/ps.el
(defun postscript-process ()
(get-buffer-process (get-buffer "*postscript*")))
(defun run-postscript ()
(interactive)
(require 'comint)
(switch-to-buffer (make-comint "postscript" "gs")))
(push '("postscript" . utf-8) process-coding-system-alist)
@caiorss
caiorss / bulletins_anonym_1.ps
Created February 27, 2017 23:47 — forked from fabienhinault/bulletins_anonym_1.ps
postscript file for vote bulletins
%!PS
%%DocumentMedia: a4 595 842 80 () ()
/pageHeight 842 def
/pageWidth 595 def
<< /PageSize [pageWidth pageHeight] >> setpagedevice
/mm { 2.834645669 mul } def
@caiorss
caiorss / logo.ps
Created February 27, 2017 23:47 — forked from larryv/logo.ps
MIT logo in PostScript
newpath
300.0 400.0 moveto
gsave
currentpoint translate
-241.66666666666669 0.0 rmoveto
108.33333333333334 0.0 rmoveto
gsave
currentpoint translate
-108.33333333333334 0.0 rmoveto
25.0 0.0 rmoveto
@caiorss
caiorss / .emacs
Created February 20, 2017 15:24 — forked from snoyberg/.emacs
Michael Snoyman's .emacs file: Stack, haskell-mode, evil-mode
;; Get package installation ready
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(package-initialize)
;; Bootstrap `use-package'
(unless (package-installed-p 'use-package)
(package-refresh-contents)
@caiorss
caiorss / packages.el
Created February 20, 2017 15:23 — forked from dysinger/packages.el
Private spacemacs layer to try out Chris Done's Intero mode for haskell
;; 1. place this in ~/.emacs.d/private/intero/packages.el
;; 2. add intero, syntax-checking and auto-completion to your
;; ~/.spacemacs layer configuration & remove the haskell layer
;; if you were using that before
;; 3. make sure you have stack installed http://haskellstack.org
;; 4. fire up emacs & open up a stack project's source files