Skip to content

Instantly share code, notes, and snippets.

View fxg42's full-sized avatar

François-Xavier Guillemette fxg42

View GitHub Profile
@fxg42
fxg42 / pandoc_express.coffee
Created September 30, 2013 16:57
Use pandoc with express
async = require 'async'
fs = require 'fs'
pandoc = require 'pdc'
path = require 'path'
temp = require 'temp'
temp.track()
openTempFile = (callback) ->
temp.open {suffix:'.pdf'}, callback
@fxg42
fxg42 / hello.groovy
Last active August 29, 2015 13:56
RabbitMQ HelloWorld
// Copyright (C) 2014 François-Xavier Guillemette
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What The Fuck You Want
// To Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
@Grab('com.rabbitmq:amqp-client:2.8.1')
import com.rabbitmq.client.*
@fxg42
fxg42 / merge_or_attach.groovy
Last active August 29, 2015 13:57
Merge, attach or refresh?
import gotchas.*
// Alice gets the location and saves it to her session...
def alice = [:]
Location.withNewSession { session ->
alice.location = session.get(Location, 1L)
session.close()
}
@fxg42
fxg42 / gotchas.md
Created April 2, 2014 13:20
gotchas...

GOTCHAS!

Notes

class B { static belongsTo = A } defines the cascading behaviour from A to B.

When domain B belongsTo A, saves and deletes from A cascade to B. Operations are cascaded from A to B but not from B to A.

class A { static hasMany = [bs:B] } defines a one-to-many relationship, a

@fxg42
fxg42 / lambda_fun.coffee
Last active August 29, 2015 14:03
lambda calculus
assert = require 'assert'
ZERO = (f) -> (x) -> x
ONE = (f) -> (x) -> f(x)
TWO = (f) -> (x) -> f(f(x))
toInt = (n) -> n((x) -> x + 1)(0)
assert 0 is toInt(ZERO)
assert 1 is toInt(ONE)
@fxg42
fxg42 / pg-highland.coffee
Last active August 29, 2015 14:07
Postgresql + Highland
_ = require 'highland'
pg = require 'pg'
QueryStream = require 'pg-query-stream'
CONNECTION_STRING = "postgres://postgres:postgres@localhost/contacts"
rows = (stmt, params=[]) ->
(push, next) ->
pg.connect CONNECTION_STRING, (err, client, done) ->
push err if (err)
@fxg42
fxg42 / HelloController.groovy
Last active January 26, 2017 20:02
Comparing Express, Phoenix, Rails, Flask, Grails and... PHP?
class HelloController {
def index() {
render([ hello:"world!" ] as grails.converters.JSON)
}
}
drop table if exists schema_migrations;
create table schema_migrations (
id serial primary key not null,
version character varying(255) unique not null
);
alter table schema_migrations
owner to postgres;
@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
@fxg42
fxg42 / optional.ex
Last active January 21, 2022 12:48
Maybe monad with an Elixir macro dsl
defmodule Optional do
defmacro __using__(_opts) do
quote do
require unquote(__MODULE__)
import unquote(__MODULE__)
end
end
def unit(nil), do: {:err, nil}
def unit(value), do: {:ok, value}