Skip to content

Instantly share code, notes, and snippets.

View AGhost-7's full-sized avatar
🤖
Discombobulating the hive mind

Jonathan Boudreau AGhost-7

🤖
Discombobulating the hive mind
  • The land of maple syrup
View GitHub Profile
@AGhost-7
AGhost-7 / gist:ee21cf364a534b4b1e59
Last active August 29, 2015 14:15
Option/Maybe Monad in CoffeeScript
# Implementation defines behaviour instead of state.
None =
map: (func) -> None
flatMap: (func) -> None
forEach: (func) -> # Nothing!
getOrElse: (def) -> def
isDefined: () -> false
fold: (empty, notEmpty) -> empty()
@AGhost-7
AGhost-7 / gist:9d51a53ae6f7b3e8a9f2
Last active August 29, 2015 14:15
SqlConnection.scala
import java.sql.{Connection, DriverManager}
import scala.util.Try
import java.net.{URISyntaxException, URI}
/** Object abstracts away some of the connection sheneanigans you need to do
* if you're trying to use JDBC. This is nice if you're just trying to make
* something of very simple with anorm (i.e., without Play! framework).
*/
object SqlConnection {
class Effect
constructor: (@object, @sideEffect) ->
map: (func) ->
copy = clone(@object)
@sideEffect(copy)
new Effect(copy, func)
flatMap: (func) ->
@AGhost-7
AGhost-7 / Build.scala
Created May 1, 2015 01:06
Function Piping
import sbt._
import Keys._
import java.io.File
object FunBuild extends Build {
lazy val root = Project(id = "root", base = file(".")).settings(
name := "Fun",
version := "0.1",
@AGhost-7
AGhost-7 / gist:cbc1c523ac399c11072a
Last active August 29, 2015 14:22
Very basic Coffeescript pattern matching
class Message
class Greeting extends Message
class Shout extends Message
class Farewell extends Message
# Curry for reusing the match
match = (patterns...) -> (val) ->
for pat in patterns
if val instanceof pat[0] then return pat[1](val)
@AGhost-7
AGhost-7 / Main.scala
Created June 2, 2015 22:19
Akka Workcount Problem
import java.io.File
import akka.actor._
import scala.concurrent.{ExecutionContext, Promise, Future}
import scala.util.{Success, Failure}
/** All IO methods */
object FileIO {
@AGhost-7
AGhost-7 / gist:b4d2144faa45c0497af2
Last active August 29, 2015 14:22
Third Party Client-Side Code Injection
var privateApi = {
privateFunction: function() {
console.log('this method cannot be invoked by the injected code.');
}
};
var api = {
log: function() {
console.log('Sandbox says');
Array.prototype.forEach.call(arguments, function(arg){ console.log(arg) });
var canvas = document.getElementsByTagName('canvas')[0]
canvas.width = window.innerWidth
canvas.height = window.innerHeight
var gl = canvas.getContext('webgl')
gl.clearColor(1, 0, 1, 1)
gl.clear(gl.COLOR_BUFFER_BIT)
var vertexShader = gl.createShader(gl.VERTEX_SHADER)
@AGhost-7
AGhost-7 / immutable-list.js
Last active August 29, 2015 14:25
Immutable Javascript Linked List
// immutable linked list
// Some functional patterns use head/tail a *lot*, and this particular collection
// has constant time for both head and tail. Concatenation is also constant time.
var nil = {};
// used to make the fields on the list truly immutable
function props(obj, fields){
var args = {};
for(var key in fields){
@AGhost-7
AGhost-7 / example.repl
Last active August 29, 2015 14:25
Function Piping in Javascript
> var pipe = require('./pipe')
undefined
> var spread = pipe.spread
undefined
> function upper(s) { return s.toUpperCase() }
undefined
> pipe('foo',upper)
'FOO'
> function splitter(sp) { return function(str) { return str.split(sp) } }
undefined