GTP utilities
filename/  c
nonnegative-real/  c
digit/  c
unique-listof/  c
confidence-interval
tab-split
tab-join
path-string->string
path-string->path
ensure-directory
rnd
pct
log2
file-remove-extension
save-pict
columnize
force/  cpu-time
time-string->values
time-string->cpu-time
time-string->real-time
time-string->gc-time
bitstring?
natural->bitstring
bitstring->natural
count-zero-bits
copy-file*
copy-racket-file*
copy-directory/  files*
enumerate
integer->digit*
digit*->integer
filename-sort
whitespace-string?
simple-comment-string?
timestamp
string->value
1 System Calls
shell
md5sum
7.7

GTP utilities

 (require gtp-util) package: gtp-util
General helper functions.

If you think one of these functions should "graduate" to another library or package, let me know at: github.com/bennn/gtp-util/issues

procedure

(filename/c x)  boolean?

  x : any/c
Flat contract for a path or string that path-only returns #false for.

Examples:
> (filename/c "segfault.mp3")

#t

> (filename/c (build-path "code.rkt"))

#t

> (filename/c (build-path "Desktop" "passwd.txt"))

#f

procedure

(nonnegative-real/c x)  boolean?

  x : any/c
Flat contract for non-negative real numbers.

Examples:

procedure

(digit/c x)  boolean?

  x : any/c
Flat contract for exact integers bewteen 0 and 9, inclusive.

Examples:
> (digit/c 0)

#t

> (digit/c 5)

#t

> (digit/c -1)

#f

> (digit/c 1.1)

#f

> (digit/c 10)

#f

procedure

(unique-listof/c c)  list-contract?

  c : contract?
Similar to listof, but rejects lists that contain two or more equal? elements.

Examples:
> ((unique-listof/c symbol?) '(u n i q))

#t

> ((unique-listof/c symbol?) '(r e p e a t))

#f

procedure

(confidence-interval r* 
  [#:cv confidence-value]) 
  (cons/c real? real?)
  r* : (listof real?)
  confidence-value : nonnegative-real/c = 1.96
Return a 95% confidence interval for the given numbers at the given confidence value.

procedure

(tab-split str)  (listof string?)

  str : string?
Split a string by tab characters.

procedure

(tab-join str*)  string?

  str* : (listof string?)
Join a list of strings by tab characters.

procedure

(path-string->string ps)  string?

  ps : path-string?
Convert a path or string to a string.

procedure

(path-string->path ps)  path?

  ps : path-string?
Convert a path or string to a path.

procedure

(ensure-directory ps)  void?

  ps : path-string?
If the given directory exists, do nothing. Otherwise, create it.

procedure

(rnd r)  string?

  r : real?
Round the given number to two decimal places. Returns the same result as (~r n #:precision '(= 2)) except that it can be typed faster.

procedure

(pct a b)  real?

  a : real?
  b : real?
Same as (* 100 (/ a b)).

procedure

(log2 n)  natural?

  n : natural?
Compute the base-2 logarithm of a number.

Assumes n is a power of 2.

procedure

(file-remove-extension ps)  path-string?

  ps : path-string?
Remove the extension from the given filename.

procedure

(save-pict out-path p)  boolean?

  out-path : path-string?
  p : pict?
Write the given pict to the given filename in .png format.

procedure

(columnize x* num-cols)  (listof list?)

  x* : list?
  num-cols : natural?
Divide a list into almost-equally-sized lists.

Examples:
> (columnize '(a b c d e f) 2)

'((a c e) (b d f))

> (columnize '(a b c d e) 2)

'((a c e) (b d))

procedure

(force/cpu-time thunk)  
any/c natural?
  thunk : (-> any/c)
Force the given thunk and record its running time. Return both the result of the thunk and the CPU time (as reported by time-apply).

Parse a string from the time macro into a sequence of integers.

Example:
> (time-string->values
    (with-output-to-string (λ () (time (sleep 1) (collect-garbage 'minor)))))

14

1014

14

Parse the corresponding field from a time-generated string.

Example:
> (let ([time-str (with-output-to-string (λ () (time (sleep 1) (collect-garbage 'minor))))])
    (values (time-string->cpu-time time-str)
            (time-string->real-time time-str)
            (time-string->gc-time time-str)))

42

1042

42

procedure

(bitstring? x)  boolean?

  x : any/c
Predicate for a string of #\1 and #\0 characters.

Examples:
> (bitstring? "0011")

#t

> (bitstring? 3)

#f

> (bitstring? " 1")

#f

procedure

(natural->bitstring n #:bits k)  string?

  n : natural?
  k : natural?
Return a binary representation of n using exactly k bits.

procedure

(bitstring->natural str)  natural?

  str : string?
Parse a string of "1" and "0" digits as a binary number, return the base-10 representation of the parsed number.

procedure

(count-zero-bits str)  natural?

  str : string?
Count the number of #\0 characters in a string of "1" and "0" digits.

procedure

(copy-file* src dst [pattern])  void?

  src : directory-exists?
  dst : directory-exists?
  pattern : string? = "*.*"
Copy every file from the directory src whose name matches the given glob pattern into the directory dst. Raises an exception if src contains a directory that matches the given pattern.

procedure

(copy-racket-file* src dst)  void?

  src : directory-exists?
  dst : directory-exists?
Same as (copy-file* src dst "*.rkt").

procedure

(copy-directory/files* src dst [pattern])  void?

  src : directory-exists?
  dst : directory-exists?
  pattern : string? = "*"
Copy every file and recursively copy every directory in src whose name matches the given pattern into the directory dst.

procedure

(enumerate x*)  (listof (cons/c natural? any/c))

  x* : (listof any/c)
Given a list of values '(A B C) return a list with each value indexed by its position '((0 . A) (1 . B) (2 . C)).

See also in-indexed and in-naturals.

procedure

(integer->digit* i)  (listof digit/c)

  i : exact-integer?
Explode an integer to a list of its digits.

Examples:
> (integer->digit* 8675309)

'(8 6 7 5 3 0 9)

> (integer->digit* 0)

'(0)

procedure

(digit*->integer d*)  exact-integer?

  d* : (listof digit/c)
Concatenate a sequence of digits into an integer.

Example:
> (digit*->integer '(9 1 1))

911

procedure

(filename-sort ps*)  (listof filename/c)

  ps* : (listof filename/c)
Sort a list of filenames.

Example:
> (filename-sort (list (build-path "foo.md") (build-path "bar.rkt")))

'(#<path:bar.rkt> #<path:foo.md>)

procedure

(whitespace-string? str)  boolean?

  str : string?
Predicate for strings that contain only whitespace characters.

Examples:
> (whitespace-string? "  \n  \t")

#t

> (whitespace-string? "X")

#f

procedure

(simple-comment-string? str)  boolean?

  str : string?
Predicate for a string that begins with a semicolon.

Examples:
> (simple-comment-string? "     ;")

#t

> (simple-comment-string? "  #;(a b c)")

#f

> (simple-comment-string? "  #|")

#f

procedure

(timestamp [time?])  string?

  time? : any/c = #true
The same as calling (date->string (current-date) time?) with date-display-format set to 'iso-8601.

Examples:
> (timestamp)

"2020-05-03T01:19:04"

> (timestamp #false)

"2020-05-03"

procedure

(string->value str)  any/c

  str : string?
Use read to parse a value from a string.

Examples:
> (string->value "#true")

#t

> (string->value "#(A B (C))")

'#(A B (C))

1 System Calls

Convenience API for making system calls.

See also racket/system.

procedure

(shell cmd arg*)  string?

  cmd : path-string?
  arg* : (or/c path-string? (listof path-string?))
Finds the executable that cmd denotes, then invokes it with the given arguments. Returns a string containing all output from the system call. Raises an exn:fail:user? exception if the executable exits uncleanly.

procedure

(md5sum filename)  string?

  filename : path-string?
Same as (call-with-input-file filename md5).