Skip to content

Instantly share code, notes, and snippets.

View d11wtq's full-sized avatar

Chris Corbyn d11wtq

  • Melbourne, Australia
View GitHub Profile
(ns example.factory.aws.cloudformation
(:require [clojure.data.json :as json]))
(defn apply-stack-fn
[create-fn update-fn]
(fn apply-stack
[payload]
(try
(create-fn payload)
(catch AlreadyExistsException e
@d11wtq
d11wtq / README.md
Last active August 29, 2015 14:20
Writing int->str (Clojure)

int->str

This function computes the English string for any given integer supported by Clojure.

Example

(int->str 110917) ; "one hundred and ten thousand nine hundred and seventeen"
@d11wtq
d11wtq / atbash.clj
Last active August 29, 2015 14:21
Atbash Cipher in Clojure
(ns atbash.core
(require [clojure.string :refer [join lower-case]]))
(def lookup
(let [alphabet "abcdefghijklmnopqrstuvwxyz"]
(zipmap alphabet (reverse alphabet))))
(defn encode
[s]
(->> (lower-case s)
@d11wtq
d11wtq / declare.clj
Last active August 29, 2015 14:21
Implicit forward declarations for Clojure
(defn def?
[[a b]]
(if (#{'defn 'def} a) b))
(defmacro progn
[& sexps]
(let [names (->> sexps (filter def?) (map def?))]
`(do
(declare ~@names)
~@sexps)))
@d11wtq
d11wtq / roman.clj
Last active August 29, 2015 14:22
Roman numerals
(ns roman.core)
(defmacro defpairs
"Helper macro to make ordered defs for pairs nicer."
[varname pairs]
`(def ~varname
(partition 2 ~pairs)))
(defpairs digits
["M" 1000
@d11wtq
d11wtq / connection_pool_adapter.rb
Created May 6, 2011 13:45
Sample database.yml for multiple slaves/masters with DataMapper
module DataMapper
module Adapters
class ConnectionPoolAdapter < AbstractAdapter
def initialize(name, options)
super
assert_kind_of 'options', @options[:pool], Array
raise ArgumentError, "The are no adapters in the adapter pool" if @options[:pool].empty?
@d11wtq
d11wtq / sequence.rb
Created May 6, 2011 14:41
Sequence Property type for DataMapper
=begin
@author Chris Corbyn
@license None, use at your own risk
=end
module DataMapper
class Property
# Adds support for a sequences table to DataMapper, via a Sequence property type.
#
# class Person
class Time
class << self
attr_accessor :mock_time
def now_with_mock_time
@mock_time || now_without_mock_time
end
alias_method_chain :now, :mock_time
@d11wtq
d11wtq / example.rb
Created July 4, 2011 06:19
Validation always fails with "Address must be an Integer" ?
class Example
include DataMapper::Resource
property :id, Serial
property :ip_address, IPAddressInteger
end
example = Example.new
example.ip_address = "10.48.1.1"
@d11wtq
d11wtq / custom_json_format.rb
Created July 4, 2011 16:13
Workaround for dm-rest-adapter serialization to JSON with :field and foreign keys
# This is used temporarily, until serialization to :raw is done in dm-serializer
module DataMapper
module Rest
class CustomJsonFormat < DataMapperRest::Format::Json
def string_representation(resource)
model = resource.model
hash = {}
hash = model.properties.reduce(hash) do |h, property|
h.merge(property.field.to_sym => property.dump(property.get(resource)))