Skip to content

Instantly share code, notes, and snippets.

View pbalduino's full-sized avatar

Plínio Balduino pbalduino

View GitHub Profile
@pbalduino
pbalduino / gist:4172386
Created November 29, 2012 22:37
cat /usr/share/calendar/calendar.lotr
$ cat /usr/share/calendar/calendar.lotr
/*
* Lord Of The Rings
*
* $FreeBSD: src/usr.bin/calendar/calendars/calendar.lotr,v 1.2 2003/10/09 00:31:48 grog Exp $
*/
#ifndef _calendar_lotr_
#define _calendar_lotr_
@pbalduino
pbalduino / haikai1.txt
Last active December 14, 2015 11:50
Haikai para o ar condicionado quebrado
mas que beleza
mão colando na mesa
quente demais
@pbalduino
pbalduino / contract.clj
Created April 4, 2013 13:41
Pre condition in Clojure
(defn foo [x]
{:pre [(> x 10)]}
(println x))
; output: 'user/foo
(foo 2)
; output: AssertionError Assert failed: (> x 10) user/foo (NO_SOURCE_FILE:1)
(foo 11)
; output: 11
@pbalduino
pbalduino / future-promise.clj
Created April 4, 2013 13:58
Future and promise in Clojure
(defn start-thread [f]
(.start (Thread. f)))
(let
[sum (future (apply + (range 1e7)))
answer (promise)]
(println "0. Started...")
(start-thread
@pbalduino
pbalduino / type-hint.clj
Created April 4, 2013 14:26
Type hint in Clojure
(defn len [x]
(.length x))
(defn len2 [^String x]
(.length x))
(time (reduce + (map len (repeat 1000000 "asdf"))))
; "Elapsed time: 8711.592674 msecs"
(time (reduce + (map len2 (repeat 1000000 "asdf"))))
@pbalduino
pbalduino / pmap.clj
Last active December 15, 2015 19:38
Parallel processing in Clojure
(defn sleep []
(Thread/sleep 1000))
(defn fast-computation [x]
(* x 2))
(defn heavy-computation [x]
(sleep)
(* x 2))
@pbalduino
pbalduino / memoization.clj
Last active December 16, 2015 04:39
Example of memoization use in Clojure
; First we create a function. Let's try Fibonacci
(defn fib [n]
(if (or (zero? n) (= n 1))
1
(+ (fib (dec n) ) (fib (- n 2)))))
; Se let's measure the time needed to run the function
(do
@pbalduino
pbalduino / memoization2.clj
Last active December 16, 2015 04:58
Another example of memoization with Clojure, also using Fibonacci
; without memoization
(defn fib [n]
(if (or (zero? n) (= n 1))
1
(+ (fib (dec n) ) (fib (- n 2)))))
(time (fib 90))
; spent more than 30 minutes and I quit
(use '[clojure.core.match :only (match)])
(doseq [n (range 1 101)]
(println
(match [(mod n 3) (mod n 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else n)))
@pbalduino
pbalduino / euler04-1.clj
Last active December 16, 2015 14:09
Trying to solve the Euler #04 The recursive way
(ns euler.euler04-1)
(defn peel
"Peel a string like a banana, removing first and last character"
[str]
(butlast (rest str)))
(defn palindrome?
"Returns true or false if name is a palindrome.
This recursive approach is slightly faster than compare with its reverse."