nip most uses of tuck from extra
parent
dbadab67ef
commit
a5957b188d
|
@ -1,4 +1,4 @@
|
||||||
USING: kernel io io.files splitting strings io.encodings.ascii
|
USING: kernel locals io io.files splitting strings io.encodings.ascii
|
||||||
hashtables sequences assocs math namespaces prettyprint
|
hashtables sequences assocs math namespaces prettyprint
|
||||||
math.parser combinators arrays sorting unicode.case ;
|
math.parser combinators arrays sorting unicode.case ;
|
||||||
|
|
||||||
|
@ -21,10 +21,7 @@ IN: benchmark.knucleotide
|
||||||
CHAR: \n swap remove >upper ;
|
CHAR: \n swap remove >upper ;
|
||||||
|
|
||||||
: tally ( x exemplar -- b )
|
: tally ( x exemplar -- b )
|
||||||
clone tuck
|
clone [ [ inc-at ] curry each ] keep ;
|
||||||
[
|
|
||||||
[ [ 1 + ] [ 1 ] if* ] change-at
|
|
||||||
] curry each ;
|
|
||||||
|
|
||||||
: small-groups ( x n -- b )
|
: small-groups ( x n -- b )
|
||||||
swap
|
swap
|
||||||
|
@ -42,10 +39,10 @@ IN: benchmark.knucleotide
|
||||||
] each
|
] each
|
||||||
drop ;
|
drop ;
|
||||||
|
|
||||||
: handle-n ( inputs x -- )
|
:: handle-n ( inputs x -- )
|
||||||
tuck length
|
inputs x length small-groups :> groups
|
||||||
small-groups H{ } tally
|
groups H{ } tally :> b
|
||||||
at [ 0 ] unless*
|
x b at [ 0 ] unless*
|
||||||
number>string 8 CHAR: \s pad-tail write ;
|
number>string 8 CHAR: \s pad-tail write ;
|
||||||
|
|
||||||
: process-input ( input -- )
|
: process-input ( input -- )
|
||||||
|
|
|
@ -123,8 +123,10 @@ PRIVATE>
|
||||||
: curses-writef ( window string -- )
|
: curses-writef ( window string -- )
|
||||||
[ window-ptr dup ] dip (curses-wprint) (curses-window-refresh) ;
|
[ window-ptr dup ] dip (curses-wprint) (curses-window-refresh) ;
|
||||||
|
|
||||||
: (curses-read) ( window-ptr n encoding -- string )
|
:: (curses-read) ( window-ptr n encoding -- string )
|
||||||
[ [ <byte-array> tuck ] keep wgetnstr curses-error ] dip alien>string ;
|
n <byte-array> :> buf
|
||||||
|
window-ptr buf n wgetnstr curses-error
|
||||||
|
buf encoding alien>string ;
|
||||||
|
|
||||||
: curses-read ( window n -- string )
|
: curses-read ( window n -- string )
|
||||||
utf8 [ window-ptr ] 2dip (curses-read) ;
|
utf8 [ window-ptr ] 2dip (curses-read) ;
|
||||||
|
|
|
@ -37,7 +37,7 @@ SYNTAX: D: parse-decimal suffix! ;
|
||||||
] 2bi ;
|
] 2bi ;
|
||||||
|
|
||||||
: scale-decimals ( D1 D2 -- D1' D2' )
|
: scale-decimals ( D1 D2 -- D1' D2' )
|
||||||
scale-mantissas tuck [ <decimal> ] 2dip <decimal> ;
|
scale-mantissas [ <decimal> ] curry bi@ ;
|
||||||
|
|
||||||
ERROR: decimal-types-expected d1 d2 ;
|
ERROR: decimal-types-expected d1 d2 ;
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ PRIVATE>
|
||||||
|
|
||||||
: get-private-key ( -- bin/f )
|
: get-private-key ( -- bin/f )
|
||||||
ec-key-handle EC_KEY_get0_private_key
|
ec-key-handle EC_KEY_get0_private_key
|
||||||
dup [ dup BN_num_bits bits>bytes <byte-array> tuck BN_bn2bin drop ] when ;
|
dup [ dup BN_num_bits bits>bytes <byte-array> [ BN_bn2bin drop ] keep ] when ;
|
||||||
|
|
||||||
:: get-public-key ( -- bin/f )
|
:: get-public-key ( -- bin/f )
|
||||||
ec-key-handle :> KEY
|
ec-key-handle :> KEY
|
||||||
|
|
|
@ -11,8 +11,7 @@ IN: io.serial.windows
|
||||||
|
|
||||||
: get-comm-state ( duplex -- dcb )
|
: get-comm-state ( duplex -- dcb )
|
||||||
in>> handle>>
|
in>> handle>>
|
||||||
DCB <struct> tuck
|
DCB <struct> [ GetCommState win32-error=0/f ] keep ;
|
||||||
GetCommState win32-error=0/f ;
|
|
||||||
|
|
||||||
: set-comm-state ( duplex dcb -- )
|
: set-comm-state ( duplex dcb -- )
|
||||||
[ in>> handle>> ] dip
|
[ in>> handle>> ] dip
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
USING: accessors alien.c-types jamshred.game jamshred.oint
|
USING: accessors alien.c-types jamshred.game jamshred.oint
|
||||||
jamshred.player jamshred.tunnel kernel math math.constants
|
jamshred.player jamshred.tunnel kernel math math.constants
|
||||||
math.functions math.vectors opengl opengl.gl opengl.glu
|
math.functions math.vectors opengl opengl.gl opengl.glu
|
||||||
opengl.demo-support sequences specialized-arrays ;
|
opengl.demo-support sequences specialized-arrays locals ;
|
||||||
FROM: alien.c-types => float ;
|
FROM: alien.c-types => float ;
|
||||||
SPECIALIZED-ARRAY: float
|
SPECIALIZED-ARRAY: float
|
||||||
IN: jamshred.gl
|
IN: jamshred.gl
|
||||||
|
@ -50,8 +50,9 @@ CONSTANT: wall-drawing-offset 0.15
|
||||||
over color>> gl-color segment-vertex-and-normal
|
over color>> gl-color segment-vertex-and-normal
|
||||||
gl-normal gl-vertex ;
|
gl-normal gl-vertex ;
|
||||||
|
|
||||||
: draw-vertex-pair ( theta next-segment segment -- )
|
:: draw-vertex-pair ( theta next-segment segment -- )
|
||||||
rot tuck draw-segment-vertex draw-segment-vertex ;
|
segment theta draw-segment-vertex
|
||||||
|
next-segment theta draw-segment-vertex ;
|
||||||
|
|
||||||
: draw-segment ( next-segment segment -- )
|
: draw-segment ( next-segment segment -- )
|
||||||
GL_QUAD_STRIP [
|
GL_QUAD_STRIP [
|
||||||
|
|
|
@ -53,13 +53,13 @@ C: <oint> oint
|
||||||
|
|
||||||
: scalar-projection ( v1 v2 -- n )
|
: scalar-projection ( v1 v2 -- n )
|
||||||
#! the scalar projection of v1 onto v2
|
#! the scalar projection of v1 onto v2
|
||||||
tuck v. swap norm / ;
|
[ v. ] [ norm ] bi / ;
|
||||||
|
|
||||||
: proj-perp ( u v -- w )
|
: proj-perp ( u v -- w )
|
||||||
dupd proj v- ;
|
dupd proj v- ;
|
||||||
|
|
||||||
: perpendicular-distance ( oint oint -- distance )
|
: perpendicular-distance ( oint oint -- distance )
|
||||||
tuck distance-vector swap 2dup left>> scalar-projection abs
|
[ distance-vector ] keep 2dup left>> scalar-projection abs
|
||||||
-rot up>> scalar-projection abs + ;
|
-rot up>> scalar-projection abs + ;
|
||||||
|
|
||||||
:: reflect ( v n -- v' )
|
:: reflect ( v n -- v' )
|
||||||
|
|
|
@ -31,8 +31,9 @@ CONSTANT: max-speed 30.0
|
||||||
forward-pivot ;
|
forward-pivot ;
|
||||||
|
|
||||||
: to-tunnel-start ( player -- )
|
: to-tunnel-start ( player -- )
|
||||||
[ tunnel>> first dup location>> ]
|
dup tunnel>> first
|
||||||
[ tuck (>>location) (>>nearest-segment) ] bi ;
|
[ >>nearest-segment ]
|
||||||
|
[ location>> >>location ] bi drop ;
|
||||||
|
|
||||||
: play-in-tunnel ( player segments -- )
|
: play-in-tunnel ( player segments -- )
|
||||||
>>tunnel to-tunnel-start ;
|
>>tunnel to-tunnel-start ;
|
||||||
|
|
|
@ -63,9 +63,10 @@ CONSTANT: default-segment-radius 1
|
||||||
#! valid values
|
#! valid values
|
||||||
[ '[ _ clamp-length ] bi@ ] keep <slice> ;
|
[ '[ _ clamp-length ] bi@ ] keep <slice> ;
|
||||||
|
|
||||||
: nearer-segment ( segment segment oint -- segment )
|
:: nearer-segment ( seg-a seg-b oint -- segment )
|
||||||
#! return whichever of the two segments is nearer to the oint
|
seg-a oint distance
|
||||||
[ 2dup ] dip tuck distance [ distance ] dip < -rot ? ;
|
seg-b oint distance <
|
||||||
|
seg-a seg-b ? ;
|
||||||
|
|
||||||
: (find-nearest-segment) ( nearest next oint -- nearest ? )
|
: (find-nearest-segment) ( nearest next oint -- nearest ? )
|
||||||
#! find the nearest of 'next' and 'nearest' to 'oint', and return
|
#! find the nearest of 'next' and 'nearest' to 'oint', and return
|
||||||
|
|
|
@ -50,10 +50,10 @@ CONSTANT: pov-polygons
|
||||||
[ [ 0.0 ] unless* ] tri@
|
[ [ 0.0 ] unless* ] tri@
|
||||||
[ (xy>loc) ] dip (z>loc) ;
|
[ (xy>loc) ] dip (z>loc) ;
|
||||||
|
|
||||||
: move-axis ( gadget x y z -- )
|
:: move-axis ( gadget x y z -- )
|
||||||
(xyz>loc) rot tuck
|
x y z (xyz>loc) :> ( xy z )
|
||||||
[ indicator>> (>>loc) ]
|
xy gadget indicator>> (>>loc)
|
||||||
[ z-indicator>> (>>loc) ] 2bi* ;
|
z gadget z-indicator>> (>>loc) ;
|
||||||
|
|
||||||
: move-pov ( gadget pov -- )
|
: move-pov ( gadget pov -- )
|
||||||
swap pov>> [ interior>> -rot = COLOR: gray COLOR: white ? >>color drop ]
|
swap pov>> [ interior>> -rot = COLOR: gray COLOR: white ? >>color drop ]
|
||||||
|
@ -82,10 +82,10 @@ TUPLE: joystick-demo-gadget < pack axis raxis controller buttons alarm ;
|
||||||
[ >>controller ] [ product-string <label> add-gadget ] bi ;
|
[ >>controller ] [ product-string <label> add-gadget ] bi ;
|
||||||
|
|
||||||
: add-axis-gadget ( gadget shelf -- gadget shelf )
|
: add-axis-gadget ( gadget shelf -- gadget shelf )
|
||||||
<axis-gadget> tuck [ >>axis ] [ add-gadget-with-border ] 2bi* ;
|
<axis-gadget> [ >>axis ] [ add-gadget-with-border ] bi-curry bi* ;
|
||||||
|
|
||||||
: add-raxis-gadget ( gadget shelf -- gadget shelf )
|
: add-raxis-gadget ( gadget shelf -- gadget shelf )
|
||||||
<axis-gadget> tuck [ >>raxis ] [ add-gadget-with-border ] 2bi* ;
|
<axis-gadget> [ >>raxis ] [ add-gadget-with-border ] bi-curry bi* ;
|
||||||
|
|
||||||
:: (add-button-gadgets) ( gadget shelf -- )
|
:: (add-button-gadgets) ( gadget shelf -- )
|
||||||
gadget controller>> read-controller buttons>> length [
|
gadget controller>> read-controller buttons>> length [
|
||||||
|
|
|
@ -7,4 +7,4 @@ TUPLE: key-handler < border handlers ;
|
||||||
: <keys> ( gadget -- key-handler ) key-handler new-border { 0 0 } >>size ;
|
: <keys> ( gadget -- key-handler ) key-handler new-border { 0 0 } >>size ;
|
||||||
|
|
||||||
M: key-handler handle-gesture
|
M: key-handler handle-gesture
|
||||||
tuck handlers>> at [ call( gadget -- ) f ] [ drop t ] if* ;
|
[ handlers>> at ] keep swap [ call( gadget -- ) f ] [ drop t ] if* ;
|
||||||
|
|
|
@ -229,14 +229,12 @@ DEFER: (d)
|
||||||
: laplacian-betti ( basis1 basis2 basis3 -- n )
|
: laplacian-betti ( basis1 basis2 basis3 -- n )
|
||||||
laplacian-matrix null/rank drop ;
|
laplacian-matrix null/rank drop ;
|
||||||
|
|
||||||
: laplacian-kernel ( basis1 basis2 basis3 -- basis )
|
:: laplacian-kernel ( basis1 basis2 basis3 -- basis )
|
||||||
[ tuck ] dip
|
basis1 basis2 basis3 laplacian-matrix :> lap
|
||||||
laplacian-matrix dup empty-matrix? [
|
lap empty-matrix? [ f ] [
|
||||||
2drop f
|
lap nullspace [| x |
|
||||||
] [
|
basis2 x [ [ wedge (alt+) ] 2each ] with-terms
|
||||||
nullspace [
|
] map
|
||||||
[ [ wedge (alt+) ] 2each ] with-terms
|
|
||||||
] with map
|
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
: graded-triple ( seq n -- triple )
|
: graded-triple ( seq n -- triple )
|
||||||
|
|
|
@ -41,7 +41,7 @@ CONSTANT: identity-transform T{ affine-transform f { 1.0 0.0 } { 0.0 1.0 } { 0.0
|
||||||
[ [ y>> second ] [ x>> second neg ] bi 2array ]
|
[ [ y>> second ] [ x>> second neg ] bi 2array ]
|
||||||
[ [ y>> first neg ] [ x>> first ] bi 2array ]
|
[ [ y>> first neg ] [ x>> first ] bi 2array ]
|
||||||
[ |a| ] tri
|
[ |a| ] tri
|
||||||
tuck [ v/n ] 2bi@ ;
|
[ v/n ] curry bi@ ;
|
||||||
|
|
||||||
: inverse-axes ( a -- a^-1 )
|
: inverse-axes ( a -- a^-1 )
|
||||||
(inverted-axes) { 0.0 0.0 } <affine-transform> ;
|
(inverted-axes) { 0.0 0.0 } <affine-transform> ;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2008 John Benediktsson
|
! Copyright (C) 2008 John Benediktsson
|
||||||
! See http://factorcode.org/license.txt for BSD license
|
! See http://factorcode.org/license.txt for BSD license
|
||||||
|
|
||||||
USING: sequences kernel arrays vectors accessors assocs sorting math math.functions ;
|
USING: sequences kernel arrays vectors accessors assocs shuffle sorting locals math math.functions ;
|
||||||
|
|
||||||
IN: math.binpack
|
IN: math.binpack
|
||||||
|
|
||||||
|
@ -9,10 +9,12 @@ IN: math.binpack
|
||||||
[ [ values sum ] map ] keep
|
[ [ values sum ] map ] keep
|
||||||
zip sort-keys values first push ;
|
zip sort-keys values first push ;
|
||||||
|
|
||||||
: binpack ( assoc n -- bins )
|
:: binpack ( assoc n -- bins )
|
||||||
[ sort-values <reversed> dup length ] dip
|
assoc sort-values <reversed> :> values
|
||||||
tuck / ceiling <array> [ <vector> ] map
|
values length :> #values
|
||||||
tuck [ (binpack) ] curry each ;
|
n #values n / ceiling <array> [ <vector> ] map :> bins
|
||||||
|
values [ bins (binpack) ] each
|
||||||
|
bins ;
|
||||||
|
|
||||||
: binpack* ( items n -- bins )
|
: binpack* ( items n -- bins )
|
||||||
[ dup zip ] dip binpack [ keys ] map ;
|
[ dup zip ] dip binpack [ keys ] map ;
|
||||||
|
|
|
@ -7,7 +7,7 @@ IN: math.finance
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: weighted ( x y a -- z )
|
: weighted ( x y a -- z )
|
||||||
tuck [ * ] [ 1 - neg * ] 2bi* + ;
|
[ * ] [ 1 - neg * ] bi-curry bi* + ;
|
||||||
|
|
||||||
: a ( n -- a )
|
: a ( n -- a )
|
||||||
1 + 2 swap / ;
|
1 + 2 swap / ;
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
USING: kernel locals math math.functions ;
|
USING: kernel locals math math.functions ;
|
||||||
IN: math.quadratic
|
IN: math.quadratic
|
||||||
|
|
||||||
: monic ( c b a -- c' b' ) tuck [ / ] 2bi@ ;
|
: monic ( c b a -- c' b' ) [ / ] curry bi@ ;
|
||||||
|
|
||||||
: discriminant ( c b -- b d ) tuck sq 4 / swap - sqrt ;
|
: discriminant ( c b -- b d ) [ nip ] [ sq 4 / swap - sqrt ] 2bi ;
|
||||||
|
|
||||||
: critical ( b d -- -b/2 d ) [ -2 / ] dip ;
|
: critical ( b d -- -b/2 d ) [ -2 / ] dip ;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
USING: accessors arrays kernel models models.product monads
|
USING: accessors arrays kernel models models.product monads
|
||||||
sequences sequences.extras ;
|
sequences sequences.extras shuffle ;
|
||||||
FROM: syntax => >> ;
|
FROM: syntax => >> ;
|
||||||
IN: models.combinators
|
IN: models.combinators
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ M: sequence <mdb-insert-msg> ( collection sequence -- mdb-insert-msg )
|
||||||
M: assoc <mdb-insert-msg> ( collection assoc -- mdb-insert-msg )
|
M: assoc <mdb-insert-msg> ( collection assoc -- mdb-insert-msg )
|
||||||
[ mdb-insert-msg new ] 2dip
|
[ mdb-insert-msg new ] 2dip
|
||||||
[ >>collection ] dip
|
[ >>collection ] dip
|
||||||
V{ } clone tuck push
|
[ V{ } clone ] dip suffix!
|
||||||
>>objects OP_Insert >>opcode ;
|
>>objects OP_Insert >>opcode ;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ CONSTANT: MDB_TUPLE_INFO "_mfd_t_info"
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: <tuple-info> ( tuple -- tuple-info )
|
: <tuple-info> ( tuple -- tuple-info )
|
||||||
class V{ } clone tuck
|
class [ V{ } clone ] dip over
|
||||||
[ [ name>> ] dip push ]
|
[ [ name>> ] dip push ]
|
||||||
[ [ vocabulary>> ] dip push ] 2bi ; inline
|
[ [ vocabulary>> ] dip push ] 2bi ; inline
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2004 Chris Double.
|
! Copyright (C) 2004 Chris Double.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: lists lists.lazy promises kernel sequences strings math
|
USING: lists lists.lazy promises kernel sequences strings math
|
||||||
arrays splitting quotations combinators namespaces
|
arrays splitting quotations combinators namespaces locals
|
||||||
unicode.case unicode.categories sequences.deep accessors ;
|
unicode.case unicode.categories sequences.deep accessors ;
|
||||||
IN: parser-combinators
|
IN: parser-combinators
|
||||||
|
|
||||||
|
@ -58,9 +58,11 @@ C: <token-parser> token-parser
|
||||||
|
|
||||||
: case-insensitive-token ( string -- parser ) t <token-parser> ;
|
: case-insensitive-token ( string -- parser ) t <token-parser> ;
|
||||||
|
|
||||||
M: token-parser parse ( input parser -- list )
|
M:: token-parser parse ( input parser -- list )
|
||||||
[ string>> ] [ ignore-case?>> ] bi
|
parser string>> :> str
|
||||||
[ tuck ] dip ?string-head
|
parser ignore-case?>> :> case?
|
||||||
|
|
||||||
|
str input str case? ?string-head
|
||||||
[ <parse-results> ] [ 2drop nil ] if ;
|
[ <parse-results> ] [ 2drop nil ] if ;
|
||||||
|
|
||||||
: 1token ( n -- parser ) 1string token ;
|
: 1token ( n -- parser ) 1string token ;
|
||||||
|
@ -319,7 +321,7 @@ LAZY: <(+)> ( parser -- parser )
|
||||||
<& &> ;
|
<& &> ;
|
||||||
|
|
||||||
: nonempty-list-of ( items separator -- parser )
|
: nonempty-list-of ( items separator -- parser )
|
||||||
[ over &> <*> <&:> ] keep <?> tuck pack ;
|
[ over &> <*> <&:> ] keep <?> [ nip ] 2keep pack ;
|
||||||
|
|
||||||
: list-of ( items separator -- parser )
|
: list-of ( items separator -- parser )
|
||||||
#! Given a parser for the separator and for the
|
#! Given a parser for the separator and for the
|
||||||
|
|
|
@ -31,7 +31,7 @@ PRIVATE>
|
||||||
V{ 0 } clone 1 rot (fib-upto) ;
|
V{ 0 } clone 1 rot (fib-upto) ;
|
||||||
|
|
||||||
: euler002 ( -- answer )
|
: euler002 ( -- answer )
|
||||||
4000000 fib-upto [ even? ] filter sum ;
|
4,000,000 fib-upto [ even? ] filter sum ;
|
||||||
|
|
||||||
! [ euler002 ] 100 ave-time
|
! [ euler002 ] 100 ave-time
|
||||||
! 0 ms ave run time - 0.22 SD (100 trials)
|
! 0 ms ave run time - 0.22 SD (100 trials)
|
||||||
|
@ -41,11 +41,11 @@ PRIVATE>
|
||||||
! -------------------
|
! -------------------
|
||||||
|
|
||||||
: fib-upto* ( n -- seq )
|
: fib-upto* ( n -- seq )
|
||||||
0 1 [ pick over >= ] [ tuck + dup ] produce [ 3drop ] dip
|
0 1 [ pick over >= ] [ [ nip ] 2keep + dup ] produce [ 3drop ] dip
|
||||||
but-last-slice { 0 1 } prepend ;
|
but-last-slice { 0 1 } prepend ;
|
||||||
|
|
||||||
: euler002a ( -- answer )
|
: euler002a ( -- answer )
|
||||||
4000000 fib-upto* [ even? ] filter sum ;
|
4,000,000 fib-upto* [ even? ] filter sum ;
|
||||||
|
|
||||||
! [ euler002a ] 100 ave-time
|
! [ euler002a ] 100 ave-time
|
||||||
! 0 ms ave run time - 0.2 SD (100 trials)
|
! 0 ms ave run time - 0.2 SD (100 trials)
|
||||||
|
@ -54,7 +54,7 @@ PRIVATE>
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: next-fibs ( x y -- y x+y )
|
: next-fibs ( x y -- y x+y )
|
||||||
tuck + ;
|
[ nip ] [ + ] 2bi ;
|
||||||
|
|
||||||
: ?retotal ( total fib- fib+ -- retotal fib- fib+ )
|
: ?retotal ( total fib- fib+ -- retotal fib- fib+ )
|
||||||
dup even? [ [ nip + ] 2keep ] when ;
|
dup even? [ [ nip + ] 2keep ] when ;
|
||||||
|
|
|
@ -5,8 +5,7 @@ IN: project-euler.100
|
||||||
|
|
||||||
! http://projecteuler.net/index.php?section=problems&id=100
|
! http://projecteuler.net/index.php?section=problems&id=100
|
||||||
|
|
||||||
! DESCRIPTION
|
! DESCRIPTION ! -----------
|
||||||
! -----------
|
|
||||||
|
|
||||||
! If a box contains twenty-one coloured discs, composed of fifteen blue discs
|
! If a box contains twenty-one coloured discs, composed of fifteen blue discs
|
||||||
! and six red discs, and two discs were taken at random, it can be seen that
|
! and six red discs, and two discs were taken at random, it can be seen that
|
||||||
|
@ -26,7 +25,7 @@ IN: project-euler.100
|
||||||
: euler100 ( -- answer )
|
: euler100 ( -- answer )
|
||||||
1 1
|
1 1
|
||||||
[ dup dup 1 - * 2 * 10 24 ^ <= ]
|
[ dup dup 1 - * 2 * 10 24 ^ <= ]
|
||||||
[ tuck 6 * swap - 2 - ] while nip ;
|
[ [ 6 * swap - 2 - ] keep swap ] while nip ;
|
||||||
|
|
||||||
! TODO: solution needs generalization
|
! TODO: solution needs generalization
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ IN: project-euler.117
|
||||||
[ 4 short tail* sum ] keep push ;
|
[ 4 short tail* sum ] keep push ;
|
||||||
|
|
||||||
: (euler117) ( n -- m )
|
: (euler117) ( n -- m )
|
||||||
V{ 1 } clone tuck [ next ] curry times last ;
|
[ V{ 1 } clone ] dip over [ next ] curry times last ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ IN: project-euler.ave-time
|
||||||
[
|
[
|
||||||
[ datastack ]
|
[ datastack ]
|
||||||
[
|
[
|
||||||
'[ _ gc benchmark 1000 / , ] tuck
|
'[ _ gc benchmark 1000 / , ]
|
||||||
'[ _ _ with-datastack drop ]
|
[ '[ _ _ with-datastack drop ] ] keep swap
|
||||||
]
|
]
|
||||||
[ 1 - ] tri* swap times call
|
[ 1 - ] tri* swap times call
|
||||||
] { } make ; inline
|
] { } make ; inline
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
! (c) 2009 Joe Groff, see BSD license
|
! (c) 2009 Joe Groff, see BSD license
|
||||||
USING: assocs kernel math.rectangles combinators accessors
|
USING: assocs kernel math.rectangles combinators accessors locals
|
||||||
math.vectors vectors sequences math combinators.short-circuit arrays fry ;
|
math.vectors vectors sequences math combinators.short-circuit arrays fry ;
|
||||||
IN: quadtrees
|
IN: quadtrees
|
||||||
|
|
||||||
|
@ -89,8 +89,9 @@ DEFER: in-rect*
|
||||||
: insert ( value point tree -- )
|
: insert ( value point tree -- )
|
||||||
dup leaf?>> [ leaf-insert ] [ node-insert ] if ;
|
dup leaf?>> [ leaf-insert ] [ node-insert ] if ;
|
||||||
|
|
||||||
: leaf-at-point ( point leaf -- value/f ? )
|
:: leaf-at-point ( point leaf -- value/f ? )
|
||||||
tuck point>> = [ value>> t ] [ drop f f ] if ;
|
point leaf point>> =
|
||||||
|
[ leaf value>> t ] [ f f ] if ;
|
||||||
|
|
||||||
: node-at-point ( point node -- value/f ? )
|
: node-at-point ( point node -- value/f ? )
|
||||||
descend at-point ;
|
descend at-point ;
|
||||||
|
@ -103,15 +104,15 @@ DEFER: in-rect*
|
||||||
: node-in-rect* ( values rect node -- values )
|
: node-in-rect* ( values rect node -- values )
|
||||||
[ (node-in-rect*) ] with each-quadrant ;
|
[ (node-in-rect*) ] with each-quadrant ;
|
||||||
|
|
||||||
: leaf-in-rect* ( values rect leaf -- values )
|
:: leaf-in-rect* ( values rect leaf -- values )
|
||||||
tuck { [ nip point>> ] [ point>> swap contains-point? ] } 2&&
|
{ [ leaf point>> ] [ leaf point>> rect contains-point? ] } 0&&
|
||||||
[ value>> over push ] [ drop ] if ;
|
[ values leaf value>> suffix! ] [ values ] if ;
|
||||||
|
|
||||||
: in-rect* ( values rect tree -- values )
|
: in-rect* ( values rect tree -- values )
|
||||||
dup leaf?>> [ leaf-in-rect* ] [ node-in-rect* ] if ;
|
dup leaf?>> [ leaf-in-rect* ] [ node-in-rect* ] if ;
|
||||||
|
|
||||||
: leaf-erase ( point leaf -- )
|
:: leaf-erase ( point leaf -- )
|
||||||
tuck point>> = [ f >>point f >>value ] when drop ;
|
point leaf point>> = [ leaf f >>point f >>value drop ] when ;
|
||||||
|
|
||||||
: node-erase ( point node -- )
|
: node-erase ( point node -- )
|
||||||
descend erase ;
|
descend erase ;
|
||||||
|
|
|
@ -22,7 +22,7 @@ IN: blum-blum-shub.tests
|
||||||
|
|
||||||
[ 3716213681 ]
|
[ 3716213681 ]
|
||||||
[
|
[
|
||||||
100 T{ blum-blum-shub f 200352954495 846054538649 } clone tuck [
|
T{ blum-blum-shub f 200352954495 846054538649 } clone 100 over [
|
||||||
random-32* drop
|
random-32* drop
|
||||||
] curry times
|
] curry times
|
||||||
random-32*
|
random-32*
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
USING: kernel math sequences strings io combinators ascii ;
|
USING: kernel math sequences strings io combinators ascii ;
|
||||||
IN: rot13
|
IN: rot13
|
||||||
|
|
||||||
: rotate ( ch base -- ch ) tuck - 13 + 26 mod + ;
|
: rotate ( ch base -- ch ) [ - 13 + 26 mod ] [ + ] bi ;
|
||||||
|
|
||||||
: rot-letter ( ch -- ch )
|
: rot-letter ( ch -- ch )
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,7 +12,7 @@ IN: sequences.abbrev
|
||||||
[ prefixes ] keep 1array '[ _ ] H{ } map>assoc ;
|
[ prefixes ] keep 1array '[ _ ] H{ } map>assoc ;
|
||||||
|
|
||||||
: assoc-merge ( assoc1 assoc2 -- assoc3 )
|
: assoc-merge ( assoc1 assoc2 -- assoc3 )
|
||||||
tuck '[ over _ at dup [ append ] [ drop ] if ] assoc-map assoc-union ;
|
[ '[ over _ at dup [ append ] [ drop ] if ] assoc-map ] keep swap assoc-union ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
! Copyright (C) 2008 Alex Chapman
|
! Copyright (C) 2008 Alex Chapman
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors arrays kernel math math.order
|
USING: accessors arrays kernel locals math math.order
|
||||||
sequences sequences.private shuffle ;
|
sequences sequences.private shuffle ;
|
||||||
IN: sequences.modified
|
IN: sequences.modified
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@ C: <scaled> scaled
|
||||||
M: scaled modified-nth ( n seq -- elt )
|
M: scaled modified-nth ( n seq -- elt )
|
||||||
[ seq>> nth ] [ c>> * ] bi ;
|
[ seq>> nth ] [ c>> * ] bi ;
|
||||||
|
|
||||||
M: scaled modified-set-nth ( elt n seq -- elt )
|
M:: scaled modified-set-nth ( elt n seq -- elt )
|
||||||
! don't set c to 0!
|
! don't set c to 0!
|
||||||
tuck [ c>> / ] 2dip seq>> set-nth ;
|
elt seq c>> / n seq seq>> set-nth ;
|
||||||
|
|
||||||
TUPLE: offset < 1modified n ;
|
TUPLE: offset < 1modified n ;
|
||||||
C: <offset> offset
|
C: <offset> offset
|
||||||
|
@ -45,8 +45,8 @@ C: <offset> offset
|
||||||
M: offset modified-nth ( n seq -- elt )
|
M: offset modified-nth ( n seq -- elt )
|
||||||
[ seq>> nth ] [ n>> + ] bi ;
|
[ seq>> nth ] [ n>> + ] bi ;
|
||||||
|
|
||||||
M: offset modified-set-nth ( elt n seq -- )
|
M:: offset modified-set-nth ( elt n seq -- )
|
||||||
tuck [ n>> - ] 2dip seq>> set-nth ;
|
elt seq n>> - n seq seq>> set-nth ;
|
||||||
|
|
||||||
TUPLE: summed < modified seqs ;
|
TUPLE: summed < modified seqs ;
|
||||||
C: <summed> summed
|
C: <summed> summed
|
||||||
|
|
|
@ -14,7 +14,9 @@ USING:
|
||||||
io.files
|
io.files
|
||||||
io.pathnames
|
io.pathnames
|
||||||
kernel
|
kernel
|
||||||
|
locals
|
||||||
math
|
math
|
||||||
|
math.order
|
||||||
openal
|
openal
|
||||||
opengl.gl
|
opengl.gl
|
||||||
sequences
|
sequences
|
||||||
|
@ -41,9 +43,7 @@ CONSTANT: game-height 256
|
||||||
first2 game-width 3 * * swap 3 * + ;
|
first2 game-width 3 * * swap 3 * + ;
|
||||||
|
|
||||||
:: set-bitmap-pixel ( bitmap point color -- )
|
:: set-bitmap-pixel ( bitmap point color -- )
|
||||||
color point bitmap
|
point bitmap-index :> index
|
||||||
|
|
||||||
point color :> index
|
|
||||||
color first index bitmap set-nth
|
color first index bitmap set-nth
|
||||||
color second index 1 + bitmap set-nth
|
color second index 1 + bitmap set-nth
|
||||||
color third index 2 + bitmap set-nth ;
|
color third index 2 + bitmap set-nth ;
|
||||||
|
@ -140,8 +140,8 @@ M: space-invaders read-port ( port cpu -- byte )
|
||||||
#! Setting this value affects the value read from port 3
|
#! Setting this value affects the value read from port 3
|
||||||
(>>port2o) ;
|
(>>port2o) ;
|
||||||
|
|
||||||
: bit-newly-set? ( old-value new-value bit -- bool )
|
:: bit-newly-set? ( old-value new-value bit -- bool )
|
||||||
tuck bit? [ bit? not ] dip and ;
|
new-value bit bit? [ old-value bit bit? not ] dip and ;
|
||||||
|
|
||||||
: port3-newly-set? ( new-value cpu bit -- bool )
|
: port3-newly-set? ( new-value cpu bit -- bool )
|
||||||
[ port3o>> swap ] dip bit-newly-set? ;
|
[ port3o>> swap ] dip bit-newly-set? ;
|
||||||
|
@ -320,17 +320,13 @@ CONSTANT: red { 255 0 0 }
|
||||||
#! point is a {x y}. color is a {r g b}.
|
#! point is a {x y}. color is a {r g b}.
|
||||||
set-bitmap-pixel ;
|
set-bitmap-pixel ;
|
||||||
|
|
||||||
: within ( n a b -- bool )
|
|
||||||
#! n >= a and n <= b
|
|
||||||
rot tuck swap <= [ swap >= ] dip and ;
|
|
||||||
|
|
||||||
: get-point-color ( point -- color )
|
: get-point-color ( point -- color )
|
||||||
#! Return the color to use for the given x/y position.
|
#! Return the color to use for the given x/y position.
|
||||||
first2
|
first2
|
||||||
{
|
{
|
||||||
{ [ dup 184 238 within pick 0 223 within and ] [ 2drop green ] }
|
{ [ dup 184 238 between? pick 0 223 between? and ] [ 2drop green ] }
|
||||||
{ [ dup 240 247 within pick 16 133 within and ] [ 2drop green ] }
|
{ [ dup 240 247 between? pick 16 133 between? and ] [ 2drop green ] }
|
||||||
{ [ dup 247 215 - 247 184 - within pick 0 223 within and ] [ 2drop red ] }
|
{ [ dup 247 215 - 247 184 - between? pick 0 223 between? and ] [ 2drop red ] }
|
||||||
[ 2drop white ]
|
[ 2drop white ]
|
||||||
} cond ;
|
} cond ;
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ fetched-in parsed-html links processed-in fetched-at ;
|
||||||
[ filter-base-links ] 2keep
|
[ filter-base-links ] 2keep
|
||||||
depth>> 1 + swap
|
depth>> 1 + swap
|
||||||
[ add-nonmatching ]
|
[ add-nonmatching ]
|
||||||
[ tuck [ apply-filters ] 2dip add-todo ] 2bi ;
|
[ dup '[ _ apply-filters ] curry 2dip add-todo ] 2bi ;
|
||||||
|
|
||||||
: normalize-hrefs ( base links -- links' )
|
: normalize-hrefs ( base links -- links' )
|
||||||
[ derive-url ] with map ;
|
[ derive-url ] with map ;
|
||||||
|
|
|
@ -37,7 +37,7 @@ TUPLE: piece
|
||||||
|
|
||||||
: modulo ( n m -- n )
|
: modulo ( n m -- n )
|
||||||
#! -2 7 mod => -2, -2 7 modulo => 5
|
#! -2 7 mod => -2, -2 7 modulo => 5
|
||||||
tuck mod over + swap mod ;
|
[ mod ] [ + ] [ mod ] tri ;
|
||||||
|
|
||||||
: (rotate-piece) ( rotation inc n-states -- rotation' )
|
: (rotate-piece) ( rotation inc n-states -- rotation' )
|
||||||
[ + ] dip modulo ;
|
[ + ] dip modulo ;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2007 Alex Chapman
|
! Copyright (C) 2007 Alex Chapman
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: combinators kernel generic math math.functions
|
USING: combinators kernel generic math math.functions
|
||||||
math.parser namespaces io sequences trees
|
math.parser namespaces io sequences trees shuffle
|
||||||
assocs parser accessors math.order prettyprint.custom ;
|
assocs parser accessors math.order prettyprint.custom ;
|
||||||
IN: trees.avl
|
IN: trees.avl
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (c) 2005 Mackenzie Straight.
|
! Copyright (c) 2005 Mackenzie Straight.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: arrays kernel math namespaces sequences assocs parser
|
USING: arrays kernel math namespaces sequences assocs parser
|
||||||
trees generic math.order accessors prettyprint.custom ;
|
trees generic math.order accessors prettyprint.custom shuffle ;
|
||||||
IN: trees.splay
|
IN: trees.splay
|
||||||
|
|
||||||
TUPLE: splay < tree ;
|
TUPLE: splay < tree ;
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel generic math sequences arrays io namespaces
|
USING: kernel generic math sequences arrays io namespaces
|
||||||
prettyprint.private kernel.private assocs random combinators
|
prettyprint.private kernel.private assocs random combinators
|
||||||
parser math.order accessors deques make prettyprint.custom ;
|
parser math.order accessors deques make prettyprint.custom
|
||||||
|
shuffle ;
|
||||||
IN: trees
|
IN: trees
|
||||||
|
|
||||||
TUPLE: tree root count ;
|
TUPLE: tree root count ;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (C) 2006, 2009 Slava Pestov.
|
! Copyright (C) 2006, 2009 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors math.vectors classes.tuple math.rectangles colors
|
USING: accessors math.vectors classes.tuple math.rectangles colors
|
||||||
kernel sequences models opengl math math.order namespaces
|
kernel locals sequences models opengl math math.order namespaces
|
||||||
ui.commands ui.gestures ui.render ui.gadgets ui.gadgets.labels
|
ui.commands ui.gestures ui.render ui.gadgets ui.gadgets.labels
|
||||||
ui.gadgets.scrollers ui.gadgets.presentations ui.gadgets.viewports
|
ui.gadgets.scrollers ui.gadgets.presentations ui.gadgets.viewports
|
||||||
ui.gadgets.packs ;
|
ui.gadgets.packs ;
|
||||||
|
@ -78,7 +78,7 @@ M: list focusable-child* drop t ;
|
||||||
dup list-empty? [
|
dup list-empty? [
|
||||||
2drop
|
2drop
|
||||||
] [
|
] [
|
||||||
tuck control-value length rem >>index
|
[ control-value length rem ] [ (>>index) ] [ ] tri
|
||||||
[ relayout-1 ] [ scroll>selected ] bi
|
[ relayout-1 ] [ scroll>selected ] bi
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
|
@ -95,9 +95,9 @@ M: list focusable-child* drop t ;
|
||||||
[ index>> ] keep nth-gadget invoke-secondary
|
[ index>> ] keep nth-gadget invoke-secondary
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
: select-gadget ( gadget list -- )
|
:: select-gadget ( gadget list -- )
|
||||||
tuck children>> index
|
gadget list children>> index
|
||||||
[ swap select-index ] [ drop ] if* ;
|
[ list select-index ] when* ;
|
||||||
|
|
||||||
: clamp-loc ( point max -- point )
|
: clamp-loc ( point max -- point )
|
||||||
vmin { 0 0 } vmax ;
|
vmin { 0 0 } vmax ;
|
||||||
|
|
|
@ -10,7 +10,7 @@ IN: units.tests
|
||||||
[ t ] [ 5 m 1 m d- 4 m = ] unit-test
|
[ t ] [ 5 m 1 m d- 4 m = ] unit-test
|
||||||
[ t ] [ 5 m 2 m d* 10 m^2 = ] unit-test
|
[ t ] [ 5 m 2 m d* 10 m^2 = ] unit-test
|
||||||
[ t ] [ 5 m 2 m d/ 5/2 { } { } <dimensioned> = ] unit-test
|
[ t ] [ 5 m 2 m d/ 5/2 { } { } <dimensioned> = ] unit-test
|
||||||
[ t ] [ 5 m 2 m tuck d/ drop 2 m = ] unit-test
|
[ t ] [ 2 m 5 m 2 m d/ drop 2 m = ] unit-test
|
||||||
|
|
||||||
[ t ] [ 1 m 2 m 3 m 3array d-product 6 m^3 = ] unit-test
|
[ t ] [ 1 m 2 m 3 m 3array d-product 6 m^3 = ] unit-test
|
||||||
[ t ] [ 3 m d-recip 1/3 { } { m } <dimensioned> = ] unit-test
|
[ t ] [ 3 m d-recip 1/3 { } { m } <dimensioned> = ] unit-test
|
||||||
|
|
|
@ -48,7 +48,7 @@ MEMO: cities-named ( name -- cities )
|
||||||
|
|
||||||
MEMO: cities-named-in ( name state -- cities )
|
MEMO: cities-named-in ( name state -- cities )
|
||||||
cities [
|
cities [
|
||||||
tuck [ name>> = ] [ state>> = ] 2bi* and
|
[ name>> = ] [ state>> = ] bi-curry bi* and
|
||||||
] with with filter ;
|
] with with filter ;
|
||||||
|
|
||||||
: find-zip-code ( code -- city )
|
: find-zip-code ( code -- city )
|
||||||
|
|
Loading…
Reference in New Issue