Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
PuercoPop / linked lists
Created June 23, 2013 02:34
Failing at linked lists
(defpackage :cl-datastructures
(:use :cl))
(in-package :cl-datastructures)
(defclass linked-list-node ()
((next :initarg :next :initform nil)
(value :initarg :value :initform nil)))
(defclass linked-list ()
((head :initarg :value :initform nil)))
(defun test-collector ()
"Looks for all the triplets and defines a test for them. In current suite or
taking a suite as an argument? "
(loop
for triplet in (setup-test-tuples
(merge-pathnames "requests/valid/*.http" *specs-dir*))
do
(let ((http-file (first triplet))
(request-file (second triplet))
(spec-file (third triplet)))
@PuercoPop
PuercoPop / first-macro.lisp
Last active December 18, 2015 10:49
my first approach to a macro
;;; Success
;;; Less Boilerplate
(defmacro define-accessor (name accessor)
`(progn
(defun ,name (board)
,accessor)
(defun (setf ,name) (value board)
(if (eq (,name board) :empty)
(setf ,accessor value)
(error 'invalid-move :text "The square is not empty")))))
@PuercoPop
PuercoPop / test.lisp
Created June 12, 2013 14:36
Why does the test fail?
(in-package :cl-user)
(ql:quickload :fiveam)
(defun top-left (board)
(car board))
(defun (setf top-left) (value board)
(setf (car board) value))
(fiveam:test top-left-accessor ()
@PuercoPop
PuercoPop / test.lisp
Last active December 18, 2015 09:29
Why does setf (top-left board) fails?
(in-package :cl-user)
;;; Everything now works thanks to Bike and http://stackoverflow.com/questions/11457071/defining-setf-expanders-in-common-lisp
;;; Learned something new today
(defun top-center (board)
(car (cdr board)))
(defun (setf top-center) (value board)
@PuercoPop
PuercoPop / example.py
Created June 11, 2013 04:39
Why it is important to use immutable as default arguments for function
# -*- coding: utf-8 -*-
"""A short example of why you shouldn't use lists or dicts as default. In python
default parameteres are bounded when the function definition is executed as
part of module loading. So after the function is created the default arguments
always point the same object (being list or dict, list in this case.). One
should only use inmutable objects as None or strings as default arguments.
"""
def foo(bar=[]):
"""Never do this
@PuercoPop
PuercoPop / Programming Praxis 10-4-13
Created May 10, 2013 19:57
2: 1978: The year 1978 is such that the sum of the first two digits and the latter two digits is equal to the middle two digits, i.e. 19 + 78 = 97. What is the next year (after 1978) for which this is true?
(defun validate-candidate (candidate)
(let ((candidate (write-to-string candidate)))
(unless (= (length candidate) 4)
(error "Test valid only for 4 digit numbers"))
(let ((initial-digits (parse-integer (subseq candidate 0 2)))
(middle-digits (parse-integer (subseq candidate 1 3)))
(final-digits (parse-integer (subseq candidate 2 4))))
(if (= middle-digits (+ initial-digits final-digits))
t
nil))))
(asdf:defsystem #:qchapi
:name "qchapi"
:description "A Restful Api for Qcha.es"
:author "Javier Olaechea <[email protected]>"
:version "20130401"
:serial t
:license "<3"
:depends-on (#:clack
#:ningle
#:yason
(defun create-pairs (lst)
"given a list, produces a list of all possible sets of pairings, without duplicates. "
(if (eq lst '())
*pair-list*
(progn
(dolist (item (cdr lst))
(push `(,(car lst) ,item) *pair-list*))
(create-pairs (cdr lst)))))
(defvar *pair-list*)
@PuercoPop
PuercoPop / tic-tac-toe.py
Created May 1, 2013 04:03
Grid best viewd in monospaced font
# -*- coding: utf-8 -*-
class IllegalMove(Exception):
pass
class TicTacToe(object):
player_1_symbol = "O"
player_2_symbol = "X"
empty_symbol = " "