From 38fd731fa9d8104e102b656da614aacce38f56f9 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 7 Apr 2011 18:14:26 -0400 Subject: [PATCH 1/7] factor.sh: change install-x11 and install-macosx to deps-linux and deps-macosx which install dependencies only; add Gtk dependecies to apt-get line --- build-support/factor.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/build-support/factor.sh b/build-support/factor.sh index 46cbaac6fa..b070abe0b3 100755 --- a/build-support/factor.sh +++ b/build-support/factor.sh @@ -549,12 +549,12 @@ make_boot_image() { } -install_build_system_apt() { - sudo apt-get --yes install libc6-dev libpango1.0-dev libx11-dev xorg-dev wget git-core git-doc rlwrap gcc make +install_deps_linux() { + sudo apt-get --yes install libc6-dev libpango1.0-dev libx11-dev xorg-dev libgtk2.0-dev libgtkglext1-dev wget git-core git-doc rlwrap gcc make check_ret sudo } -install_build_system_port() { +install_deps_macosx() { test_program_installed git if [[ $? -ne 1 ]] ; then ensure_program_installed yes @@ -588,8 +588,8 @@ set_delete case "$1" in install) install ;; - install-x11) install_build_system_apt; install ;; - install-macosx) install_build_system_port; install ;; + deps-linux) install_deps_linux ;; + deps-macosx) install_deps_macosx ;; self-update) update; make_boot_image; bootstrap;; quick-update) update; refresh_image ;; update) update; update_bootstrap ;; From 7aa7b08d425d7c0ca67ba29372acd97a346fc343 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 9 Apr 2011 23:51:14 -0500 Subject: [PATCH 2/7] Change how sequence>assoc and friends work. Now they take two quotations, the first of which prepares the key/value pair and the second insert it into the assoc. mrjbq7's group-by, formerly collect-values, is now called collect-by. --- basis/math/statistics/statistics-docs.factor | 19 ++++++------- basis/math/statistics/statistics.factor | 28 +++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/basis/math/statistics/statistics-docs.factor b/basis/math/statistics/statistics-docs.factor index 63263e603c..c43106a977 100644 --- a/basis/math/statistics/statistics-docs.factor +++ b/basis/math/statistics/statistics-docs.factor @@ -112,41 +112,42 @@ HELP: sorted-histogram HELP: sequence>assoc { $values - { "seq" sequence } { "quot" quotation } { "exemplar" "an exemplar assoc" } + { "seq" sequence } { "quot1" quotation } { "quot2" quotation } { "exemplar" "an exemplar assoc" } { "assoc" assoc } } -{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } " according to the passed quotation." } +{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } ". The first quotation gets passed an element from the sequence and should output whatever the second quotation needs, e.g. ( element -- value key ) if the second quotation is inserting into an assoc." } { $examples { $example "! Iterate over a sequence and increment the count at each element" + "! The first quotation has stack effect ( key -- key ), a no-op" "USING: assocs prettyprint math.statistics ;" - "\"aaabc\" [ inc-at ] H{ } sequence>assoc ." + "\"aaabc\" [ ] [ inc-at ] H{ } sequence>assoc ." "H{ { 97 3 } { 98 1 } { 99 1 } }" } } ; HELP: sequence>assoc! { $values - { "assoc" assoc } { "seq" sequence } { "quot" quotation } + { "assoc" assoc } { "seq" sequence } { "quot1" quotation } { "quot2" quotation } } -{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } " according to the passed quotation." } +{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } ". The first quotation gets passed an element from the sequence and should output whatever the second quotation needs, e.g. ( element -- value key ) if the second quotation is inserting into an assoc." } { $examples { $example "! Iterate over a sequence and add the counts to an existing assoc" "USING: assocs prettyprint math.statistics kernel ;" - "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ inc-at ] sequence>assoc! ." + "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ ] [ inc-at ] sequence>assoc! ." "H{ { 97 5 } { 98 2 } { 99 1 } }" } } ; HELP: sequence>hashtable { $values - { "seq" sequence } { "quot" quotation } + { "seq" sequence } { "quot1" quotation } { "quot2" quotation } { "hashtable" hashtable } } -{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according to the passed quotation." } +{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according a combination of the first and second quotations. The quot1 is passed each element, and quot2 gets the hashtable on the top of the stack with quot1's results underneath for inserting into the hashtable." } { $examples { $example "! Count the number of times an element occurs in a sequence" "USING: assocs prettyprint math.statistics ;" - "\"aaabc\" [ inc-at ] sequence>hashtable ." + "\"aaabc\" [ ] [ inc-at ] sequence>hashtable ." "H{ { 97 3 } { 98 1 } { 99 1 } }" } } ; diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index e5b5fb0872..ae7114423b 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -1,8 +1,7 @@ ! Copyright (C) 2008 Doug Coleman, Michael Judge. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays combinators kernel math math.functions -math.order sequences sorting locals sequences.private -assocs fry ; +USING: assocs combinators generalizations kernel locals math +math.functions math.order sequences sequences.private sorting ; IN: math.statistics : mean ( seq -- x ) @@ -59,31 +58,34 @@ IN: math.statistics assoc) ( seq quot assoc -- assoc ) - [ swap curry each ] keep ; inline +: (sequence>assoc) ( seq quot1 quot2 assoc -- assoc ) + [ swap curry compose each ] keep ; inline PRIVATE> -: sequence>assoc! ( assoc seq quot: ( obj assoc -- ) -- assoc ) - rot (sequence>assoc) ; inline +: sequence>assoc! ( assoc seq quot1 quot2 -- assoc ) + 4 nrot (sequence>assoc) ; inline -: sequence>assoc ( seq quot: ( obj assoc -- ) exemplar -- assoc ) +: sequence>assoc ( seq quot1 quot2 exemplar -- assoc ) clone (sequence>assoc) ; inline -: sequence>hashtable ( seq quot: ( obj hashtable -- ) -- hashtable ) +: sequence>hashtable ( seq quot1 quot2 -- hashtable ) H{ } sequence>assoc ; inline : histogram! ( hashtable seq -- hashtable ) - [ inc-at ] sequence>assoc! ; + [ ] [ inc-at ] sequence>assoc! ; : histogram ( seq -- hashtable ) - [ inc-at ] sequence>hashtable ; + [ ] [ inc-at ] sequence>hashtable ; : sorted-histogram ( seq -- alist ) histogram >alist sort-values ; -: collect-values ( seq quot: ( obj hashtable -- ) -- hash ) - '[ [ dup @ ] dip push-at ] sequence>hashtable ; inline +: collect-pairs ( seq quot -- hashtable ) + [ push-at ] sequence>hashtable ; inline + +: collect-by ( seq quot -- hashtable ) + [ dup ] prepose collect-pairs ; inline : mode ( seq -- x ) histogram >alist From 405745ca79c2a0462487c4e61e85d69f4ddb1912 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 10 Apr 2011 12:35:46 -0500 Subject: [PATCH 3/7] Add support for parsing DNS TXT queries. --- extra/dns/dns.factor | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/extra/dns/dns.factor b/extra/dns/dns.factor index 802d4d7277..d9a283a6a5 100644 --- a/extra/dns/dns.factor +++ b/extra/dns/dns.factor @@ -6,7 +6,7 @@ io io.binary io.encodings.binary io.encodings.string io.encodings.utf8 io.sockets io.sockets.private io.streams.byte-array io.timeouts kernel make math math.bitwise math.parser namespaces nested-comments random sequences -slots.syntax splitting system vectors vocabs.loader ; +slots.syntax splitting system vectors vocabs.loader strings ; IN: dns : with-input-seek ( n seek-type quot -- ) @@ -286,6 +286,9 @@ M: SOA rdata>byte-array } cleave ] B{ } append-outputs-as ; +M: TXT rdata>byte-array + drop ; + : rr>byte-array ( rr -- byte-array ) [ { @@ -333,6 +336,26 @@ M: SOA rdata>byte-array : dns-AAAA-query ( domain -- message ) AAAA IN dns-query ; : dns-MX-query ( domain -- message ) MX IN dns-query ; : dns-NS-query ( domain -- message ) NS IN dns-query ; +: dns-TXT-query ( domain -- message ) TXT IN dns-query ; + +: TXT-message>strings ( message -- strings ) + answer-section>> + [ rdata>> + [ + binary [ + [ + read1 [ + read , t + ] [ + f + ] if* + ] loop + ] with-input-stream + ] { } make [ >string ] map + ] map ; + +: TXT. ( domain -- ) + dns-TXT-query TXT-message>strings [ [ print ] each ] each ; : reverse-lookup ( reversed-ip -- message ) PTR IN dns-query ; From 82a88ba57ad8713f5ebf19df6eb0806e68207cce Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 10 Apr 2011 12:38:00 -0500 Subject: [PATCH 4/7] Decode TXT strings as utf8, use write instead of print. --- extra/dns/dns.factor | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/extra/dns/dns.factor b/extra/dns/dns.factor index d9a283a6a5..70b4275660 100644 --- a/extra/dns/dns.factor +++ b/extra/dns/dns.factor @@ -344,18 +344,14 @@ M: TXT rdata>byte-array [ binary [ [ - read1 [ - read , t - ] [ - f - ] if* + read1 [ read , t ] [ f ] if* ] loop ] with-input-stream - ] { } make [ >string ] map + ] { } make [ utf8 decode ] map ] map ; : TXT. ( domain -- ) - dns-TXT-query TXT-message>strings [ [ print ] each ] each ; + dns-TXT-query TXT-message>strings [ [ write ] each ] each ; : reverse-lookup ( reversed-ip -- message ) PTR IN dns-query ; From 250d96d7b18242506d5943fa99656eed2e1e4a3d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 10 Apr 2011 12:57:39 -0500 Subject: [PATCH 5/7] Clean up heaps implementation, test max-heaps. --- basis/heaps/heaps-tests.factor | 26 ++++++++++---- basis/heaps/heaps.factor | 63 +++++++++++++++------------------- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/basis/heaps/heaps-tests.factor b/basis/heaps/heaps-tests.factor index 703cf53080..64871a69e5 100644 --- a/basis/heaps/heaps-tests.factor +++ b/basis/heaps/heaps-tests.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays kernel math namespaces tools.test heaps heaps.private math.parser random assocs sequences sorting -accessors math.order ; +accessors math.order locals ; IN: heaps.tests [ heap-pop ] must-fail @@ -27,19 +27,31 @@ IN: heaps.tests [ 0 ] [ heap-size ] unit-test [ 1 ] [ t 1 pick heap-push heap-size ] unit-test -: heap-sort ( alist -- keys ) - [ heap-push-all ] keep heap-pop-all ; +: heap-sort ( alist heap -- keys ) + [ heap-push-all ] keep heap-pop-all ; : random-alist ( n -- alist ) iota [ drop 32 random-bits dup number>string - ] H{ } map>assoc ; + ] H{ } map>assoc >alist ; -: test-heap-sort ( n -- ? ) - random-alist dup >alist sort-keys swap heap-sort = ; +:: test-heap-sort ( n heap reverse? -- ? ) + n random-alist + [ sort-keys reverse? [ reverse ] when ] keep + heap heap-sort = ; + +: test-minheap-sort ( n -- ? ) + f test-heap-sort ; + +: test-maxheap-sort ( n -- ? ) + t test-heap-sort ; 14 [ - [ t ] swap [ 2^ test-heap-sort ] curry unit-test + [ t ] swap [ 2^ f test-heap-sort ] curry unit-test +] each-integer + +14 [ + [ t ] swap [ 2^ t test-heap-sort ] curry unit-test ] each-integer : test-entry-indices ( n -- ? ) diff --git a/basis/heaps/heaps.factor b/basis/heaps/heaps.factor index 28d18cb53a..326266773b 100644 --- a/basis/heaps/heaps.factor +++ b/basis/heaps/heaps.factor @@ -2,7 +2,7 @@ ! Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel math sequences arrays assocs sequences.private -growable accessors math.order summary vectors ; +growable accessors math.order summary vectors fry combinators ; IN: heaps GENERIC: heap-push* ( value key heap -- entry ) @@ -58,30 +58,25 @@ M: heap heap-size ( heap -- n ) [ right ] dip data-nth ; inline : data-set-nth ( entry n heap -- ) - [ [ >>index drop ] 2keep ] dip + [ [ >>index drop ] [ ] 2bi ] dip data>> set-nth-unsafe ; inline : data-push ( entry heap -- n ) dup heap-size [ swap 2dup data>> ensure 2drop data-set-nth - ] keep ; inline - -: data-pop ( heap -- entry ) - data>> pop ; inline - -: data-pop* ( heap -- ) - data>> pop* ; inline + ] [ + ] bi ; inline : data-first ( heap -- entry ) data>> first ; inline : data-exchange ( m n heap -- ) - [ [ data-nth ] curry bi@ ] - [ [ data-set-nth ] curry bi@ ] 3bi ; inline + [ '[ _ data-nth ] bi@ ] + [ '[ _ data-set-nth ] bi@ ] 3bi ; inline -GENERIC: heap-compare ( pair1 pair2 heap -- ? ) +GENERIC: heap-compare ( entry1 entry2 heap -- ? ) -: (heap-compare) ( pair1 pair2 heap -- <=> ) +: (heap-compare) ( entry1 entry2 heap -- <=> ) drop [ key>> ] compare ; inline M: min-heap heap-compare (heap-compare) +gt+ eq? ; @@ -97,16 +92,17 @@ M: max-heap heap-compare (heap-compare) +lt+ eq? ; : right-bounds-check? ( m heap -- ? ) [ right ] dip heap-bounds-check? ; inline -: continue? ( m up[m] heap -- ? ) - [ data-nth swap ] keep [ data-nth ] keep - heap-compare ; inline +: continue? ( m n heap -- ? ) + [ data-nth nip ] + [ nip data-nth ] + [ 2nip ] 3tri heap-compare ; DEFER: up-heap : (up-heap) ( n heap -- ) [ dup up ] dip 3dup continue? [ - [ data-exchange ] 2keep up-heap + [ data-exchange ] [ up-heap ] 2bi ] [ 3drop ] if ; inline recursive @@ -115,10 +111,8 @@ DEFER: up-heap over 0 > [ (up-heap) ] [ 2drop ] if ; inline recursive : (child) ( m heap -- n ) - 2dup right-value - [ 2dup left-value ] dip - rot heap-compare - [ right ] [ left ] if ; + { [ drop ] [ left-value ] [ right-value ] [ nip ] } 2cleave + heap-compare [ right ] [ left ] if ; : child ( m heap -- n ) 2dup right-bounds-check? @@ -127,11 +121,11 @@ DEFER: up-heap DEFER: down-heap : (down-heap) ( m heap -- ) - [ child ] 2keep swapd + [ drop ] [ child ] [ nip ] 2tri 3dup continue? [ 3drop ] [ - [ data-exchange ] 2keep down-heap + [ data-exchange ] [ down-heap ] 2bi ] if ; inline recursive : down-heap ( m heap -- ) @@ -140,14 +134,14 @@ DEFER: down-heap PRIVATE> M: heap heap-push* ( value key heap -- entry ) - [ dup ] keep [ data-push ] keep up-heap ; + [ dup ] [ data-push ] [ ] tri up-heap ; : heap-push ( value key heap -- ) heap-push* drop ; : heap-push-all ( assoc heap -- ) - [ swapd heap-push ] curry assoc-each ; + '[ swap _ heap-push ] assoc-each ; -: >entry< ( entry -- key value ) +: >entry< ( entry -- value key ) [ value>> ] [ key>> ] bi ; inline M: heap heap-peek ( heap -- value key ) @@ -163,29 +157,28 @@ M: bad-heap-delete summary index>> ; M: heap heap-delete ( entry heap -- ) - [ entry>index ] keep + [ entry>index ] [ ] bi 2dup heap-size 1 - = [ - nip data-pop* + nip data>> pop* ] [ - [ nip data-pop ] 2keep - [ data-set-nth ] 2keep + [ nip data>> pop ] + [ data-set-nth ] + [ ] 2tri down-heap ] if ; M: heap heap-pop* ( heap -- ) - dup data-first swap heap-delete ; + [ data-first ] keep heap-delete ; M: heap heap-pop ( heap -- value key ) - dup data-first [ swap heap-delete ] keep >entry< ; + [ data-first ] keep + [ heap-delete ] [ drop ] 2bi >entry< ; : heap-pop-all ( heap -- alist ) [ dup heap-empty? not ] [ dup heap-pop swap 2array ] produce nip ; -: heap-values ( heap -- alist ) - data>> [ value>> ] { } map-as ; - : slurp-heap ( heap quot: ( elt -- ) -- ) over heap-empty? [ 2drop ] [ [ [ heap-pop drop ] dip call ] [ slurp-heap ] 2bi From 1e783476433596c34a64adb9cda4c645cfa0a33a Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 10 Apr 2011 14:10:13 -0500 Subject: [PATCH 6/7] Print a newline after each TXT message in DNS. --- extra/dns/dns.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/dns/dns.factor b/extra/dns/dns.factor index 70b4275660..57795f49c2 100644 --- a/extra/dns/dns.factor +++ b/extra/dns/dns.factor @@ -351,7 +351,7 @@ M: TXT rdata>byte-array ] map ; : TXT. ( domain -- ) - dns-TXT-query TXT-message>strings [ [ write ] each ] each ; + dns-TXT-query TXT-message>strings [ [ write ] each nl ] each ; : reverse-lookup ( reversed-ip -- message ) PTR IN dns-query ; From 9b97f22b9212ee2b9f355bc0d45118c2adf3d326 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 10 Apr 2011 14:18:35 -0500 Subject: [PATCH 7/7] Make print-topic print a newline and adjust usages elsewhere. Reported by mrjbq7. --- basis/help/apropos/apropos.factor | 2 +- basis/help/help.factor | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/basis/help/apropos/apropos.factor b/basis/help/apropos/apropos.factor index 9377f0a656..0d40122088 100644 --- a/basis/help/apropos/apropos.factor +++ b/basis/help/apropos/apropos.factor @@ -71,4 +71,4 @@ M: apropos >link ; INSTANCE: apropos topic : apropos ( str -- ) - [ blank? ] trim print-topic nl ; + [ blank? ] trim print-topic ; diff --git a/basis/help/help.factor b/basis/help/help.factor index 27ce7a1435..501c5f01ea 100644 --- a/basis/help/help.factor +++ b/basis/help/help.factor @@ -127,11 +127,11 @@ M: word set-article-parent swap "help-parent" set-word-prop ; : print-topic ( topic -- ) >link last-element off - [ $title ] [ ($blank-line) article-content print-content ] bi ; + [ $title ] [ ($blank-line) article-content print-content nl ] bi ; SYMBOL: help-hook -help-hook [ [ print-topic nl ] ] initialize +help-hook [ [ print-topic ] ] initialize : help ( topic -- ) help-hook get call( topic -- ) ;