Skip to content

Instantly share code, notes, and snippets.

@rgantt
rgantt / abs-fatal.php
Created August 18, 2011 20:51
php 5.4 traits
<?php
/**
* This gives a fatal error
*/
class AbsTest {
abstract public function saySomething( $something );
public function saySomething( $something ) {
echo "{$something}\n";
}
}
@rgantt
rgantt / closure-by-reference.php
Created August 9, 2011 15:27
anonymous recursion in php
<?php
$fibonacci = function( $n ) use ( &$fibonacci ) {
return ( $n < 2 ) ? $n : ( $fibonacci( $n - 1 ) + $fibonacci( $n - 2 ) );
};
echo $fibonacci(4); // Outputs "3" (0,1,1,2,3)
;; rgantt's solution to Fibonacci Sequence
;; https://4clojure.com/problem/26
(fn[l]
(map
(fn fib[n] (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2)))))
(range l))
)
@rgantt
rgantt / subpath.java
Created July 20, 2011 03:04
the subpath algorithm
/**
* visitedSoFar contains all nodes that have been traversed
* bestPathSoFar should be populated with only the subpath of visitedSoFar that contains no dead ends
*/
for( int j = 0; j < visitedSoFar.size(); j++ ) {
for( int i = visitedSoFar.size() - 1; i > j; i-- ) {
if( visitedSoFar.get( i ).equals( visitedSoFar.get( j ) ) ) {
j = i;
}
@rgantt
rgantt / basic-global.php
Created July 15, 2011 04:47
Anonymous functions and closure in PHP
<?php
$count = 0;
function increase_count() {
global $count;
$count++;
}
increase_count();
echo $count;
@rgantt
rgantt / profile.php
Created June 14, 2011 21:39
dead simple profiling in PHP 5.3
<?php
function profile( $message, $closure ) {
$start = microtime( true );
$closure( $start );
$duration = ( microtime( true ) - $start );
return "{$message} {$duration}\n";
}
@rgantt
rgantt / compile_and_route_articles.rb
Created June 7, 2011 17:57
Compiling and routing examples - nanoc
# run index.html through erb
compile '/' do
filter :erb
layout 'page'
end
# run the articles through textile
compile '/articles/*' do
filter :redcloth
layout 'article'
@rgantt
rgantt / sextile-excerpt.php
Created June 5, 2011 20:31
sextile excerpt
<?php
class sextile {
protected $textile;
public function TextileThis( $text, $lite = '', $encode = '', $noimage = '', $strict = '', $rel = '' ) {
return $this->textile->TextileThis( $this->handle_latex( $text ), $lite, $encode, $noimage, $strict, $rel );
}
/**
* Spoof interface compatibility