about summary refs log tree commit diff
path: root/lisp/klatre/klatre.lisp
blob: b20d1ab5282a7fc5d6b46a31c4ef661b59b9a212 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(in-package #:klatre)
(declaim (optimize (safety 3)))

(defmacro comment (&rest _))

(defun posp (n) (> n 0))

;;; Sequence utilities

(defun slice (vector start end)
  (make-array (- end start)
              :element-type (array-element-type vector)
              :displaced-to vector
              :displaced-index-offset start))

(defun chunk-vector (size vector &key start end sharedp)
  (check-type size (integer 1))
  (loop
     with slicer = (if sharedp #'slice #'subseq)
     and low = (or start 0)
     and high = (or end (length vector))
     for s from low below high by size
     for e from (+ low size) by size
     collect (funcall slicer vector s (min e high))))

(defun chunk-list/unbounded (size list)
  (loop
     for front = list then next
     for next = (nthcdr size front)
     collect (ldiff front next)
     while next))

(defun chunk-list/bounded (size list upper-limit)
  (loop
     for front = list then next
     for next = (nthcdr (min size upper-limit) front)
     collect (ldiff front next)
     do (decf upper-limit size)
     while (and next (plusp upper-limit))))

(defun chunk-list (size list &key (start 0) end)
  "Returns successive chunks of list of size SIZE, starting at START and ending
at END."
  (declare (inline check-list/bounded check-list/simple))
  (check-type size (integer 1))
  (let ((list (nthcdr start list)))
    (when list
      (if end
          (chunk-list/bounded size list (- end start))
          (chunk-list/unbounded size list)))))

(defun mapconcat (func lst sep)
  "Apply FUNC to each element of LST, and concat the results as strings,
separated by SEP."
  (check-type lst cons)
  (check-type sep (simple-array character (*)))
  (let ((vs (make-array 0
                        :element-type 'character
                        :fill-pointer 0
                        :adjustable t))
        (lsep (length sep)))
    (mapcar #'(lambda (str)
                (let ((nstr (the (simple-array character (*))
                                 (funcall func str))))
                  (dotimes (j (length nstr) j)
                    (vector-push-extend (char nstr (the fixnum j)) vs))
                  (dotimes (k lsep k)
                    (vector-push-extend (char sep (the fixnum k)) vs))))
                lst)
    vs))

;;;
;;; String handling
;;;

(defparameter dottime-format
  '((:year 4) #\- (:month 2) #\- (:day 2)
    #\T
    (:hour 2) #\· (:min 2) "+00") ; TODO(grfn): Allow passing offset
  "`:LOCAL-TIME' format specifier for dottime")

(defun format-dottime (timestamp)
  "Return TIMESTAMP formatted as dottime, using a +00 offset"
  (check-type timestamp local-time:timestamp)
  (local-time:format-timestring nil timestamp
                                :format dottime-format
                                :timezone local-time:+utc-zone+))

(comment
 (format-dottime (local-time:now)))