April 23, 2016
16 middle
142 youth
130 strong
3 Resembling
11 hill,
52 heavenly
1 steep-up
1 climbed
17 majesty,
46 sacred
132 looks
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
}
]
}
FOO = TABLE()
FOO<key> = value
$x = 1
@y = [
%z = {
'foo', 'bar',
'baz', 'qux',
};
$z{foo} = 3;
data = open(filename).read()
lines = data.split('\n')
for line in lines:
words = line.split(' ')
for word in words:
print word
#! /usr/bin/env python
import re
import sys
#! /usr/bin/env gforth
if len(sys.argv) != 2:
print 'Usage: ./ten.py <src>'
sys.exit(1)
: usage ." Usage: ten.fs <src>" cr bye ;
: usage-check argc @ 2 <> if usage then ;
usage-check
data = open(sys.argv[1]).read()
1 arg slurp-file
data = re.sub('[\n\r\t ]+', ' ', data)
words = [i for i in data.split(' ') if i != '']
variable front
variable back
variable on-word
: space? ( ch -- ) dup 32 = over 10 = or over 13 = or swap 8 = or ;
: this-word ( -- a n ) front @ back @ over - ;
: process-word this-word dup if on-word @ execute else 2drop then ;
: chomp ( a -- ) back ! process-word back @ 1+ front ! ;
: process-char ( a -- ) dup c@ space? if chomp else drop then ;
: split-words ( a n -- )
over front ! over + dup rot ?do i process-char loop chomp ;
tally = {}
for word in words:
if word not in tally:
tally[word] = 0
tally[word] += 1
variable word-list
: visit-list ( a op -- )
begin over while dup >r over @ -rot execute r> repeat 2drop ;
: end-visit ( op a -- ) drop 0 ;
: add-word ( a n -- ) here >r word-list @ , , , 1 , r> word-list ! ;
: word>name ( a -- a n ) dup cell+ cell+ @ swap cell+ @ ;
: word>count ( a -- a ) 3 cells + ;
variable hash-value
: 0hash 0 hash-value ! ;
: +hash1 ( n -- )
hash-value @ + dup 10 lshift + dup 6 rshift xor hash-value ! ;
: +hash ( a n -- ) over + swap ?do i c@ +hash1 loop ;
: nhash ( n -- n ) hash-value @ swap mod ;
50000000 constant table-size
table-size cells allocate throw constant word-table
word-table table-size cells 0 fill
: table-cell ( a n -- a ) 0hash +hash table-size nhash cells word-table + ;
: add-to-table ( a a n -- ) table-cell ! ;
: get-from-table ( a n -- a ) table-cell @ ;
variable target variable target-len
variable found
: match-word ( a -- )
dup word>name target @ target-len @ compare 0=
if found ! end-visit else drop then ;
: find-word ( a n -- )
target-len ! target ! 0 found !
target @ target-len @ get-from-table
['] match-word visit-list ;
: handle-word ( a n -- )
2dup find-word found @ if 2drop 1 found @ word>count +!
else 2dup add-word word-list @ -rot add-to-table then ;
' handle-word on-word !
for word in tally.keys():
print tally[word], word
: dump-word ( a -- ) dup word>count @ . word>name type cr ;
word-list @ ' dump-word visit-list
Slides at:
github.com/flagxor