]> gitweb.factorcode.org Git - factor.git/commitdiff
Constructors experiment
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Fri, 30 Jan 2009 20:40:08 +0000 (14:40 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Fri, 30 Jan 2009 20:40:08 +0000 (14:40 -0600)
extra/constructors/authors.txt [new file with mode: 0644]
extra/constructors/constructors-tests.factor [new file with mode: 0644]
extra/constructors/constructors.factor [new file with mode: 0644]

diff --git a/extra/constructors/authors.txt b/extra/constructors/authors.txt
new file mode 100644 (file)
index 0000000..d4f5d6b
--- /dev/null
@@ -0,0 +1 @@
+Slava Pestov
\ No newline at end of file
diff --git a/extra/constructors/constructors-tests.factor b/extra/constructors/constructors-tests.factor
new file mode 100644 (file)
index 0000000..367f0ad
--- /dev/null
@@ -0,0 +1,21 @@
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test constructors calendar kernel accessors
+combinators.short-circuit ;
+IN: constructors.tests
+
+TUPLE: stock-spread stock spread timestamp ;
+
+CONSTRUCTOR: stock-spread ( stock spread -- stock-spread )
+   now >>timestamp ;
+
+SYMBOL: AAPL
+
+[ t ] [
+    AAPL 1234 <stock-spread>
+    {
+        [ stock>> AAPL eq? ]
+        [ spread>> 1234 = ]
+        [ timestamp>> timestamp? ]
+    } 1&&
+] unit-test
\ No newline at end of file
diff --git a/extra/constructors/constructors.factor b/extra/constructors/constructors.factor
new file mode 100644 (file)
index 0000000..6968fd7
--- /dev/null
@@ -0,0 +1,21 @@
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: slots kernel sequences fry accessors parser lexer words
+effects.parser ;
+IN: constructors
+
+! An experiment
+
+: constructor-quot ( class slot-names body -- quot )
+    [ <reversed> [ setter-word '[ swap _ execute ] ] map [ ] join ] dip
+    '[ _ new @ @ ] ;
+
+: define-constructor ( name class effect body -- )
+    [ [ in>> ] dip constructor-quot ] [ drop ] 2bi
+    define-declared ;
+
+: CONSTRUCTOR:
+    scan-word [ name>> "<" ">" surround create-in ] keep
+    "(" expect ")" parse-effect
+    parse-definition
+    define-constructor ; parsing
\ No newline at end of file