english: change comma-list to fit my brain better.

We can always map-index in the caller if you want it before passing the parts in.
windows-high-dpi
John Benediktsson 2018-02-15 13:47:23 -08:00
parent f960c51878
commit be657a20fb
2 changed files with 27 additions and 39 deletions

View 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

View 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 ;