Hi,

I've just posted a thoroughly reworked version of format.r to rebol.org.
Please check it out.

    http://www.rebol.org/utility/format.r

The information for the updated version doesn't appear on the index pages,
but the file itself has been replaced.

Larry Palmiter and Gerald Goertzel gave me enormous help in reworking
these functions into a more flexible and useful form.


The main reason for writing FORMAT (the main function in format.r) was the
lack of any control over how numbers are formed in REBOL. There are at least
three problems here that format.r solves:

1. You can't choose how many decimal points of precision to use.

>> form pi
== "3.14159265358979"

>> format pi %.4
== "3.1416"
>> format pi %.6
== "3.141593"

2. You can't choose between scientific and fixed point formats:

>> form pi / 100
== "3.14159265358979E-2"
>> form pi * 1e12
== "3141592653589.79"

>> format pi / 100 %.6
== "0.031416"
>> format pi * 1e12 %.4.1
== "3.1416E+012"

3. Often the formed value cannot be converted back to the original value:

>> x: 1 / 3
== 0.333333333333333
>> x - to decimal! form x
== 3.33066907387547E-16

>> full-form x                   ; FULL-FORM also on format.r
== "3.333333333333333E-1"
>> x - to decimal! full-form x
== 0


There are other useful functions in format.r which can be used to
automatically display blocks in aligned columns. QT is used for simple
blocks:

>> arr: copy [] repeat x 25 [append arr log-e x]
== [0 0.693147180559945 1.09861228866811 1.38629436111989 ...
>> qt arr
0.0000 0.6931 1.0986 1.3863 1.6094 1.7918 1.9459 2.0794 2.1972 2.3026 2.3979
2.4849 2.5649 2.6391 2.7081 2.7726 2.8332 2.8904 2.9444 2.9957 3.0445 3.0910
3.1355 3.1781 3.2189

and QC for nested blocks, where each block contains a number of data fields:

>> arr: compose/deep [
[    ["pi" (pi)]
[    ["pi * pi" (pi * pi)]
[    ["exp pi" (exp pi)]
[    ["log-e pi" (log-e pi)]]
== [["pi" 3.14159265358979] ["pi * pi" 9.86960440108936] ["exp pi" ...
>> qc arr
pi        3.14
pi * pi   9.87
exp pi   23.14
log-e pi  1.14


There's a whole lot more you can do with format.r. There are about 40
usage examples commented into the script to get you started.

Please send me any bugs you find, comments or suggestions! I'd be happy
to answer any questions about how to use the functions in format.r to
get the results you want.


Have fun,
Eric

===========

Here's a directory display function based on one by Jos'h Fuller
that makes good use of QC:

show-dir: func [
    "List the specified directory. Inspired by Jos'h Fuller's DIR"
    where [ file! ]          "Directory path."
    /by 'sort-by [ word! ]   {Options are 'name (default) 'date 'size}
    /desc                    "Sort in descending order"
    /label                   "Show column labels."
    /local file files comp
][
    sort-by: select reduce [none 1 'name 1 'date 2 'size 3] sort-by
    comp: either desc [:greater?][:lesser?]
    files: copy []
    foreach file read where [
        append/only files reduce [file   modified? file   size? file]
    ]
    sort/compare files func [a b][
        either a/:sort-by = b/:sort-by
            [comp a/1 b/1][comp a/:sort-by b/:sort-by]
    ]
    if label [insert/only files ["NAME" "DATE/TIME" "SIZE"]]
    qc files %40.0.2.1
]

Reply via email to