Skip to content

Instantly share code, notes, and snippets.

@budu
budu / xkcd287.clj
Created April 17, 2012 02:24
Logic puzzle: XKCD #287 solution in Clojure core.logic
(use 'clojure.core.logic)
(def rvar? (comp not lvar?))
(defmacro arithm [r x y op po]
`(fn [a#]
(let [r# (walk a# ~r)
x# (walk a# ~x)
y# (walk a# ~y)]
(cond
@budu
budu / ComUtils.cs
Created April 12, 2012 19:40
Utility class to extract properties dynamically from a COM object. This provide a method that extract all of a given object properties (even those taking parameters) and generate a nice set of nested dictionnaries.
public static class ComUtils
{
public static object Get(object obj, string key, object[] args = null)
{
return obj.GetType().InvokeMember(
key,
System.Reflection.BindingFlags.GetProperty,
null,
obj,
args);
@budu
budu / send_more_money.clj
Created April 12, 2012 05:03
Logic puzzle: send more money solution in Clojure core.logic, translated from cKanren paper. Takes forever to run!
(use 'clojure.core.logic
'clojure.core.logic.arithmetic)
(defne diffo
[l]
([[]])
([[x]])
([[x y . tail]]
(fresh [l0 l1]
(!= x y)
@budu
budu / attr_history.rb
Created March 3, 2012 18:03
Ruby Class mixin to create an accessor for which historical vales are kept in an Array, not thread-safe.
module AttrHistory
def attr_history(name)
attr_reader "#{name}_history", name
define_method "#{name}=" do |value|
history = send("#{name}_history") || []
instance_variable_set "@#{name}_history", history.push(value)
instance_variable_set "@#{name}", value
end
end
@budu
budu / factory.clj
Created December 21, 2011 03:35
A factory defining macro.
(defmacro deffactory
"Defines a factory function that returns an instance of the specified
record initialized with the default values provided overridden by the
values it is given."
[factory-name record-name defaults]
`(defn ~factory-name
~(str "A factory function returning a new instance of " record-name
" initialized with the defaults specified at the time of"
" definition overridden by the given values. The values can"
" be specified as a map or field-value pairs.")
@budu
budu / sicp.clj
Created September 4, 2011 23:56
Answers to exercises from chapter 1 of SICP in Clojure
(ns sicp)
;;;; Section 1.1 - The Elements of Programming
;;; Exercise 1.1
[10 12 8 3 6 'a 'b 19 false 4 16 6 16]
@budu
budu / SnakeMyTurnIn.hs
Created August 27, 2011 05:06
Snake my_turn_in puzzle implementation in Haskell
-- Snake my_turn_in puzzle implementation
--
-- The direction argument accept an Ordering value where GT is
-- :forward, LT is :backward and EQ is :same.
--
-- Problem definition can be found in:
-- https://github.com/budu/Puzzles
myTurnIn :: (Ord a, Integral a) => a -> a -> Ordering -> a -> a
myTurnIn _ _ _ 1 = 0
@budu
budu / sy2.clj
Created May 4, 2011 01:11
Recursive implementation of Shunting-yard algorithm (refactored)
(ns sy2 "based on: https://gist.github.com/953966")
(defmacro if-eof [true-f false-f]
`(try
~false-f
(catch Exception e#
(if (= (.getMessage e#) "EOF while reading")
~true-f
(throw e#)))))
@budu
budu / sy.clj
Created May 3, 2011 00:08
Monadic implementation of the Shunting-yard algorithm
(ns sy
"Monadic implementation of the Shunting-yard algorithm, including
infix binary operator with associativity, function calls and basic
sequence support. It transalate a simple C like expression syntax into
s-expression that can be evaluated by Clojure.
1 + 2 => (+ 1 2)
1 + 2 * 3 => (+ 1 (* 2 3))
1 * 2 + 3 => (+ (* 1 2) 3)
(1 + 2) * 3 => (* (identity (+ 1 2)) 3)
@budu
budu / crazy.clj
Created April 20, 2011 22:50
A macro which define a function and a macro that call that function passing the inner macro arguments and the form used to call it as the function arguments, or something like that.
(defmacro crazy [name & args]
(let [name* (symbol (str name \*))]
`(do
(defn ~name* [self# & params#]
(prn self#))
(defmacro ~name [~'& args#]
`(~~name* (quote ~~'&form) ~@args#)))))