On this page:
empty$?
number-or-string->string
integer->string-with-ordinal
list-of-strings?
list->list-of-strings
range-expand
range-expand-to-strings
ceiling-pow10
concatenate-list-of-vectors
transpose
list-of-lists?
reduce-across-lists
vector-reverse
8.0

8 Utilities

Utility Functions

 (require "../bc-utilities.rkt") package: base

procedure

(empty$? s)  boolean?

  s : any/c
"#t" if "s" is a string of zero length.

Returns "#f" if "s" is null, or "s" is not a string, or "s" has length >= 1.

procedure

(number-or-string->string x 
  [#:precision precision 
  #:width width]) 
  (or/c boolean? string?)
  x : any/c
  precision : nonnegative-integer? = 2
  width : nonnegative-integer? = 1
if x is a number, converts to a string with given precision and width.

if x is a string representation of a number, then x is converted back to a number and then to a new string representation with given precision and width.

if x is already a string, immutable copy is returned.

otherwise, x is converted to string by "(format \"~a\" x)" call.

procedure

(integer->string-with-ordinal n)  string?

  n : integer?
If the tens digit of a number is 1, then "th" is written after the number. For example: 13th, 19th, 112th,311th.

If the tens digit is not equal to 1, then: If the units digit is 1, write "st". If the units digit is 2, write "nd" if the units digit is 3, write "rd" all other digits (0, 4-9), write "th"

procedure

(list-of-strings? ls)  boolean?

  ls : list?
Returns "#t" if all member of "ls" are strings.

Returns "#f" for empty list.

procedure

(list->list-of-strings ls 
  [#:precision precision 
  #:width width]) 
  list-of-strings?
  ls : list?
  precision : nonnegative-integer? = 2
  width : nonnegative-integer? = 1
given a list, returns a new list in which all elements have been converted to strings, using number-or-string->string function.

procedure

(range-expand s)  list?

  s : string?
Takes string representation of number ranges in string s, and expands to the full list of numbers. Subranges in the range string do not have tbe consecutive (or contiguous). Negative numbers also allowed. spaces are removed from s before expansion.

Examples:
(range-expand  "1,2,3,4-10") -> '(1 2 3 4 5 6 7 8 9 10)
 
(range-expand  "15,16,20-24,29") -> '(15 16 20 21 22 23 24 29)
 
(range-expand "-6,-3--1,3-5,7-11,14,15,17-20") -> '(-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)
 
(range-expand "5-7, 2-4") -> '(2 3 4 5 6 7)

Based on https://rosettacode.org/wiki/Range_expansion#Racket .

procedure

(range-expand-to-strings s)  list-of-strings?

  s : string?
Same as range-expand, but returns expanded range number as a list of strings.

procedure

(ceiling-pow10 x)  number?

  x : number?
round x up to nearest integer multiple of a power of 10; integer powers of 10 are not changed. Useful for making pretty maximums for graph axes.

eg
(ceiling-pow10 7) -> 10
(ceiling-pow10 10) -> 10
(ceiling-pow10 77) -> 80
(ceiling-pow10 101) -> 200
(ceiling-pow10 1000) -> 1000
(ceiling-pow10 1010) -> 2000

procedure

(concatenate-list-of-vectors vectors)  vector

  vectors : list?
make a list of vectors into a single vector. just concatenates; doesn’t attempt to merge etc.

procedure

(transpose #:rows rows)  list-of-lists?

  rows : list-of-lists?
transpose rows: given a list[1-j] of lists[j][1-k], transpose to give a list[1-k] of lists[k][1-j]

Example:
(transpose #:rows '( ("alpha" "beta" "gamma") (1 1 1)  (2 3 4) (3 5 7)))
-> '(("alpha" 1 2 3) ("beta" 1 3 5) ("gamma" 1 4 7))

procedure

(list-of-lists? lol)  boolean?

  lol : any/c
Returns #t if a lol is a list of lists. Sublists can be emptylists. Returns #f if lol is empty, or if lol containns a member that is not itself a list.

Examples:
(list-of-lists? '('()) ) -> #f
(list-of-lists? '(1 2 3) ) -> #f
(list-of-lists? '('() '() '()) ) -> #t
(list-of-lists? '('(1 2 3) 5 '(3 6 9)) ) -> #f
(list-of-lists? '('(1 2 3) '(4 6 8) '(3 6 9)) ) -> #t

procedure

(reduce-across-lists #:function list-reducer    
  #:list-of-lists lol)  list?
  list-reducer : procedure?
  lol : list-of-lists?
Example:
(reduce-across-lists #:function (lambda(ls)(foldl + 0 ls))
                     #:list-of-lists '( (1 2 3) (1 4 9) (1 8 27)))
-> '(3 14 39)

procedure

(vector-reverse vec)  vector?

  vec : vector?
new vector with order of elements reserved