]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/seq.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / emacs-lisp / seq.el
index 456efd077dbdaf56ae359d1d2617ef39119badba..92f0ad78566c78cfa46f38cd77b8c09f6bdb6ae3 100644 (file)
@@ -1,10 +1,10 @@
 ;;; seq.el --- Sequence manipulation functions  -*- lexical-binding: t -*-
 
-;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
+;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
 
 ;; Author: Nicolas Petton <nicolas@petton.fr>
 ;; Keywords: sequences
-;; Version: 2.3
+;; Version: 2.14
 ;; Package: seq
 
 ;; Maintainer: emacs-devel@gnu.org
@@ -57,7 +57,7 @@
 ;;; Code:
 
 (eval-when-compile (require 'cl-generic))
-(require 'cl-extra) ;; for cl-subseq
+(require 'cl-lib) ;; for cl-subseq
 
 (defmacro seq-doseq (spec &rest body)
   "Loop over a sequence.
@@ -144,6 +144,18 @@ if positive or too small if negative)."
             sequence)
     (nreverse result)))
 
+(defun seq-map-indexed (function sequence)
+  "Return the result of applying FUNCTION to each element of SEQUENCE.
+Unlike `seq-map', FUNCTION takes two arguments: the element of
+the sequence, and its index within the sequence."
+  (let ((index 0))
+    (seq-map (lambda (elt)
+               (prog1
+                   (funcall function elt index)
+                 (setq index (1+ index))))
+             sequence)))
+
+
 ;; faster implementation for sequences (sequencep)
 (cl-defmethod seq-map (function (sequence sequence))
   (mapcar function sequence))
@@ -206,6 +218,16 @@ The result is a sequence of the same type as SEQUENCE."
 (cl-defmethod seq-sort (pred (list list))
   (sort (seq-copy list) pred))
 
+(defun seq-sort-by (function pred sequence)
+  "Sort SEQUENCE using PRED as a comparison function.
+Elements of SEQUENCE are transformed by FUNCTION before being
+sorted.  FUNCTION must be a function of one argument."
+  (seq-sort (lambda (a b)
+              (funcall pred
+                       (funcall function a)
+                       (funcall function b)))
+            sequence))
+
 (cl-defgeneric seq-reverse (sequence)
   "Return a sequence with elements of SEQUENCE in reverse order."
   (let ((result '()))