On this page:
+ +
--
box-me
identity
symetric
to-proc
get-type
5.1 Lists
transpose
list-choose
first-value
mean
list-set
list->lines
5.2 Functions and Applications
argbest
map/ apply
for-each/ apply
ntimes
times
5.3 Vectors
with-vector
vector-clone
5.4 Strings
to-string
protect-string
trim
string-reverse
string->lines
regexp-matcher
comment-section
comment-chapter
proc->string
proc->symbol
5.5 Files and Directories
directory-list-rec
filter-file-list
file->lines/ latin-1
file->name-ext
path->quote-string
5.6 Classes and Objects
map/ send
for-each/ send
Version: 4.2.2

5 Common Scheme Utilities

 (require (planet orseau/lazy-doc:1:5/common))

This module provides some useful functions and forms that are common to most of the files of this package and other future packages.
(++ var)
Increments var.

(-- var)
Decrements var.

(box-me b val)  (one-of/c val)
  b : box?
  val : any/c
Puts val in the box b and returns val. Useful to return a value and box it as a side effect.

(identity x r ...)  (one-of/c x)
  x : any/c
  r : any/c
Returns only the first of the parameters.

(symetric fxy)  procedure?
  fxy : procedure?
Returns the symetric function of fxy.

(to-proc x)  procedure?
  x : any/c
Returns a procedure that accepts any number of arguments and returns x. If x was already a procedure, returns x without change.

(get-type x)  (listof procedure?)
  x : any/c
Returns (some of) the types that x matches.

Examples:

  > (get-type '(a b c))

  ("list?")

  > (get-type 101)

  ("number?")

  > (get-type get-type)

  ("procedure?")

5.1 Lists

(transpose ll)  (listof list?)
  ll : (listof list?)
Transposes a list of lists.

Example:

  > (transpose '((a b c) (0 1 2)))

  ((a 0) (b 1) (c 2))

(list-choose l)  any
  l : list?
Chooses one element from l.

Examples:

  > (list-choose '(a b c d e f g h i j))

  c

  > (list-choose '(a b c d e f g h i j))

  f

  > (list-choose '(a b c d e f g h i j))

  j

(first-value l ...)  any
  l : list?
Returns the first value of l that is not #f, or returns #f if none is found. Useful with functions that return a value or #f (like get-env). A default value can be set as the last element.

Examples:

  > (first-value #f #f "plop" #f 'moustard)

  "plop"

  > (first-value #f #f #f)

  #f

(mean n ...)  number?
  n : number?
Returns the average value of the n.

(list-set l n v)  list?
  l : list?
  n : number?
  v : any/c
Returns the same list l where element at position n is replaced by v.

Example:

  > (list-set '(a b c d e) 3 'huh?)

  (a b c huh? e)

(list->lines l sep)  (listof? list?)
  l : list?
  sep : any/c
Splits a list into "lines" (list of lists).

Example:

  > (list->lines '(a b c x d e x x f g h x o) 'x)

  ((a b c) (d e) () (f g h) (o))

5.2 Functions and Applications

(argbest proc lst)  any
  proc : procedure?
  lst : list?
Returns the best element of lst. Each challenger is compared to the best value with proc. If proc returns #t, the best wins.

Examples:

  > (argbest < '(5 2 5 7 8 1 5))

  1

  > (argbest > '(5 2 5 7 8 1 5))

  8

  > (argbest (λ(best chall)
               (and (<= (first best) (first chall))
                    (< (second best) (second chall))))
             '((3 6)(7 2)(8 3)(3 5)(3 7)))

  (3 5)

(map/apply proc ll ...)  list?
  proc : procedure?
  ll : (listof list?)
Applies proc to each list element of ll.

(for-each/apply proc ll ...)  void?
  proc : procedure?
  ll : (listof list?)
Like map/apply but with for-each.

(ntimes n proc)  void?
  n : number?
  proc : procedure?
Does proc n times.

(times n val-max body ...)
Binds n to the values from 0 to val-max while doing body ...

5.3 Vectors

(with-vector lst-id body ...)
Temporarily turns lst-id into a vector, does body ... then turns it back to a list

(vector-clone v)  any
  v : any/c

5.4 Strings

(to-string x)  string?
  x : any/c
Turns any value into a string.

(protect-string x)  string?
  x : any/c
Turns any value into a string. If x is already a string, quotes its quotes.

Example:

  > (protect-string "the string \"plop\" is a string.")

  "\"the string \\\"plop\\\" is a string.\""

(trim s [left right])  string?
  s : string?
  left : number? = 0
  right : number? = left
Removes left and right characters from s.

Example:

  > (trim "abcdefghij" 3 1)

  "defghi"

(string-reverse str)  string?
  str : string?
Reverses str.

Example:

  > (string-reverse "emordnilap a ton ma I")

  "I am not a palindrome"

(string->lines str [sep])  (listof string?)
  str : string?
  sep : string? = "\n"
Splits str at sep.

Example:

  > (string->lines "One\nTwo Three\nFour")

  ("One" "Two Three" "Four")

(regexp-matcher re)  procedure?
  re : (or/c string? pregexp? regpexp?)
Returns a procedure that matches re.

Example:

  > (map (regexp-matcher "pl(.)p") '("aplipa" "youp" "coplop"))

  (("plip" "i") #f ("plop" "o"))

(comment-section text [#:pre pre #:post post])  void?
  text : string?
  pre : string? = ";;;   "
  post : string? = (string-reverse pre)
Displays a comment string that can be copied into the source file. The width of the result depends on the with of text.

Example:

  > (comment-section "And now for something completely different")

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ;;;   And now for something completely different   ;;;

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(comment-chapter text [pre post #:width width])  void?
  text : string?
  pre : string? = "   "
  post : string? = (string-reverse pre)
  width : number? = 80
Similar to comment-section but the width of the result does not depend on the width of text.

Example:

  > (comment-chapter "The Show Sets Sales"
      #:width 60)

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ;;;;;;;;;;;;;;;;;   The Show Sets Sales   ;;;;;;;;;;;;;;;;;;

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(proc->string proc)  string?
  proc : procedure?

Example:

  > (proc->string proc->symbol)

  "proc->symbol"

(proc->symbol proc)  symbol?
  proc : procedure?

Example:

  > (proc->symbol proc->symbol)

  proc->symbol

5.5 Files and Directories

(directory-list-rec [path])  (listof (or/c string? path?))
  path : path-string? = (current-directory)
Returns the list of files and directory contained in path, recursively including sub-directories. The files are returned with their full path.

(filter-file-list re [files])  (listof (or/c string? path?))
  re : (or/c string? pregexp? regpexp?)
  files : (listof (or/c string? path?)) = (directory-list)
Filters the list of files files with the regexp re.

Example:

  > (filter-file-list "parser\\.ss$" (directory-list))

  (#<path:defs-parser.ss> #<path:simple-parser.ss>)

(file->lines/latin-1 file)  (listof string?)
  file : path-string?

(file->name-ext file)  any
  file : path-string?
Returns two values: the name part of the file and the extension part, without the dot.

Example:

  > (file->name-ext "common.scm.ss")

  "common.scm"

  "ss"

(path->quote-string path)  string?
  path : path-string?
Returns the path as a string, and surround it with double-quotes if it contains spaces.

Examples:

  > (path->quote-string "C:\\Program Files\\PLT")

  "\"C:\\Program Files\\PLT\""

  > (path->quote-string "~/scheme/cabbages")

  "~/scheme/cabbages"

5.6 Classes and Objects

(map/send message obj ...)
 
message = method
  | (method arg ...)
Sends method along with its arguments to each object and returns the list of results.

For example
  (map/send my-method (list obj1 obj2 obj3))
is equivalent to
  (map (λ(x)(send x my-method)) (list obj1 obj2 obj3))
and
  (map/send (my-method 3 5) (list obj1 obj2 obj3))
is equivalent to
  (map (λ(x)(send x my-method 3 5)) (list obj1 obj2 obj3))
(for-each/send method obj ...)
Like map/send but returns (void).