Skip to content

Instantly share code, notes, and snippets.

@jdoig
jdoig / routes.clj
Created July 28, 2011 10:30
Very simple scooter hire example
(ns webapp.routes
(:use compojure.core
hiccup.core)
(:require [clj-http.client :as client]
[compojure.route :as route]
[clj-json.core :as json]))
;Make everything £40 for simplicity
(defn calculate-price [vehicle]
(assoc vehicle :price-per-day 40))
@jdoig
jdoig / sdl_demo.fs
Created July 28, 2011 23:27
sdl demo using F#
#light
open SdlDotNet.Core;
open SdlDotNet.Graphics;
open SdlDotNet.Graphics.Sprites;
open SdlDotNet.Input;
open System.Drawing;
open System;
type StuperGario = class
val mutable location : Point
@jdoig
jdoig / Tools.fs
Created July 29, 2011 07:08
Tools for the first pass of my pathfinding
module Tools
open System
let sqr x = float(x * x) (* Square two ints into a float *)
(* Remove element where predicate *)
let rec remove l predicate =
match l with
| [] -> []
| hd::tl -> if predicate(hd) then
@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
@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 / 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 / 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 / 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 / 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 / 1-WordUp.rb
Created October 28, 2011 16:56
7 Languages in 7 Weeks: Day 1
puts 'Hello, World'