From 388bf88d0f0d443468ab892a49dff073f13e74f7 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 22 Sep 2008 19:39:27 -0700 Subject: [PATCH 01/10] Some cleanup of printf and better docs. --- extra/printf/printf-docs.factor | 8 +++++++- extra/printf/printf-tests.factor | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/extra/printf/printf-docs.factor b/extra/printf/printf-docs.factor index fdecc2ad68..f2369a7213 100755 --- a/extra/printf/printf-docs.factor +++ b/extra/printf/printf-docs.factor @@ -39,7 +39,9 @@ HELP: sprintf { $see-also printf } ; ARTICLE: "printf" "Formatted printing" -"The " { $vocab-link "printf" } " and " { $vocab-link "sprintf" } " words are used for formatted printing.\n" +"The " { $vocab-link "printf" } " vocabulary is used for formatted printing.\n" +{ $subsection printf } +{ $subsection sprintf } "\n" "Several format specifications exist for handling arguments of different types, and specifying attributes for the result string, including such things as maximum width, padding, and decimals.\n" { $table @@ -72,3 +74,7 @@ ARTICLE: "printf" "Formatted printing" "\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point." "\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent." } ; + +ABOUT: "printf" + + diff --git a/extra/printf/printf-tests.factor b/extra/printf/printf-tests.factor index b365343bf0..5302048dfe 100644 --- a/extra/printf/printf-tests.factor +++ b/extra/printf/printf-tests.factor @@ -7,6 +7,10 @@ USING: kernel printf tools.test ; [ "%s" sprintf ] must-infer +[ t ] [ "" "" sprintf = ] unit-test + +[ t ] [ "asdf" "asdf" sprintf = ] unit-test + [ t ] [ "10" 10 "%d" sprintf = ] unit-test [ t ] [ "+10" 10 "%+d" sprintf = ] unit-test From 7435df76844f5f6df24905e43fd860c48899b3df Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Sep 2008 10:33:36 -0700 Subject: [PATCH 02/10] Adding first version of math.finance. --- extra/math/finance/authors.txt | 1 + extra/math/finance/finance-docs.factor | 38 +++++++++++++++++++++++++ extra/math/finance/finance-tests.factor | 8 ++++++ extra/math/finance/finance.factor | 36 +++++++++++++++++++++++ extra/math/finance/summary.txt | 1 + 5 files changed, 84 insertions(+) create mode 100644 extra/math/finance/authors.txt create mode 100644 extra/math/finance/finance-docs.factor create mode 100644 extra/math/finance/finance-tests.factor create mode 100644 extra/math/finance/finance.factor create mode 100644 extra/math/finance/summary.txt diff --git a/extra/math/finance/authors.txt b/extra/math/finance/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/math/finance/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/math/finance/finance-docs.factor b/extra/math/finance/finance-docs.factor new file mode 100644 index 0000000000..9094a5bff6 --- /dev/null +++ b/extra/math/finance/finance-docs.factor @@ -0,0 +1,38 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: help.markup help.syntax ; + +IN: math.finance + +HELP: enumerate +{ $values { "seq" "a sequence" } { "newseq" "a sequence" } } +{ $description "Returns a new sequence where each element is an array of { value, index }" } ; + +HELP: sma +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description "Returns the Simple Moving Average with the specified periodicity." } ; + +HELP: ema +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Exponential Moving Average with the specified periodicity, calculated by:\n" + { $list + "A = 2.0 / (N + 1)" + "EMA[t] = (A * SEQ[t]) + ((1-A) * EMA[t-1])" } +} ; + +HELP: macd +{ $values { "seq" "a sequence" } { "n1" "short number of periods" } { "n2" "long number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Moving Average Converge of the sequence, calculated by:\n" + { $list "MACD[t] = EMA2[t] - EMA1[t]" } +} ; + +HELP: momentum +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Momentum of the sequence, calculated by:\n" + { $list "MOM[t] = SEQ[t] - SEQ[t-n]" } +} ; + diff --git a/extra/math/finance/finance-tests.factor b/extra/math/finance/finance-tests.factor new file mode 100644 index 0000000000..dce701bb2f --- /dev/null +++ b/extra/math/finance/finance-tests.factor @@ -0,0 +1,8 @@ +USING: kernel math math.functions math.finance tools.test ; + +IN: math.finance.tests + +[ { 2 4 } ] [ { 1 3 5 } 2 sma ] unit-test + +[ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test + diff --git a/extra/math/finance/finance.factor b/extra/math/finance/finance.factor new file mode 100644 index 0000000000..75ea693862 --- /dev/null +++ b/extra/math/finance/finance.factor @@ -0,0 +1,36 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: arrays kernel grouping math math.statistics sequences ; + +IN: math.finance + +: enumerate ( seq -- newseq ) + #! Returns a sequence where each element and its index + -1 swap [ [ 1+ ] dip swap [ 2array ] keep swap ] { } map-as swap drop ; + +: ema ( seq n -- newseq ) + #! An exponentially-weighted moving average: + #! A = 2.0 / (N + 1) + #! EMA[t] = (A * VAL[t]) + ((1-A) * EMA[t-1]) + 1+ 2.0 swap / dup 1 swap - swap rot + [ [ dup ] dip * ] map swap drop 0 swap + [ [ dup ] 2dip [ * ] dip + dup ] map + [ drop drop ] dip 1 tail-slice >array ; + +: sma ( seq n -- newseq ) + #! Simple moving average + clump [ mean ] map ; + +: macd ( seq n1 n2 -- newseq ) + #! Moving Average Convergence Divergence + #! MACD[t] = EMA2[t] - EMA1[t] + rot dup ema [ swap ema ] dip [ - ] 2map ; + +: momentum ( seq n -- newseq ) + #! Momentum + #! M[t] = P[t] - P[t-n] + 2dup tail-slice -rot swap [ length ] keep + [ - neg ] dip swap head-slice [ - ] 2map ; + + diff --git a/extra/math/finance/summary.txt b/extra/math/finance/summary.txt new file mode 100644 index 0000000000..9fb3bc8aa7 --- /dev/null +++ b/extra/math/finance/summary.txt @@ -0,0 +1 @@ +Moving averages and other calculations useful for finance. From 2521de100d0afa59eb34ff1c52bff0b599168ed8 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Sep 2008 14:10:04 -0700 Subject: [PATCH 03/10] Some improvements to math-finance based on feedback. --- extra/math/finance/finance.factor | 35 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/extra/math/finance/finance.factor b/extra/math/finance/finance.factor index 75ea693862..ffb02208d3 100644 --- a/extra/math/finance/finance.factor +++ b/extra/math/finance/finance.factor @@ -1,36 +1,37 @@ ! Copyright (C) 2008 John Benediktsson ! See http://factorcode.org/license.txt for BSD license -USING: arrays kernel grouping math math.statistics sequences ; +USING: arrays assocs fry kernel grouping math math.statistics math.vectors sequences ; IN: math.finance : enumerate ( seq -- newseq ) - #! Returns a sequence where each element and its index - -1 swap [ [ 1+ ] dip swap [ 2array ] keep swap ] { } map-as swap drop ; + >alist ; + + : ema ( seq n -- newseq ) - #! An exponentially-weighted moving average: - #! A = 2.0 / (N + 1) - #! EMA[t] = (A * VAL[t]) + ((1-A) * EMA[t-1]) - 1+ 2.0 swap / dup 1 swap - swap rot - [ [ dup ] dip * ] map swap drop 0 swap - [ [ dup ] 2dip [ * ] dip + dup ] map - [ drop drop ] dip 1 tail-slice >array ; + a swap first-rest swap '[ [ dup ] 2dip swap rot weighted ] accumulate 2nip ; : sma ( seq n -- newseq ) - #! Simple moving average clump [ mean ] map ; : macd ( seq n1 n2 -- newseq ) - #! Moving Average Convergence Divergence - #! MACD[t] = EMA2[t] - EMA1[t] - rot dup ema [ swap ema ] dip [ - ] 2map ; + rot dup ema [ swap ema ] dip v- ; : momentum ( seq n -- newseq ) - #! Momentum - #! M[t] = P[t] - P[t-n] 2dup tail-slice -rot swap [ length ] keep - [ - neg ] dip swap head-slice [ - ] 2map ; + [ - neg ] dip swap head-slice v- ; From 78b5e36cac3757cf5e17e7eafbf1ffe3265a0581 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Sep 2008 14:36:21 -0700 Subject: [PATCH 04/10] Adding math-compare vocabulary. --- extra/math/compare/authors.txt | 1 + extra/math/compare/compare-docs.factor | 37 +++++++++++++++++++++++++ extra/math/compare/compare-tests.factor | 25 +++++++++++++++++ extra/math/compare/compare.factor | 19 +++++++++++++ extra/math/compare/summary.txt | 1 + 5 files changed, 83 insertions(+) create mode 100644 extra/math/compare/authors.txt create mode 100644 extra/math/compare/compare-docs.factor create mode 100644 extra/math/compare/compare-tests.factor create mode 100644 extra/math/compare/compare.factor create mode 100644 extra/math/compare/summary.txt diff --git a/extra/math/compare/authors.txt b/extra/math/compare/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/math/compare/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/math/compare/compare-docs.factor b/extra/math/compare/compare-docs.factor new file mode 100644 index 0000000000..eb199cd5fe --- /dev/null +++ b/extra/math/compare/compare-docs.factor @@ -0,0 +1,37 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: help.markup help.syntax ; + +IN: math.compare + +HELP: absmin +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the smaller absolute number with the original sign." +} ; + +HELP: absmax +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the larger absolute number with the original sign." +} ; + +HELP: posmax +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the most-positive value, or zero if both are negative." +} ; + +HELP: negmin +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the most-negative value, or zero if both are positive." +} ; + +HELP: clamp +{ $values { "a" "a number" } { "value" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the value when between 'a' and 'b', 'a' if <= 'a', or 'b' if >= 'b'." +} ; + diff --git a/extra/math/compare/compare-tests.factor b/extra/math/compare/compare-tests.factor new file mode 100644 index 0000000000..57d2d36cbc --- /dev/null +++ b/extra/math/compare/compare-tests.factor @@ -0,0 +1,25 @@ +USING: kernel math math.functions math.compare tools.test ; + +IN: math.compare.tests + +[ -1 ] [ -1 5 absmin ] unit-test +[ -1 ] [ -1 -5 absmin ] unit-test + +[ -5 ] [ 1 -5 absmax ] unit-test +[ 5 ] [ 1 5 absmax ] unit-test + +[ 0 ] [ -1 -3 posmax ] unit-test +[ 1 ] [ 1 -3 posmax ] unit-test +[ 3 ] [ -1 3 posmax ] unit-test + +[ 0 ] [ 1 3 negmin ] unit-test +[ -3 ] [ 1 -3 negmin ] unit-test +[ -1 ] [ -1 3 negmin ] unit-test + +[ 0 ] [ 0 -1 2 clamp ] unit-test +[ 1 ] [ 0 1 2 clamp ] unit-test +[ 2 ] [ 0 3 2 clamp ] unit-test + + + + diff --git a/extra/math/compare/compare.factor b/extra/math/compare/compare.factor new file mode 100644 index 0000000000..93f501a059 --- /dev/null +++ b/extra/math/compare/compare.factor @@ -0,0 +1,19 @@ +USING: math math.order kernel ; + +IN: math.compare + +: absmin ( a b -- x ) + [ [ abs ] dip abs < ] 2keep ? ; + +: absmax ( a b -- x ) + [ [ abs ] dip abs > ] 2keep ? ; + +: posmax ( a b -- x ) + 0 max max ; + +: negmin ( a b -- x ) + 0 min min ; + +: clamp ( a value b -- x ) + min max ; + diff --git a/extra/math/compare/summary.txt b/extra/math/compare/summary.txt new file mode 100644 index 0000000000..95edea509b --- /dev/null +++ b/extra/math/compare/summary.txt @@ -0,0 +1 @@ +Comparison functions. From 8ad11e7906bf6ac313a135074dc0f3a1e83c34e0 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Sep 2008 14:50:28 -0700 Subject: [PATCH 05/10] Adding copyrights. --- extra/math/compare/compare-tests.factor | 3 +++ extra/math/compare/compare.factor | 3 +++ 2 files changed, 6 insertions(+) diff --git a/extra/math/compare/compare-tests.factor b/extra/math/compare/compare-tests.factor index 57d2d36cbc..765f34e695 100644 --- a/extra/math/compare/compare-tests.factor +++ b/extra/math/compare/compare-tests.factor @@ -1,3 +1,6 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + USING: kernel math math.functions math.compare tools.test ; IN: math.compare.tests diff --git a/extra/math/compare/compare.factor b/extra/math/compare/compare.factor index 93f501a059..116daa1ca6 100644 --- a/extra/math/compare/compare.factor +++ b/extra/math/compare/compare.factor @@ -1,3 +1,6 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + USING: math math.order kernel ; IN: math.compare From 934833287d115b128b3a5ce5304d914da8955a52 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Sep 2008 21:24:04 -0700 Subject: [PATCH 06/10] Simplified math-compare by using bi@. --- extra/math/compare/compare.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/math/compare/compare.factor b/extra/math/compare/compare.factor index 116daa1ca6..28a8eadc81 100644 --- a/extra/math/compare/compare.factor +++ b/extra/math/compare/compare.factor @@ -6,10 +6,10 @@ USING: math math.order kernel ; IN: math.compare : absmin ( a b -- x ) - [ [ abs ] dip abs < ] 2keep ? ; + [ [ abs ] bi@ < ] 2keep ? ; : absmax ( a b -- x ) - [ [ abs ] dip abs > ] 2keep ? ; + [ [ abs ] bi@ > ] 2keep ? ; : posmax ( a b -- x ) 0 max max ; From fe95fb813efeece3a5df9b07d32dec3cfae5e51c Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Wed, 24 Sep 2008 12:24:01 -0700 Subject: [PATCH 07/10] Adding distribute word to math-finance. --- extra/math/finance/finance-docs.factor | 28 +++++++++++++++++++++++++ extra/math/finance/finance-tests.factor | 6 ++++++ extra/math/finance/finance.factor | 12 ++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/extra/math/finance/finance-docs.factor b/extra/math/finance/finance-docs.factor index 9094a5bff6..10deec8a69 100644 --- a/extra/math/finance/finance-docs.factor +++ b/extra/math/finance/finance-docs.factor @@ -9,6 +9,34 @@ HELP: enumerate { $values { "seq" "a sequence" } { "newseq" "a sequence" } } { $description "Returns a new sequence where each element is an array of { value, index }" } ; +HELP: distribute +{ $values { "amount" "a number of amount" } { "n" "a number of buckets" } { "seq" "a sequence" } } +{ $description + "Distribute 'amount' in 'n' buckets, as equally as possible. Returns a list of 'n' elements that sum to 'amount'.\n" +} +{ $examples + { $example + "USING: math.finance" + "3 1 distribute" + "{ 3 }" } + { $example + "USING: math.finance" + "3 3 distribute" + "{ 1 1 1 }" } + { $example + "USING: math.finance" + "5 3 distribute" + "{ 2 1 2 }" } + { $example + "USING: math.finance" + "3 5 distribute" + "{ 1 0 1 0 1 }" } + { $example + "USING: math.finance" + "1000 7 distribute" + "{ 143 143 143 142 143 143 143 }" } +} ; + HELP: sma { $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } { $description "Returns the Simple Moving Average with the specified periodicity." } ; diff --git a/extra/math/finance/finance-tests.factor b/extra/math/finance/finance-tests.factor index dce701bb2f..8c98583c84 100644 --- a/extra/math/finance/finance-tests.factor +++ b/extra/math/finance/finance-tests.factor @@ -6,3 +6,9 @@ IN: math.finance.tests [ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test +[ { 3 } ] [ 3 1 distribute ] unit-test +[ { 1 1 1 } ] [ 3 3 distribute ] unit-test +[ { 2 1 2 } ] [ 5 3 distribute ] unit-test +[ { 1 0 1 0 1 } ] [ 3 5 distribute ] unit-test +[ { 143 143 143 142 143 143 143 } ] [ 1000 7 distribute ] unit-test + diff --git a/extra/math/finance/finance.factor b/extra/math/finance/finance.factor index ffb02208d3..ce59836442 100644 --- a/extra/math/finance/finance.factor +++ b/extra/math/finance/finance.factor @@ -1,13 +1,20 @@ ! Copyright (C) 2008 John Benediktsson ! See http://factorcode.org/license.txt for BSD license -USING: arrays assocs fry kernel grouping math math.statistics math.vectors sequences ; +USING: arrays assocs kernel grouping sequences shuffle +math math.functions math.statistics math.vectors ; IN: math.finance : enumerate ( seq -- newseq ) >alist ; +: distribute ( amount n -- seq ) + [ / ] keep 0 [ 0 0 ] dip + [ + [ [ dup ] dip + ] dip + [ dup round ] dip 2dup - + [ drop ] dip ] map 3nip ; + : ema ( seq n -- newseq ) - a swap first-rest swap '[ [ dup ] 2dip swap rot weighted ] accumulate 2nip ; + a swap first-rest swap [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ; : sma ( seq n -- newseq ) clump [ mean ] map ; @@ -34,4 +41,3 @@ PRIVATE> 2dup tail-slice -rot swap [ length ] keep [ - neg ] dip swap head-slice v- ; - From fd969a3a7f41435f4b936e89598cd5158bbe3b38 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 25 Sep 2008 13:11:48 -0700 Subject: [PATCH 08/10] Adding a randomize word to sequences-lib. --- extra/sequences/lib/lib-docs.factor | 5 +++++ extra/sequences/lib/lib.factor | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/extra/sequences/lib/lib-docs.factor b/extra/sequences/lib/lib-docs.factor index 9975da00db..197d092aa2 100755 --- a/extra/sequences/lib/lib-docs.factor +++ b/extra/sequences/lib/lib-docs.factor @@ -18,3 +18,8 @@ HELP: each-withn "passed to the quotation given to each-withn for each element in the sequence." } { $see-also map-withn } ; + +HELP: randomize +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Shuffle the elements in the sequence randomly, returning the new sequence." } ; + diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index 690d7f4b76..8bf95b6bc3 100755 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -160,3 +160,16 @@ PRIVATE> : ?nth* ( n seq -- elt/f ? ) 2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +USE: math.ranges +USE: random +: randomize ( seq -- seq' ) + dup length 1- + [ dup 1 > ] [ + [ [0,b) random ] keep dup + [ rot [ exchange ] keep ] dip 1- + ] [ drop ] while ; + + From 5bb148fd10f628c91cf7a42304402404fe1269fa Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 25 Sep 2008 13:15:39 -0700 Subject: [PATCH 09/10] Removing distribute word from math-finance. --- extra/math/finance/finance-docs.factor | 28 ------------------------- extra/math/finance/finance-tests.factor | 6 ------ extra/math/finance/finance.factor | 6 ------ 3 files changed, 40 deletions(-) diff --git a/extra/math/finance/finance-docs.factor b/extra/math/finance/finance-docs.factor index 10deec8a69..9094a5bff6 100644 --- a/extra/math/finance/finance-docs.factor +++ b/extra/math/finance/finance-docs.factor @@ -9,34 +9,6 @@ HELP: enumerate { $values { "seq" "a sequence" } { "newseq" "a sequence" } } { $description "Returns a new sequence where each element is an array of { value, index }" } ; -HELP: distribute -{ $values { "amount" "a number of amount" } { "n" "a number of buckets" } { "seq" "a sequence" } } -{ $description - "Distribute 'amount' in 'n' buckets, as equally as possible. Returns a list of 'n' elements that sum to 'amount'.\n" -} -{ $examples - { $example - "USING: math.finance" - "3 1 distribute" - "{ 3 }" } - { $example - "USING: math.finance" - "3 3 distribute" - "{ 1 1 1 }" } - { $example - "USING: math.finance" - "5 3 distribute" - "{ 2 1 2 }" } - { $example - "USING: math.finance" - "3 5 distribute" - "{ 1 0 1 0 1 }" } - { $example - "USING: math.finance" - "1000 7 distribute" - "{ 143 143 143 142 143 143 143 }" } -} ; - HELP: sma { $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } { $description "Returns the Simple Moving Average with the specified periodicity." } ; diff --git a/extra/math/finance/finance-tests.factor b/extra/math/finance/finance-tests.factor index 8c98583c84..dce701bb2f 100644 --- a/extra/math/finance/finance-tests.factor +++ b/extra/math/finance/finance-tests.factor @@ -6,9 +6,3 @@ IN: math.finance.tests [ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test -[ { 3 } ] [ 3 1 distribute ] unit-test -[ { 1 1 1 } ] [ 3 3 distribute ] unit-test -[ { 2 1 2 } ] [ 5 3 distribute ] unit-test -[ { 1 0 1 0 1 } ] [ 3 5 distribute ] unit-test -[ { 143 143 143 142 143 143 143 } ] [ 1000 7 distribute ] unit-test - diff --git a/extra/math/finance/finance.factor b/extra/math/finance/finance.factor index ce59836442..8d33c1eb7d 100644 --- a/extra/math/finance/finance.factor +++ b/extra/math/finance/finance.factor @@ -9,12 +9,6 @@ IN: math.finance : enumerate ( seq -- newseq ) >alist ; -: distribute ( amount n -- seq ) - [ / ] keep 0 [ 0 0 ] dip - [ + [ [ dup ] dip + ] dip - [ dup round ] dip 2dup - - [ drop ] dip ] map 3nip ; - Date: Thu, 25 Sep 2008 13:20:30 -0700 Subject: [PATCH 10/10] Improvements to randomize based on IRC feedback. --- extra/sequences/lib/lib.factor | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index 8bf95b6bc3..a7202c9cae 100755 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -166,10 +166,5 @@ PRIVATE> USE: math.ranges USE: random : randomize ( seq -- seq' ) - dup length 1- - [ dup 1 > ] [ - [ [0,b) random ] keep dup - [ rot [ exchange ] keep ] dip 1- - ] [ drop ] while ; - + dup length 1 swap [a,b) [ dup random pick exchange ] each ;