Skip to content

Commit

Permalink
Add a bunch of stuff that's been lying around my disk forever
Browse files Browse the repository at this point in the history
  • Loading branch information
offby1 committed Jun 4, 2013
1 parent 84dd72c commit fc79d7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
24 changes: 24 additions & 0 deletions autogram/ruby/autogram.rb
@@ -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.")
42 changes: 42 additions & 0 deletions autogram3/a3.rkt
@@ -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)))))
1 change: 1 addition & 0 deletions praxis/.gitignore
@@ -0,0 +1 @@
cover_db/

0 comments on commit fc79d7c

Please sign in to comment.