From ec17b254e6a606c3983682d6e84cf5d47fcac54a Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 26 Jun 2016 18:25:04 -0700 Subject: [PATCH] sequences.extras: move some words here, add more words --- .../sequences/extras/extras-tests.factor | 30 ++++++++++++++++ collections/sequences/extras/extras.factor | 35 +++++++++++++++++++ .../parser/analyzer/analyzer-tests.factor | 25 ------------- libs/html/parser/analyzer/analyzer.factor | 30 ++-------------- 4 files changed, 67 insertions(+), 53 deletions(-) diff --git a/collections/sequences/extras/extras-tests.factor b/collections/sequences/extras/extras-tests.factor index f47828ff48..7708149a46 100644 --- a/collections/sequences/extras/extras-tests.factor +++ b/collections/sequences/extras/extras-tests.factor @@ -227,3 +227,33 @@ IN: sequences.extras.tests { } [ "test:" all-words [ name>> over prepend ] map-zip 2drop ] unit-test + + + +{ 0 3 } +[ 1 { 3 5 7 9 11 } [ odd? ] find-nth ] unit-test + +{ 2 7 } +[ 3 { 3 5 7 9 11 } [ odd? ] find-nth ] unit-test + +{ 3 9 } +[ 3 1 { 3 5 7 9 11 } [ odd? ] find-nth-from ] unit-test + +{ 4 11 } +[ 1 { 3 5 7 9 11 } [ odd? ] find-last-nth ] unit-test + +{ 2 7 } +[ 3 { 3 5 7 9 11 } [ odd? ] find-last-nth ] unit-test + +{ 0 3 } +[ 1 2 { 3 5 7 9 11 } [ odd? ] find-last-nth-from ] unit-test + + +[ 0 { 3 5 7 9 11 } [ odd? ] find-nth ] +[ undefined-find-nth? ] must-fail-with + +[ 0 { 3 5 7 9 11 } [ odd? ] find-last-nth ] +[ undefined-find-nth? ] must-fail-with + +{ { 1 1 2 1 } } +[ 3 { 1 1 2 1 } [ 2 = not ] head-nth ] unit-test \ No newline at end of file diff --git a/collections/sequences/extras/extras.factor b/collections/sequences/extras/extras.factor index cc9d33282d..baa23d6aa3 100644 --- a/collections/sequences/extras/extras.factor +++ b/collections/sequences/extras/extras.factor @@ -600,6 +600,41 @@ PRIVATE> : map-find-last-index ( ... seq quot: ( ... elt index -- ... result/f ) -- ... result i elt ) [ find-last-index ] (map-find-index) ; inline +: loopn-index ( n quot -- ) + [ iota ] [ $[ @ not ] ] bi* find 2drop ; inline + +: loopn ( n quot -- ) + [ drop ] prepose loopn-index ; inline + +ERROR: undefined-find-nth m n seq quot ; + +: check-trivial-find ( m n seq quot -- m n seq quot ) + pick 0 = [ undefined-find-nth ] when ; inline + +: find-nth-from ( m n seq quot -- i/f elt/f ) + check-trivial-find [ f ] 3dip $[ + drop _ _ find-from [ dup [ 1 + ] when ] dip over + ] loopn [ dup [ 1 - ] when ] dip ; inline + +: find-nth ( n seq quot -- i/f elt/f ) + [ 0 ] 3dip find-nth-from ; inline + +: find-last-nth-from ( m n seq quot -- i/f elt/f ) + check-trivial-find [ f ] 3dip $[ + drop _ _ find-last-from [ dup [ 1 - ] when ] dip over + ] loopn [ dup [ 1 + ] when ] dip ; inline + +: find-last-nth ( n seq quot -- i/f elt/f ) + [ [ nip length 1 - ] [ ] 2bi ] dip find-last-nth-from ; inline + +ERROR: head-nth-reached-end n seq quot ; +:: head-nth ( n seq quot -- seq' ) + n seq quot find-nth drop [ + [ seq ] dip 1 + head + ] [ + n seq quot head-nth-reached-end + ] if* ; inline + :: (start-all) ( subseq seq increment -- indices ) 0 [ [ subseq seq ] dip start* dup ] diff --git a/libs/html/parser/analyzer/analyzer-tests.factor b/libs/html/parser/analyzer/analyzer-tests.factor index 25016cd996..db65267aef 100644 --- a/libs/html/parser/analyzer/analyzer-tests.factor +++ b/libs/html/parser/analyzer/analyzer-tests.factor @@ -3,31 +3,6 @@ USING: html.parser html.parser.analyzer kernel math sequences tools.test ; IN: html.parser.analyzer.tests -{ 0 3 } -[ 1 { 3 5 7 9 11 } [ odd? ] find-nth ] unit-test - -{ 2 7 } -[ 3 { 3 5 7 9 11 } [ odd? ] find-nth ] unit-test - -{ 3 9 } -[ 3 1 { 3 5 7 9 11 } [ odd? ] find-nth-from ] unit-test - -{ 4 11 } -[ 1 { 3 5 7 9 11 } [ odd? ] find-last-nth ] unit-test - -{ 2 7 } -[ 3 { 3 5 7 9 11 } [ odd? ] find-last-nth ] unit-test - -{ 0 3 } -[ 1 2 { 3 5 7 9 11 } [ odd? ] find-last-nth-from ] unit-test - - -[ 0 { 3 5 7 9 11 } [ odd? ] find-nth ] -[ undefined-find-nth? ] must-fail-with - -[ 0 { 3 5 7 9 11 } [ odd? ] find-last-nth ] -[ undefined-find-nth? ] must-fail-with - { V{ T{ tag f text f "foo" f } } diff --git a/libs/html/parser/analyzer/analyzer.factor b/libs/html/parser/analyzer/analyzer.factor index 61ba324054..a86aa0332e 100644 --- a/libs/html/parser/analyzer/analyzer.factor +++ b/libs/html/parser/analyzer/analyzer.factor @@ -2,7 +2,8 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs combinators combinators.short-circuit fry html.parser http.client io kernel locals math math.statistics -sequences sets splitting unicode urls urls.encoding shuffle ; +sequences sequences.extras sets splitting unicode urls urls.encoding +shuffle ; IN: html.parser.analyzer : scrape-html ( url -- response vector ) @@ -20,39 +21,12 @@ IN: html.parser.analyzer : find-all ( seq quot -- alist ) [ >alist ] [ $[ second @ ] ] bi* filter ; inline -: loopn-index ( n quot -- ) - [ iota ] [ $[ @ not ] ] bi* find 2drop ; inline - -: loopn ( n quot -- ) - [ drop ] prepose loopn-index ; inline - : html-class? ( tag string -- ? ) swap "class" attribute [ blank? ] split-when member? ; : html-id? ( tag string -- ? ) swap "id" attribute = ; -ERROR: undefined-find-nth m n seq quot ; - -: check-trivial-find ( m n seq quot -- m n seq quot ) - pick 0 = [ undefined-find-nth ] when ; inline - -: find-nth-from ( m n seq quot -- i/f elt/f ) - check-trivial-find [ f ] 3dip $[ - drop _ _ find-from [ dup [ 1 + ] when ] dip over - ] loopn [ dup [ 1 - ] when ] dip ; inline - -: find-nth ( n seq quot -- i/f elt/f ) - [ 0 ] 3dip find-nth-from ; inline - -: find-last-nth-from ( m n seq quot -- i/f elt/f ) - check-trivial-find [ f ] 3dip $[ - drop _ _ find-last-from [ dup [ 1 - ] when ] dip over - ] loopn [ dup [ 1 + ] when ] dip ; inline - -: find-last-nth ( n seq quot -- i/f elt/f ) - [ [ nip length 1 - ] [ ] 2bi ] dip find-last-nth-from ; inline - : find-first-name ( vector string -- i/f tag/f ) >lower $[ name>> _ = ] find ; inline