Adding hats

db4
Alex Chapman 2008-10-14 15:05:05 +11:00
parent c40591af2e
commit 868ad06426
4 changed files with 146 additions and 0 deletions

1
extra/hats/authors.txt Normal file
View File

@ -0,0 +1 @@
Alex Chapman

View File

@ -0,0 +1,87 @@
! Copyright (C) 2008 Alex Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: boxes hats kernel namespaces symbols tools.test ;
IN: hats.tests
SYMBOLS: lion giraffe elephant rabbit ;
! caps
[ rabbit ] [ rabbit <cap> out ] unit-test
[ rabbit ] [ f <cap> rabbit in out ] unit-test
[ rabbit ] [ rabbit <cap> take ] unit-test
[ f ] [ rabbit <cap> empty-hat out ] unit-test
[ rabbit f ] [ rabbit <cap> [ take ] keep out ] unit-test
[ rabbit t ] [ rabbit <cap> [ take ] keep empty-hat? ] unit-test
[ lion ] [ rabbit <cap> [ drop lion ] change-hat out ] unit-test
! bowlers
[ giraffe ] [ [ giraffe rabbit set rabbit <bowler> out ] with-scope ] unit-test
[ rabbit ]
[
[
lion rabbit set [
rabbit rabbit set rabbit <bowler> out
] with-scope
] with-scope
] unit-test
[ rabbit ] [
rabbit <bowler>
[
lion rabbit set [
rabbit rabbit set out
] with-scope
] with-scope
] unit-test
[ elephant ] [
rabbit <bowler>
[
elephant rabbit set [
rabbit rabbit set
] with-scope
out
] with-scope
] unit-test
[ rabbit ] [
rabbit <bowler>
[
elephant in [
rabbit in out
] with-scope
] with-scope
] unit-test
[ elephant ] [
rabbit <bowler>
[
elephant in [
rabbit in
] with-scope
out
] with-scope
] unit-test
! Top Hats
[ lion ] [ lion rabbit set-global rabbit <top-hat> out ] unit-test
[ giraffe ] [ rabbit <top-hat> giraffe in out ] unit-test
! Tuple hats
TUPLE: foo bar ;
C: <foo> foo
: test-tuple ( -- tuple )
rabbit <foo> ;
: test-slot-hat ( -- slot-hat )
test-tuple 2 <slot-hat> ; ! hack!
[ rabbit ] [ test-slot-hat out ] unit-test
[ lion ] [ test-slot-hat lion in out ] unit-test
! Boxes as hats
[ rabbit ] [ <box> rabbit in out ] unit-test
[ <box> rabbit in lion in ] must-fail
[ <box> out ] must-fail

57
extra/hats/hats.factor Normal file
View File

@ -0,0 +1,57 @@
! Copyright (C) 2008 Alex Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors boxes kernel namespaces ;
IN: hats
! Bullwinkle: Hey Rocky, watch me pull a rabbit out of my hat!
! Rocky: But that trick never works!
! Bullwinkle: This time for sure!
! hat protocol
MIXIN: hat
GENERIC: out ( hat -- object )
GENERIC: (in) ( object hat -- )
: in ( hat object -- hat ) over (in) ; inline
: empty-hat? ( hat -- ? ) out not ; inline
: empty-hat ( hat -- hat ) f in ; inline
: take ( hat -- object ) dup out f rot (in) ; inline
: change-hat ( hat quot -- hat )
over >r >r out r> call r> swap in ; inline
! caps (the simplest of hats)
TUPLE: cap object ;
C: <cap> cap
M: cap out ( cap -- object ) object>> ;
M: cap (in) ( object cap -- ) (>>object) ;
INSTANCE: cap hat
! bowlers (dynamic variable hats)
TUPLE: bowler variable ;
C: <bowler> bowler
M: bowler out ( bowler -- object ) variable>> get ;
M: bowler (in) ( object bowler -- ) variable>> set ;
INSTANCE: bowler hat
! Top Hats (global variable hats)
TUPLE: top-hat variable ;
C: <top-hat> top-hat
M: top-hat out ( top-hat -- object ) variable>> get-global ;
M: top-hat (in) ( object top-hat -- ) variable>> set-global ;
INSTANCE: top-hat hat
USE: slots.private
! Slot hats
TUPLE: slot-hat tuple slot ;
C: <slot-hat> slot-hat
: >slot-hat< ( slot-hat -- tuple slot ) [ tuple>> ] [ slot>> ] bi ; inline
M: slot-hat out ( slot-hat -- object ) >slot-hat< slot ;
M: slot-hat (in) ( object slot-hat -- ) >slot-hat< set-slot ;
INSTANCE: slot-hat hat
! Put a box on your head
M: box out ( box -- object ) box> ;
M: box (in) ( object box -- ) >box ;
INSTANCE: box hat

1
extra/hats/summary.txt Normal file
View File

@ -0,0 +1 @@
A protocol for getting and setting