sequences.extras: adding ?first{2,3,4}.

db4
John Benediktsson 2013-09-04 20:39:32 -07:00
parent 5eb9d4532d
commit c9c30c6e6d
1 changed files with 26 additions and 2 deletions

View File

@ -1,5 +1,5 @@
USING: accessors arrays assocs fry grouping growable kernel USING: accessors arrays assocs combinators fry grouping growable
locals make math math.order math.ranges sequences kernel locals make math math.order math.ranges sequences
sequences.deep sequences.private sorting splitting vectors ; sequences.deep sequences.private sorting splitting vectors ;
FROM: sequences => change-nth ; FROM: sequences => change-nth ;
IN: sequences.extras IN: sequences.extras
@ -458,3 +458,27 @@ PRIVATE>
: remove-last! ( obj seq -- seq ) : remove-last! ( obj seq -- seq )
[ last-index ] keep over [ remove-nth! ] [ nip ] if ; [ last-index ] keep over [ remove-nth! ] [ nip ] if ;
: ?first2 ( seq -- first/f second/f )
dup length {
{ 0 [ drop f f ] }
{ 1 [ first-unsafe f ] }
[ drop first2-unsafe ]
} case ;
: ?first3 ( seq -- first/f second/f third/f )
dup length {
{ 0 [ drop f f f ] }
{ 1 [ first-unsafe f f ] }
{ 2 [ first2-unsafe f ] }
[ drop first3-unsafe ]
} case ;
: ?first4 ( seq -- first/f second/f third/f fourth/f )
dup length {
{ 0 [ drop f f f f ] }
{ 1 [ first-unsafe f f f ] }
{ 2 [ first2-unsafe f f ] }
{ 3 [ first3-unsafe f ] }
[ drop first4-unsafe ]
} case ;