]> gitweb.factorcode.org Git - factor.git/commitdiff
english: change comma-list to fit my brain better.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 15 Feb 2018 21:47:23 +0000 (13:47 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 15 Feb 2018 21:47:23 +0000 (13:47 -0800)
We can always map-index in the caller if you want it before passing the parts in.

basis/english/english-tests.factor
basis/english/english.factor

index 5708201515ddfe32ea77e449d4d78c31433c6f7f..c4ee93ce1f0172e2f57981091f814ba2d74f5d5e 100644 (file)
@@ -87,29 +87,27 @@ IN: english
 { "the" } [ "geese" ?plural-article ] unit-test
 { "a" }   [ "sheep" ?plural-article ] unit-test
 
-{ { "" } }
-    [ { } "or" simple-comma-list ] unit-test
+{ { } } [ { } "or" comma-list ] unit-test
 
-{ { { "a" } } }
-    [ { "a" } "or" simple-comma-list ] unit-test
+{ { "a" } } [ { "a" } "or" comma-list ] unit-test
 
-{ { { "a" } { " or " "b" } } }
-    [ { "a" "b" } "or" simple-comma-list ] unit-test
+{ { "a" " or " "b" } }
+[ { "a" "b" } "or" comma-list ] unit-test
 
-{ { { "a" ", " } { "b" } { ", or " "c" } } }
-    [ { "a" "b" "c" } "or" simple-comma-list ] unit-test
+{ { "a" ", " "b" ", or " "c" } }
+[ { "a" "b" "c" } "or" comma-list ] unit-test
 
-{ { { "a" ", " } { "b" ", " } { "x" } { ", and " "c" } } }
-    [ { "a" "b" "x" "c" } "and" simple-comma-list ] unit-test
+{ { "a" ", " "b" ", " "x" ", and " "c" } }
+[ { "a" "b" "x" "c" } "and" comma-list ] unit-test
 
 { {
-    { "an " { $link object } ", " }
-    { "a " { $link pair } ", " }
-    { "a " { $link number } ", " }
-    { "an " { $link array } ", " }
-    { "a " { $link string } ", " }
-    { "a " { $link sequence } }
-    { ", or " "an " { $link assoc } }
+    { "an " { $link object } } ", "
+    { "a " { $link pair } } ", "
+    { "a " { $link number } } ", "
+    { "an " { $link array } } ", "
+    { "a " { $link string } } ", "
+    { "a " { $link sequence } } ", or "
+    { "an " { $link assoc } }
 } } [
     { object pair number array string sequence assoc }
     or-markup-example
@@ -121,6 +119,5 @@ IN: english
 } } [ { object } or-markup-example ] unit-test
 
 { {
-    { "an " { $link object } }
-    { " or " "a " { $link pair } }
+    { "an " { $link object } } " or " { "a " { $link pair } }
 } } [ { object pair } or-markup-example ] unit-test
index 55a8840c20c726d180c6911f75d94dc4c1ff24b5..64b72213f25b9ff7bdc9c940c15637181ba89bbd 100644 (file)
@@ -168,32 +168,23 @@ PRIVATE>
 : ?plural-article ( word -- article )
     dup singular? [ a/an ] [ drop "the" ] if ;
 
-: comma-list-by ( parts quot conjunction  -- clause-seq )
-    ! using map-index for maximum flexibility
-    [ '[ _ call( x n -- x ) ] map-index
-        ! not using oxford comma for 2 objects: "a, or b"; instead it's "a or b"
-        dup length 2 > ", " " " ? " "
-    ] dip glue
-
-    over length {
-        { [ dup 0 = ] [ 3drop { "" } ] }
-        { [ dup 1 = ] [ 2drop ] }
-        [ drop [ unclip-last [
-                dup length 1 - '[ _ - zero? [ ", " suffix ] unless ] map-index
-            ] dip ] dip prefix suffix
-        ]
-    } cond ;
-
-: simple-comma-list ( parts conjunction -- parts' )
-    [ drop 1array ] swap comma-list-by ;
+: comma-list ( parts conjunction  -- clause-seq )
+    [
+        V{ } clone [
+            [ '[ ", " _ push ] ] [ '[ _ push ] ] bi interleave
+        ] keep { } like
+    ] dip over length dup 3 >= [
+        [ 3 > ", " " " ? " " surround ] [ 2 - pick set-nth ] bi
+    ] [ 2drop ] if ;
 
 : or-markup-example ( classes -- markup )
-    [ drop dup word? [
+    [
+        dup word? [
             [ name>> a/an " " append ] [ \ $link swap 2array ] bi 2array
         ] [
             [ "\"" ?head drop a/an ] keep 1array \ $snippet prefix " " swap 3array
         ] if
-    ] "or" comma-list-by ;
+    ] map "or" comma-list ;
 
 : $or-markup-example ( classes -- )
     or-markup-example print-element ;