Skip to content

Instantly share code, notes, and snippets.

View zahardzhan's full-sized avatar

Роман Захаров zahardzhan

View GitHub Profile
(defun str (&rest args)
"Returns the concatenation of string values of the args.
With no or nil args, returns the empty string."
(with-output-to-string (s)
(dolist (arg args) (princ (or arg "") s))))
(defmacro with-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
@zahardzhan
zahardzhan / *scratch*.clj
Created January 29, 2010 12:29
part of leica
(defn set-root-logger-log-level! [log-level]
(let [root-logger (Logger/getLogger "")
console-handler (first (.getHandlers root-logger))
date-formatter (java.text.SimpleDateFormat. "HH:mm:ss")
log-formatter (proxy [Formatter] []
(format
[#^LogRecord record]
(str \return
(.format date-formatter (Date. (.getMillis record)))
\space (.getMessage record) \newline)))]
(defn fn-or
"Объединение функций возвращает функцию эквивалентную
(fn [x] (or (f x) (g x) ... ) или
(fn [x & xs] (or (f x1 x2 ...) (g x1 x2 ...) ... )"
[f & fs]
(if-not fs f (let [chain (apply fn-or fs)]
(fn ([x] (or (f x) (chain x)))
([x & xs] (or (apply f x xs) (apply chain x xs)))))))
(defn seq-dispatch
@zahardzhan
zahardzhan / machine.clj
Created February 13, 2010 12:31
Turing machine in Clojure
;;; -*- mode: clojure; coding: utf-8 -*-
;;; author: Roman Zaharov <zahardzhan@gmail.com>
(ns clojure.turing-machine.machine)
(in-ns 'clojure.turing-machine.machine)
(defn convert-rules [rules]
(apply conj [] (for [rule rules :let [[state read jump write move] rule]]
{:state state :read read :jump jump :write write :move move})))
(defn up [] (int (Math/pow -1 (inc (.nextInt (new java.util.Random) 2)))))
(first (filter (partial = 10) (iterate #(+ % (up)) 0)))
(let [position (atom 0)
movements (cycle [-1 1 1])]
(defn up1 []
(reset! position (inc @position))
(nth movements @position)))
(use 'clojure.test)
(defn same
"Returns true if (f x) returns same value for any x in coll."
[f coll]
(apply = (map f coll)))
(deftest same-test
(is (same :x [{:x nil} {:y nil}]))
(is (same :x [{:x 1} {:x 1 :y 2}]))
@zahardzhan
zahardzhan / fn.clj
Created February 20, 2010 10:45
fn namespace
(ns fn (:refer-clojure :exclude [not and or]))
(def not complement)
(defn and "Functions intersection."
([f] f)
([f & fs] (let [chain (apply and fs)]
(fn [& xs] (clojure.core/and (apply f xs)
(apply chain xs))))))
(defn (power :is number? :or (throw 'Bad-input))
[(x :is number?)
(y :is [pos? integer?])
(n :is [pos? integer?] :is [neg? float?] :is [zero? double?] :or false)]
(/ x 0))
;;; Project 1, 6.001, Spring 2005
(ns basebot
(:use clojure.test))
(in-ns 'basebot)
;;; idea is to simulate a baseball robot
;; imagine hitting a ball with an initial velocity of v
(let [precedence-counter (atom 0)]
(defn make-agent
{:post [(instance? clojure.lang.Agent %)]}
[& state]
(let [state (apply array-map state)
agent-type (or (:type state) ::agent)
state (dissoc state :type)
a (agent (with-meta
(merge {:name nil