Skip to content

Instantly share code, notes, and snippets.

View mneedham's full-sized avatar

Mark Needham mneedham

View GitHub Profile
column_1 column_2
mark random
mark something_else
dave
Only care about column_1 uniqueness but want to print out column 2 in the result
Output
1 dave
I have a map function which returns the data like this:
Key1 -> Doc1
Key2 -> Doc2
Key1 -> Doc3
What I want to get is:
Key1 -> [Doc1, Doc3]
Key2 -> [Doc2]
PipedOutputStream poStream = new PipedOutputStream();
PipedInputStream piStream = new PipedInputStream();
System.setIn(piStream);
poStream.connect(piStream);
poStream.write("19".getBytes(), 0, 1);
Program.main();
Program
@mneedham
mneedham / gist:941867
Created April 26, 2011 06:18
Purely Functional Data Structures - Chris Okasaki
Write a function suffixes of type that takes a list xs & returns a list of all the suffixes of xs in decreasing order of length
For example:
suffixes [1, 2, 3, 4] => [[1, 2, 3, 4], [2, 3, 4], [3, 4], [4], []]
Non tail recursive:
let rec suffixes list =
match list with
| [] -> [[]]
@mneedham
mneedham / gist:941892
Created April 26, 2011 06:39
Purely Functional Data Structures - Chris Okasaki
let suffixesTR list =
let rec loop l acc =
match l with
| [] -> acc
| [last] -> loop [] (List.concat [acc; [[last]]; [[]]])
| hd::tl -> loop tl (List.concat [acc; [l]])
in
loop list []
(defproject test-project "0.1.0"
:description "Test Project"
:dependencies [[org.clojure/clojure "1.2.0"],
[org.clojure/clojure-contrib "1.2.0"]]
:dev-dependencies [[swank-clojure "1.2.0"]])
Wanted to understand why this fails to compile:
List(("Mark", 4), ("Charles", 5)).filter(case(name, number) => number == 4)
:1: error: illegal start of simple expression
List(("Mark", 4), ("Charles", 5)).filter(case(name, number) => number == 4)
Yet this works fine:
List(("Mark", 4), ("Charles", 5)).filter{case(name, number) => number == 4}
(defn yaks [doc]
; ...
{:tag :Yaks :content[(map (ƒ [s] {:tag :Yak :content [s]}) yaks)]
(defn build-map [doc]
{:tag :meta :content
[{:tag :Foo :content [(xml1-> doc zf/descendants :Bar text)]}
{:tag :Bar :content [(xml1-> doc zf/descendants :Foo text)]}
{:tag :Baz :content [(xml1-> doc :Baz text)]}
class ContainsElementMatchingCriteria {
def apply[T](allCriteria: Seq[Criteria[T]]) = {
new Matcher[Seq[T]] {
def apply(actual: Seq[T]) = {
MatchResult(
actual.exists((result: T) => allCriteria.forall(_.matches(result))),
"No search result exists with criteria " + allCriteria + " in " + actual,
"An element exists with criteria " + allCriteria + " in " + actual
)
}
@mneedham
mneedham / trie.rb
Created November 27, 2011 22:43
Incomplete implementation of a trie in ruby
class Trie
def initialize()
@trie = Hash.new()
end
def build(str)
chars = str.chars
chars.inject(@trie) do |prev, char|
unless prev[char]
prev[char] = Hash.new()