Skip to content

Instantly share code, notes, and snippets.

module ParameterizedConcern
def extended(mod = nil, &block)
unless mod
@extended_block = block
return
end
mod.module_eval(&@extended_block) if @extended_block
end

2010 Modularity Olympics

This is a contest, open to programming languages from all nations, to write modular and extensible code to solve the following problem: Implement a service that can run queries on a database.

The Challenge

Sounds simple right? Wrong! A programmer without control over the source-code of that service must be able to later add enhancements such as statistics collecting, timeouts, memoization, and so forth. There are a few more requirements:

  1. the “enhancements” must be specified in a configuration object which is consumed at run-time (e.g., it could be based on user-input).
  2. The enhancements are ordered (stats collecting wraps timeouts, not the other way around) but it must be possible to reverse the order of the enhancements at run-time.
  3. The enhancements must be “surgical” and not “global”. That is, it must be possible to simultaneously have two query services, one reversed and one not reversed, and even have a query service without any enhancements.
class Proc
def <<(other)
case other
when Proc
Proc.new do |*args|
call(other.call(*args))
end
else
call(other)
end
@mgerbone
mgerbone / gist:1285369
Created October 13, 2011 20:12
nginx config
# This is example contains the bare mininum to get nginx going with
# Unicorn or Rainbows! servers. Generally these configuration settings
# are applicable to other HTTP application servers (and not just Ruby
# ones), so if you have one working well for proxying another app
# server, feel free to continue using it.
#
# The only setting we feel strongly about is the fail_timeout=0
# directive in the "upstream" block. max_fails=0 also has the same
# effect as fail_timeout=0 for current versions of nginx and may be
# used in its place.
(ns explicit-state-transducers)
(defn take-with-explicit-state
"A stateless rewrite of the `c.c/take` transducer that requires its state to
be provided as part of each input"
[n]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
@Deraen
Deraen / editor_plugin.cljs
Last active October 17, 2015 23:25
Extending Closure Classes From Clojurescript. Original source: http://www.50ply.com/blog/2012/07/08/extending-closure-from-clojurescript/
(ns frontend.editor-plugin
(:require-macros [frontend.goog-extend :refer [goog-extend]])
(:import [goog.editor Plugin]))
; 4. Now the code should work with advanced compilation but Closure Compiler
; will display few warnings about using "this" in static functions.
; To fix this, the constuctor should have proper JSDoc comments:
(goog-extend ^{:jsdoc ["@constructor" "@extends {goog.editor.Plugin}"]} FooPlugin Plugin
([]
; 1. This doesn't work with advanced compilation as Closure compiler checks that first
@r00k
r00k / gist:36677951462ffd01d98c
Last active December 9, 2016 15:40
Ben Orenstein's Weekly Review Checklist
# Checklist for Weekly Review
## Sweep
[ ] Process work email inbox
[ ] Process personal email inbox
[ ] Review calendar, +/- 2 weeks.
[ ] Review [this trigger list](http://gettingthingsdone.com/wp-content/uploads/2014/10/Mind_Sweep_Trigger_List.pdf)
## Process
Capybara.add_selector :record do
xpath { |record| XPath.css("#" + ActionController::RecordIdentifier.dom_id(record)) }
match { |record| record.is_a?(ActiveRecord::Base) }
end
;; warm up: balancing
=> (s/def ::balanced
(s/* (s/cat :open #{'<} :children ::balanced :close #{'>})))
:user/balanced
=> (s/conform ::balanced '[< < > < > > < >])
[{:open <, :children [{:open <, :close >} {:open <, :close >}], :close >} {:open <, :close >}]
=> (s/conform ::balanced '[< < > < < > > > < >])
[{:open <, :children [{:open <, :close >} {:open <, :children [{:open <, :close >}], :close >}], :close >} {:open <, :close >}]
;; infix to prefix
Generativity Thought Experiment
Suppose I want to generate something, maybe:
- a number
- a string
- a list of numbers
- a set of parameters that define a 3d model
- a game level
- a poem
- a story