Skip to content

Instantly share code, notes, and snippets.

View budu's full-sized avatar
😀

Nicolas Buduroi budu

😀
View GitHub Profile
@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#)))))
@budu
budu / gist:782221
Created January 16, 2011 22:32
Prototype a new Marginalia parser
(use '(clojure.contrib [reflect :only [get-field]])
'(clojure [string :only [join replace]]))
(deftype Comment [content])
(defmethod print-method Comment [comment ^String out]
(.write out (str \" (.content comment) \")))
(defn read-comment [reader semicolon]
(let [sb (StringBuilder.)]
@budu
budu / player.clj
Created July 11, 2010 22:42
A simple (headless) MP3 player written in Clojure using the clj-audio library.
(ns player
(:use clj-audio.core
clj-audio.sampled)
(:import javax.sound.sampled.SourceDataLine
java.io.File))
;;;; playlist
(def music-file-extensions ["mp3"])