マップ関数(mapping function)は、
リストや他の集まりの各要素に指定した関数を適用します。
Emacs Lispにはそのような関数がいくつかあります。
mapcar
とmapconcat
はリストを走査するもので、ここで説明します。
オブジェクト配列obarray内のシンボルについて
マップする関数mapatoms
については、
See Creating Symbols。
これらのマップ関数では、文字テーブルは扱えません。
というのは、文字テーブルは疎な配列であり、その添字範囲も非常に大きいからです。
文字テーブルの疎な性質を考慮して文字テーブルについてマップするには、
関数map-char-table
(see Char-Tables)を使います。
mapcar
は、sequenceの各要素に順にfunctionを適用し、 結果のリストを返す。引数sequenceは文字テーブル以外の任意の種類のシーケンスでよい。 つまり、リスト、ベクトル、ブールベクトル、あるいは、文字列である。 結果はつねにリストである。 結果の長さはsequenceの長さと同じである。
たとえば、つぎのとおり。
(mapcar 'car '((a b) (c d) (e f))) ⇒ (a c e) (mapcar '1+ [1 2 3]) ⇒ (2 3 4) (mapcar 'char-to-string "abc") ⇒ ("a" "b" "c") ;;my-hooks
の各関数を呼び出す (mapcar 'funcall my-hooks) (defun mapcar* (function &rest args) "Apply FUNCTION to successive cars of all ARGS. Return the list of results." ;; リストをつくしていなければ (if (not (memq 'nil args)) ;; carに関数を適用する (cons (apply function (mapcar 'car args)) (apply 'mapcar* function ;; Recurse for rest of elements. (mapcar 'cdr args))))) (mapcar* 'cons '(a b c) '(1 2 3 4)) ⇒ ((a . 1) (b . 2) (c . 3))
mapconcat
は、sequenceの各要素にfunctionを適用する。 それらの結果は、文字列である必要があり、連結される。mapconcat
は、結果の文字列のあいだに文字列separatorを挿入する。 普通、separatorは、空白やコンマ、その他の句読点を含む。引数functionは、引数を1つ取る関数であり、 文字列を返す必要がある。 引数sequenceは、文字テーブル以外の任意の種類のシーケンスでよい。 つまり、リスト、ベクトル、ブールベクトル、あるいは、文字列である。
(mapconcat 'symbol-name '(The cat in the hat) " ") ⇒ "The cat in the hat" (mapconcat (function (lambda (x) (format "%c" (1+ x)))) "HAL-8000" "") ⇒ "IBM.9111"