Skip to content

Instantly share code, notes, and snippets.

View fogus's full-sized avatar
💭
attempting to learn how to better learn

Fogus fogus

💭
attempting to learn how to better learn
View GitHub Profile
def ∑(r:Range)(f:Int =>Int)=r.reduceLeft(_+ f(_))
def ∏(r:Range)(f:Int =>Int)=r.reduceLeft(_* f(_))
// And now we can write:
val s= ∑(1 to 100)(x=>x^2)
val p= ∑(1 to 100 by 2)(x=>x^2)
val y= ∏(1 to 30 by 3)(_)
@fogus
fogus / same-diagonal.rkt
Created August 11, 2011 20:32
Function to chech if 2 chessboard squares are in the same diagonal
(define (in-same-diagonal cell1 cell2)
(let((diff (map (compose abs -) cell1 cell2)))
(and (not (zero? (cadr diff)))
(= 1 (apply quotient diff))
(= 0 (apply modulo diff)))))
@fogus
fogus / maze.clj
Created August 13, 2011 14:19 — forked from cgrand/maze.clj
A maze generator (Wilson's algorithm) which can work with any topography (hextiles, torus variants, teapot etc.)
; http://groups.google.com/group/clojure/browse_thread/thread/974e2c7f89e27231/5f4bff3e58dfa36f
; output images http://i.imgur.com/gSASS.png (square maze)
; http://i.imgur.com/uEqaq.png (hex maze)
;; generic Wilson's algorithm implementation
(defn maze
"Returns a random maze carved out of walls; walls is a set of
2-item sets #{a b} where a and b are locations.
The returned maze is a set of the remaining walls."
[walls]
@fogus
fogus / express-sample.cljs
Created August 25, 2011 21:25 — forked from jneira/express-sample.cljs
Clojurescript / node.js basic examples
(ns express_sample
(:require [cljs.nodejs :as node]))
(def express (node/require "express"))
(def app (. express (createServer)))
(defn -main [& args]
(doto app
(.use (. express (logger)))
(.get "/" (fn [req res]
@fogus
fogus / delimc.lisp
Created August 26, 2011 14:47 — forked from manuel/delimc.lisp
Delimited control example
(run-cc
($lambda ()
($let ((p (make-prompt)))
(+ 2 ($push-prompt p
($if (with-sub-cont p
($lambda (k)
(+ ($push-sub-cont k #f)
($push-sub-cont k #t))))
3
4))))))
(ns fj
(:import [java.util.concurrent RecursiveTask
ForkJoinPool]))
(set! *warn-on-reflection* true)
;; -----------------------------------------------
;; Helpers to provide an idiomatic interface to FJ
(defprotocol IFJTask
@fogus
fogus / build.sh
Created August 26, 2011 18:08 — forked from headius/build.sh
Building the Hotspot compiler team's Hotspot branch alone
#!/bin/sh
# This script is for building the hsx/hotspot-comp branch in isolation.
#
# Prerequisites:
# * appropriate build tools
# * a working Sun Java 6 build (i.e. JDK6) is available at:
# HOME/java/re/j2se/1.6.0/latest/binaries/linux-[i586/amd64]
# * a working Sun Java 7 build (i.e. JDK7) is available at:
# HOME/java/re/j2se/1.7.0/latest/binaries/linux-[i586/amd64]
# HOME/java/re/j2se/1.7.0/promoted/latest/binaries/linux-[i586/amd64]
@fogus
fogus / gameoflife.scala
Created August 30, 2011 18:30 — forked from gclaramunt/gameoflife.scala
Game of Life in Scala
package gameoflife
class Board[T](val b:Array[Array[T]]) {
val h=b.size
val w=b(0).size
def conv(idx:Int,max:Int)=if (idx<0) (max-Math.abs(idx %max) ) else (idx % max )
def apply(x:Int,y:Int)=b(conv(x,h))(conv(y,w))
def update(x:Int,y:Int,value:T)= b(conv(x,h))(conv(y,w))=value
@fogus
fogus / symbols.scala
Created August 30, 2011 18:40 — forked from gclaramunt/symbols.scala
Pi and Square root
val Π = Math.Pi
def √(x:Double)=Math.sqrt(x)
val x= √(9*Π)
@fogus
fogus / forth.rb
Created August 30, 2011 18:40 — forked from perimosocordiae/forth.rb
A pure Ruby interpreter for Forth rr
#!/usr/bin/env ruby
# the stack
$stack = []
def pop() $stack.pop || ufe end
def push(f) $stack<<f end
# poor man's Exception class
def ufe() raise("Stack underflow") end
# lambda constructor helpers