Skip to content

Instantly share code, notes, and snippets.

@jdoig
jdoig / nicerImmutable.cs
Created February 21, 2012 21:54
some .NET 4.0 syntax sugar for immuable state
class Program
{
static void Main(string[] args)
{
var james = new Person(
name: "James",
age: 31,
deceased: false);
}
}
@jdoig
jdoig / oops.rb
Created November 9, 2011 19:02
Total bastardization of day 3
def get_filename(obj)
filename = obj.to_s.downcase + '.txt'
end
def read_line(line)
line.chomp.split(', ')
end
module ActsAsCsv
def self.included(base)
@jdoig
jdoig / 1-each.rb
Created October 29, 2011 12:24
7 Languages in 7 Weeks: Day 2
(1..16).to_a.each {|i| i % 4==0 ? puts(i) : print(i) }
@jdoig
jdoig / 1-WordUp.rb
Created October 28, 2011 16:56
7 Languages in 7 Weeks: Day 1
puts 'Hello, World'
@jdoig
jdoig / xml_test.clj
Created October 3, 2011 22:34
XML to Json with some transformation
(ns xml_test.core
(:require [clojure.zip :as zip]
[clojure.xml :as xml]
[clj-json.core :as json])
(:use clojure.contrib.zip-filter.xml))
(defn -main [& args]
(let [paintings (zip/xml-zip (xml/parse "paintings.xml"))]
(->> (xml-> paintings :painting)
(map (fn [painting] { :name (first (xml-> painting :img (attr :alt)))
@jdoig
jdoig / Tools.fs
Created July 29, 2011 10:23
Tools for generic pathfinding
module Tools
open System
let sqr x = float(x * x)
let rec remove l predicate =
match l with
| [] -> []
| hd::tl -> if predicate(hd) then
(remove tl predicate)
@jdoig
jdoig / Pathfinding.fs
Created July 29, 2011 10:17
Generic Pathfinding implimentation
module Pathfinding
open Level
open Tools
(* a wrapper for mapPoint that can contain pathing data as per typical A* pathfinding *)
(* g = cost of path so far, h = estimated cost to goal, parent = tile we came here from *)
type PathingNode =
{point:MapPoint; h:float; g:float; parent:PathingNode option}
(* returns a pathnode based on a given map point *)
@jdoig
jdoig / Program.fs
Created July 29, 2011 10:10
Main program file of first attempt at path finding in F#
open Tools
open Level
open Pathfinding
let level1 = {width=5;height=9;map=(loadMap @"C:\Test\lvl1.txt" |> Seq.toList)}
let start = level1.GetElement 4 8
let goal = level1.GetElement 4 0
let path = pathFind(level1,goal,start,[{point=start;h=start.Distance goal;g=0.0;parent=None}],[])
let rec readPath (path:PathingNode, list:PathingNode list) =
@jdoig
jdoig / Level.fs
Created July 29, 2011 09:57
2D tilemap Level for pathfinding code
module Level
open Tools
type MapPoint =
{x:int;y:int;value:int} (* a point in 2d map space *)
(*Calculate distance to other map point*)
member this.Distance mp = sqrt (sqr(this.x+mp.x) + sqr(this.y+mp.y))
(*Simple construct to hold the 2D map data*)
type Map =
@jdoig
jdoig / Pathfinding.fs
Created July 29, 2011 07:12
First attempt at pathfinding in F#
module Pathfinding
open Level
open Tools
(* a wrapper for mapPoint that can contain pathing data as per typical A* pathfinding *)
(* g = cost of path so far, h = estimated cost to goal, parent = tile we came here from *)
type PathingNode =
{point:MapPoint; h:float; g:float; parent:PathingNode option}
member this.f = this.g + this.h