Skip to content

Instantly share code, notes, and snippets.

View zahardzhan's full-sized avatar

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

View GitHub Profile
action a > a
strategy a > action
schedule strategy > strategy
schedule strategy > strategy a > action a
(defn schedule-strategy [strategy]
(fn scheduled-strategy [a-stgy]
(fn scheduled-action [a-actn]
((strategy a-actn) a-actn))))
(defn as-file
[arg & {:as args :keys [exists create readable writeable directory]}]
(let [argtype (type arg)]
(cond (= argtype File)
(let [maybe-exists
(fn [f] (cond (= exists true) (when (.exists f) f)
(= exists false) (when-not (.exists f) f)
(not exists) f))
maybe-directory
(fn [f] (cond (= directory true) (when (.isDirectory f) f)
(defmacro when-supplied [& clauses]
(if (not clauses) true
`(and (or (nil? ~(first clauses))
(do ~(second clauses)))
(when-supplied ~@(next (next clauses))))))
(defn make-download-agent
[line & {:as opts :keys [environment strategy precedence path name]}]
{:pre [(when-supplied strategy (instance? clojure.lang.IFn strategy)
precedence (number? precedence)
;;; -*- mode: clojure; coding: utf-8 -*-
;; Copyright (C) 2010 Roman Zaharov <[email protected]>
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
(ns log
(:use
(defmacro defhook [name qualifier target args & body]
(let [key# (keyword name)]
`(add-hook ~qualifier ~target ~key# (fn ~args ~@body))))
(defn run [{:as a :keys [strategy run-atom]}]
(when (run? a) (throw (Exception. "Cannot run agent while it is running.")))
(when (dead? a) (throw (Exception. "Cannot run dead agent.")))
(try (reset! run-atom true)
(let [action (strategy a)]
@zahardzhan
zahardzhan / tictactoe.py
Created August 30, 2010 14:00
TicTacToe
def wins(grid):
rows = [[grid[i+j] for j in 0, 1, 2] for i in 0, 3, 6]
cols = [[grid[i+j] for j in 0, 3, 6] for i in 0, 1, 2]
digs = [[grid[i] for i in 0, 4, 8], [grid[i] for i in 2, 4, 6]]
return any(all(cell is 'x' for cell in row) or
all(cell is 'o' for cell in row)
for row in rows + cols + digs)
def full(grid):
return all(cell is 'x' or cell is 'o' for cell in grid)
address_dispatch_table = {}
class MetaDownloadAgent(type):
def __new__(cls, name, bases, attributes):
return super(MetaDownloadAgent, cls).__new__(cls, name, bases, attributes)
def __init__(self, name, bases, attributes):
super(MetaDownloadAgent, self).__init__(name, bases, attributes)
if 'address_pattern' in attributes:
address_dispatch_table[attributes['address_pattern']] = self
# -*- mode: python; coding: utf-8; -*-
# (def nbr-deltas [[-1 -1][-1 0][-1 1][0 -1][0 1][1 -1][1 0][1 1]])
# (defn nbr-cells [[x y]] (map (fn [[a b]] [(+ x a)(+ y b)]) nbr-deltas))
# (defn cell-table [cell] (apply conj {cell 10} (map #(vec [% 1]) (nbr-cells cell))))
# (defn all-table [cells] (apply merge-with + (map cell-table cells)))
# (defn next-gen [cells] (keys (filter #(#{3 12 13} (second %)) (all-table cells))))
# (defn n-next-gen [n cells] (if (== n 0) cells (recur (- n 1) (next-gen cells))))
# (def r-pentomino [[0 1][1 1][2 1][1 2][2 0]])
;;; -*- mode: clojure; coding: utf-8 -*-
;;;
;;; Loops — Common Lisp Iterate macro for Clojure
;;;
;;; Copyright 1989 by Jonathan Amsterdam
;;; Adapted to ANSI Common Lisp in 2003 by Andreas Fuchs
;;; Adapted to Clojure in 2010 by Roman Zaharov @zahardzhan <[email protected]>
;;;
;;; Permission to use, copy, modify, and distribute this software and its
;;; documentation for any purpose and without fee is hereby granted,
@zahardzhan
zahardzhan / downloadsomemusic.py
Created September 29, 2011 06:01
My first Python script
#!/usr/bin/python
# Script for downloading music from sites:
# www.mnogomp3.net
# www.tenshi.spb.ru
import getopt
import sys
import os
import re