Skip to content

Instantly share code, notes, and snippets.

;; GRID is a vector of vectors that represents a game board
;; Example:
;; [[1 3 4 9 5] [5 1 2 4 7] [1 "@" 0 0 5] [4 2 3 9 1] [1 2 3 4 5]]
;; should print out
;; 13495
;; 51247
;; 1@ 5
;; 42391
;; 12345
;;
(defn set-thing-at-grid-position [grid position thing]
"Sets the position of grid to THING, then returns the new grid."
(let [x (first position)
y (nth position 1)]
(into (conj (subvec grid 0 y)
(into (conj (subvec (first (subvec grid y (inc y))) 0 x)
thing)
(subvec (first (subvec grid y (inc y))) (inc x))))
(subvec grid (inc y)))))
@echosa
echosa / typed clojure issue.clj
Created March 5, 2014 00:41
An issue I'm having with typed Clojure
(ns greed.coords
(:require [clojure.core.typed :refer [ann]]))
(ann get-next-coordinate-northeast
[NonEmptySeqable (Option Number) (Option Number) -> (Option (IPersistentList Number))])
(defn get-next-coordinate-northeast [grid x y]
(when-not (or (nil? x) (nil? grid) (>= x (- (count (first grid)) 1))
(nil? y) (<= y 0))
(list (+ x 1) (- y 1))))
@echosa
echosa / zero-path-between.clj
Created March 12, 2014 14:15
This is a function I can't annotate correctly. The given annotation is how the function is currently used.
(ann zero-path-between
[(IPersistentVector (IPersistentVector Any)) Symbol (Vector* Number Number)
(Vector* Number Number)
->
(IPersistentVector (IPersistentVector Any))])
(defn zero-path-between
"Traverses the path between start and end and changes each position to 0."
[grid direction start end]
(let [next (get-next-coordinate grid start direction)]
(if (and next (not= next end))
@echosa
echosa / core.typed-deprecated-warning.clj
Created March 12, 2014 15:12
This results in a deprecated warning for fn>. Posting here to link to a Jira ticket.
(ann get-number-of-cleared-spaces
[(IPersistentVector (IPersistentVector Any))
->
AnyInteger])
(defn get-number-of-cleared-spaces
"Returns the number of cleared spaces in the grid."
[grid]
(inc (reduce (fn> [[cleared :- AnyInteger]
[row :- (IPersistentVector Any)]]
(let [result (inc (reduce (fn> [[row-cleared :- AnyInteger]
@echosa
echosa / UserAssignment.php
Created March 12, 2014 22:55
User class for an assignment I was given
<?php
/**
* A few notes on this code.
*
* First, I installed PHPUnit via composer to run the tests. This is the reason for the
* require_once.
*
* Second, I made this all one file (multiple classes) in order to make it easier to
* post to a gist. Normally, of course, I'd have separate paths/files for different
* classes. If you put this all into one file to check/run with phpunit, you'll need to
<?php
/**
* For easy of posting to Gist, I have put everything in one file called
* knight.php. The getValidKnightSquares() function is first in the file.
*
* The require_once is there to load PHPUnit installed with Composer. There is a
* unit test class for the getValidKnightSquares() function at the end of this
* file.
*/
namespace Chess;
<html>
<head>
<style type="text/css">
.container {
width: 100%;
border: 1px solid black;
background: #CCCCCC;
}
.theFloat {
(ann player-character String)
(def player-character
"The character that represents the player."
"@")
(ann find-player
[(IPersistentVector (IPersistentVector Any))
->
(Vector* Number Number)])
(defn find-player
@echosa
echosa / trail
Created July 10, 2014 14:25
Delete trailing spaces in Acme
#!/usr/bin/env bash
# Removes trailing spaces from entire window.
# Run as `trail` (no | or < necessary)
# Inspired by:
# http://www.mostlymaths.net/2013/03/extensibility-programming-acme-text-editor.html
echo -n "0,$" | 9p write acme/$winid/addr
9p read acme/$winid/body | sed 's/ *$//g' | 9p write acme/$winid/data
echo -n "0,0" | 9p write acme/$winid/addr