Skip to content

Instantly share code, notes, and snippets.

View jneira's full-sized avatar
:octocat:

Javier Neira jneira

:octocat:
View GitHub Profile
(define (member-of-2? a lst1 lst2)
(if (or (null? lst1) (null? lst2)) #f
(or
(and (eq? a (car lst1))
(or (eq? a (car lst2))
(member-of-2? a (list a) (cdr lst2))))
(member-of-2? a (cdr lst1) lst2))))
type Instrument = String
type Account = String
type Quantity = BigDecimal
type Money = BigDecimal
import java.util.{Calendar, Date}
val today = Calendar.getInstance.getTime
case class Trade(ref: String, ins: Instrument, account: Account, unitPrice: Money,
quantity: Quantity, tradeDate: Date = today) {
def ifM[B](t: => M[B], f: => M[B])(implicit a: Monad[M], b: A <:< Boolean): M[B] =
>>= ((x: A) => if (x) t else f)
;;1.45 Definir una macro MI-DO que tenga exactamente la misma funcionalidad que la macro DO,
;;pero que además de devolver el valor correspondiente cuando se cumpla la condición de finalización,
;;devuelva un segundo valor que indique el número de iteraciones que se han realizado. No se deben
;;utilizar las primitivas DO, DO*, DOLIST, DOTIMES.
(defmacro iterar (steps final &rest body)
`(labels ((main ,(mapcar #'car steps)
(if ,(car final) ,(cadr final)
(progn ,@body
(main ,@(apply #'append
@jneira
jneira / greed.clj
Created January 5, 2011 20:00 — forked from xaviuzz/greed.rb
;; Ultimo commit con propuestas brillantes de http://twitter.com/_ogf_
(defn score-single [ps]
(case ps 1 100 5 50 0))
(defn score-triple [ps]
(case ps 1 1000 (* ps 100)))
(defn scores-by-freq [[ps freq]]
[(* (quot freq 3)
(score-triple ps))
(ns string-calc)
(defprotocol Adder
(add [self]))
(extend-type java.lang.String
Adder
(add [s]
(apply + (map #(Integer/parseInt %)
(.split s ",")))))
;;1.48 No todos los sistemas de LISP implementan el mecanismo
;;Backquote. Definir BACKQUOTE de forma que tenga el mismo
;;efecto que Backquote y permita manejar de forma apropiada
;;expresiones como COMA y COMA-AT de la forma que se
;;muestra en el siguiente ejemplo:
;;> (BACKQUOTE (A B (LIST ‘C ‘D) (COMA (LIST ‘E ‘F)
;; (COMA-AT (LIST ‘G ‘H)))
;;(A B (LIST ‘C ‘D) (E F) G H)
(defn fizz-buzz [n]
(str (when (zero? (mod n 3)) "fizz")
(when (zero? (mod n 5)) "buzz")))
(defn fizz-buzz [n]
(let [when-div-by
#(when (zero? (mod n %1)) %2)
join str]
(join (when-div-by 3 "fizz")
(when-div-by 5 "buzz"))))
(ns roman-numerals
(:use clojure.test))
(def digits ["I" "IV" "V" "IX" "X" "XL" "L" "XC" "C" "CD" "D" "CM" "M"])
(def digits-ints (zipmap [1 4 5 9 10 40 50 90 100 400 500 900 1000] digits))
(defn floor [nums n]
(first (filter #(<= % n) (reverse (sort nums)))))
(defn from-int [n]
(defpackage game-of-life)
(defun get-neighbours (grid x y)
(let ((result 0) (xo (- x 1))
(yo (- y 1)))
(dotimes (i 3)
(dotimes (j 3)
(let ((dx (+ xo i))
(dy (+ yo j)))
(when (and (array-in-bounds-p grid dx dy)