Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a bunch of stuff that's been lying around my disk forever
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env ruby1.9 | ||
|
||
def histogram(string) | ||
lettermap = Hash.new{|h,k|h[k] = 0} | ||
string.chars.each do |c| | ||
lettermap[c] += 1 | ||
end | ||
lettermap | ||
end | ||
|
||
def expand_template(letters, template) | ||
histogram(template) | ||
end | ||
|
||
def update_histogram(letters, template) | ||
next_histogram = expand_template(letters, template) | ||
|
||
letters.each do |letter, count| | ||
puts "#{letter}: #{count} => #{next_histogram[letter]}" | ||
end | ||
end | ||
|
||
h = histogram("This sentence has yadda yadda.") | ||
update_histogram(h, "This sentence has yadda yadda.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#! /bin/sh | ||
#| Hey Emacs, this is -*-scheme-*- code! | ||
PLTSTDERR=debug exec racket -l errortrace --require "$0" --main -- ${1+"$@"} | ||
|# | ||
|
||
#lang racket | ||
|
||
(require unstable/debug | ||
(planet neil/numspell/numspell) | ||
(planet dherman/memoize)) | ||
|
||
(define (add-two-hashes h1 h2) | ||
(for/fold ([rv h1]) | ||
([(k v) (in-dict h2)]) | ||
(hash-update rv k (curry + v) 0))) | ||
|
||
(define (add-hashes . hashes) | ||
(cond | ||
((null? hashes) | ||
(make-immutable-hash)) | ||
((null? (cdr hashes)) | ||
(car hashes)) | ||
(else | ||
(foldl add-two-hashes (car hashes) (cdr hashes))))) | ||
|
||
(define (string->hist str) | ||
(for/fold ([rv (make-immutable-hash)]) | ||
([ch str]) | ||
(hash-update rv ch add1 0))) | ||
|
||
(define (char->string ch histogram) | ||
(number->english (dict-ref histogram ch))) | ||
|
||
(let ([template (list "This sentence has " #\e "s.")]) | ||
|
||
(let-values ([(chars strings) (partition char? template)]) | ||
(let loop () | ||
(let* ([current-hist (apply add-hashes (map string->hist strings))] | ||
[updated-hist (map (curryr char->string current-hist) chars)]) | ||
(append | ||
updated-hist | ||
strings))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cover_db/ |