:: longest-prefix ( ind seq -- start end )
ind dup ind + seq length min [a..b]
seq ind head-slice '[
- ind swap seq <slice> _ subseq-start
+ [ _ ] dip ind swap seq <slice> subsequence-starts
] map-find-last ;
:: create-pair ( ind seq -- array )
M: iokit-game-input-backend get-controllers
+controller-states+ get-global keys [ controller boa ] map ;
-: ?join ( pre post sep -- string )
- 2over subseq-start [ swap 2nip ] [ [ 2array ] dip join ] if ;
+: ?glue ( seq subseq sep -- string )
+ 2over subsequence-starts [ drop nip ] [ glue ] if ;
M: iokit-game-input-backend product-string
handle>>
- [ kIOHIDManufacturerKey device-property ]
- [ kIOHIDProductKey device-property ] bi " " ?join ;
+ [ kIOHIDProductKey device-property ]
+ [ kIOHIDManufacturerKey device-property ] bi " " ?glue ;
M: iokit-game-input-backend product-id
handle>>
ERROR: mime-decoding-ran-out-of-bytes ;
: dump-until-separator ( multipart -- multipart )
- [ ] [ current-separator>> ] [ bytes>> ] tri
- dup [ mime-decoding-ran-out-of-bytes ] unless
- 2dup subseq-start [
- cut-slice
+ [ ] [ bytes>> ] [ current-separator>> ] tri
+ over [ mime-decoding-ran-out-of-bytes ] unless
+ 2dup subsequence-starts [
+ swapd cut-slice
[ mime-write ]
[ swap length tail-slice >>bytes ] bi*
] [
+ swap
tuck [ length ] bi@ - 1 - cut-slice
[ mime-write ]
[ >>bytes ] bi* fill-bytes
{ [ dup "/" head? ] [ nip ] }
{ [ dup empty? ] [ drop ] }
{ [ over "/" tail? ] [ append ] }
- { [ "/" pick subseq-start not ] [ nip ] }
+ { [ over "/" subsequence-starts not ] [ nip ] }
[ [ "/" split1-last drop "/" ] dip 3append ]
} cond remove-dot-segments ;
{ f } [ "amigo" "hola" subseq-start ] unit-test
{ f } [ "holaa" "hola" subseq-start ] unit-test
+{ 3 } [ "hola" "a" subsequence-starts ] unit-test
+{ f } [ "hola" "x" subsequence-starts ] unit-test
+{ 0 } [ "a" "" subsequence-starts ] unit-test
+{ 0 } [ "" "" subsequence-starts ] unit-test
+{ 0 } [ "hola" "hola" subsequence-starts ] unit-test
+{ 1 } [ "hola" "ol" subsequence-starts ] unit-test
+{ f } [ "hola" "amigo" subsequence-starts ] unit-test
+{ f } [ "hola" "holaa" subsequence-starts ] unit-test
+
{ "Beginning" } [ "Beginning and end" 9 head ] unit-test
{ f } [ CHAR: I "team" member? ] unit-test
} cond*
] unit-test
+{ "hi " "there" } [
+ "hi there" {
+ { [ dup "there" subsequence-starts ] [ cut ] }
+ [ f ]
+ } cond*
+] unit-test
+
+{ "hi " "there" } [
+ "hi there" {
+ { [ dup "foo" subsequence-starts ] [ head f ] }
+ { [ dup "there" subsequence-starts ] [ cut ] }
+ [ f ]
+ } cond*
+] unit-test
+
+{ "hi there" f } [
+ "hi there" {
+ { [ dup "foo" subsequence-starts ] [ head f ] }
+ { [ dup "bar" subsequence-starts ] [ cut ] }
+ [ f ]
+ } cond*
+] unit-test
+
{ f } [ f { } chain ] unit-test
{ 3 } [ H{ { 1 H{ { 2 3 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
{ f } [ H{ { 1 H{ { 3 4 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
: prolog-encoding ( string -- iana-encoding )
'[
- _ "encoding=" over subseq-start
+ _ dup "encoding=" subsequence-starts
10 + swap [ [ 1 - ] dip nth ] [ index-from ] [ swapd subseq ] 2tri
] [ drop "UTF-8" ] recover ;
dup is-prime? [ increment-counts ] [ drop ] if
] each sieve get ;
-: consecutive-under ( m limit -- n/f )
- prime-tau-upto [ dup <repetition> ] dip subseq-start ;
+: consecutive-under ( limit m -- n/f )
+ [ prime-tau-upto ] [ dup <repetition> ] bi* subsequence-starts ;
PRIVATE>
: euler047a ( -- answer )
- 4 200000 consecutive-under ;
+ 200000 4 consecutive-under ;
! [ euler047a ] 100 ave-time
! 331 ms ave run time - 19.14 SD (100 trials)