[m-rev.] for review: add string.format_table/2

Ralph Becket rafe at cs.mu.OZ.AU
Fri Feb 4 14:27:38 AEDT 2005


Ian MacLarty, Thursday,  3 February 2005:
> For review by anyone.
> 
> Estimated hours taken: 3
> Branches: main
> 
> Add a function to the string module to generate a formatted text table.
> +string__format_table(Columns, Seperator) = Table :-
> +	MaxWidths = list.map(find_max_length, Columns),
> +	PaddedColumns = list.map_corresponding(pad_column, MaxWidths, Columns),
> +	(
> +		PaddedColumns = [PaddedHead | PaddedTail],
> +		Rows = list.foldl(list.map_corresponding(
> +			string.join_rev_columns(
> +				Seperator)), PaddedTail, PaddedHead)
> +	;
> +		PaddedColumns = [],
> +		Rows = []
> +	),
> +	Table = string.join_list("\n", Rows).
> +
> +:- func join_rev_columns(string, string, string) = string.
> +
> +join_rev_columns(Seperator, Col1, Col2) = Col2 ++ Seperator ++ Col1.
> +
> +:- func find_max_length(justified_column) = int.
> +
> +find_max_length(left(Strings)) = MaxWidth :-
> +	list.foldl(max_str_length, Strings, {0, ""}, {MaxWidth, _}).
> +find_max_length(right(Strings)) = MaxWidth :-
> +	list.foldl(max_str_length, Strings, {0, ""}, {MaxWidth, _}).

It would be better to use list.foldl2 and make max_str_length a
predicate, rather than use tuples.

> +max_str_length(Str, {PrevMaxLen, PrevMaxStr}, {MaxLen, MaxStr}) :-
> +	Length = string.length(Str),
> +	(
> +		Length > PrevMaxLen
> +	->
> +		MaxLen = Length,
> +		MaxStr = Str
> +	;
> +		MaxLen = PrevMaxLen,
> +		MaxStr = PrevMaxStr
> +	).

Otherwise, looks good.

-- Ralph
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list