From mboxrd@z Thu Jan 1 00:00:00 1970 From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Subject: Re: New =?utf-8?Q?=E2=80=98--list-generations=E2=80=99?= and =?utf-8?Q?=E2=80=98--delete-generations=E2=80=99?= options Date: Wed, 11 Sep 2013 23:25:27 +0200 Message-ID: <87li336ofs.fsf@gnu.org> References: <87vc2o4qwc.fsf@gnu.org> <87y57kljro.fsf@karetnikov.org> <87hae81uvo.fsf@gnu.org> <87bo4fcbcz.fsf@karetnikov.org> <878uzj6nev.fsf@gnu.org> <877gf1yftq.fsf@karetnikov.org> <87bo4dspl2.fsf@gnu.org> <87a9jxeh05.fsf@gnu.org> <87r4d9r2lv.fsf@gnu.org> <874na4jfp4.fsf_-_@karetnikov.org> <87eh97616m.fsf@gnu.org> <87bo48xdgb.fsf@karetnikov.org> <87hadz9gze.fsf@gnu.org> <87fvtjdl7y.fsf@karetnikov.org> <87bo444e9q.fsf@gnu.org> <87fvtfzihg.fsf@karetnikov.org> <87ioybxdun.fsf@gnu.org> <877geq9wx6.fsf@karetnikov.org> <87zjrmgcjh.fsf@gnu.org> <87ob80os3c.fsf@karetnikov.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44475) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJrzh-00009j-NU for guix-devel@gnu.org; Wed, 11 Sep 2013 17:30:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VJrzb-0003aX-9M for guix-devel@gnu.org; Wed, 11 Sep 2013 17:30:37 -0400 Received: from hera.aquilenet.fr ([141.255.128.1]:50400) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJrza-0003aH-TA for guix-devel@gnu.org; Wed, 11 Sep 2013 17:30:31 -0400 In-Reply-To: <87ob80os3c.fsf@karetnikov.org> (Nikita Karetnikov's message of "Wed, 11 Sep 2013 09:16:55 +0400") List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org To: Nikita Karetnikov Cc: guix-devel@gnu.org Nikita Karetnikov skribis: >>> How can I subtract 22 days from (current-time) using SRFI-19? > >> Note that the above example suggests that =E2=80=98string->duration=E2= =80=99 returns a >> time object with of type =E2=80=98time-duration=E2=80=99 (thus independe= nt of the >> current time.) > > Ah, OK. But we=E2=80=99ll have to subtract from (current-time) later any= way, > right? Why do you want to return a =E2=80=98time-duration=E2=80=99 objec= t? To separate concerns, and to simplify testing. > So, here=E2=80=99s the parsing phase. WDYT? Overall looks good to me, but I think it can be simplified: > (define (string->generations str) > (define (maybe-integer) > (let ((x (string->number str))) > (and (integer? x) > (list x)))) > > (define (maybe-comma-separated-integers) > (let ((lst (delete-duplicates > (map string->number > (delete "" (string-split str #\,)))))) > (and (every integer? lst) > lst))) > (define (safe-match:substring->number match n) > (false-if-exception (string->number (match:substring match n)))) > > (define (maybe-whole-range) > (let* ((rx (make-regexp "^([0-9]+)\\.\\.([0-9]+)$")) > (res (regexp-exec rx str)) > (x (safe-match:substring->number res 1)) > (y (safe-match:substring->number res 2))) By definition submatches 1 and 2 exist when RES is true. Thus, I=E2=80=99d remove =E2=80=98safe-match:substring->number=E2=80=99 and= do: (match (string-match "^([0-9]+)\\.\\.([0-9]+)$" str) (#f #f) (matches (let ((start (number->string (match:substring matches 1))) (end (number->string (match:substring matches 2)))) ...))) > (or (maybe-integer) (maybe-comma-separated-integers) > (maybe-whole-range) (maybe-start-range) (maybe-end-range))) Probably this can reduce to a big =E2=80=98cond=E2=80=99, which would be ev= en more readable: (cond ((maybe-integer) =3D> list) ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str) =3D> (lambda (match) ...)) ...) > (define (string->duration str) > (define (maybe-duration hours pattern) > (let ((res (regexp-exec (make-regexp pattern) str))) > (false-if-exception > (make-time time-duration 0 > (* 3600 hours (string->number (match:substring res 1)))= )))) > > (define (days) > (maybe-duration 24 "^([0-9]+)d$")) > > (define (weeks) > (maybe-duration (* 24 7) "^([0-9]+)w$")) > > (define (months) > (maybe-duration (* 24 30) "^([0-9]+)m$")) > > (or (days) (weeks) (months))) Likewise, just: (define (hours->duration hours) (make-time ...)) (cond ((string-match "^([0-9]+)d$" str) =3D> ...) ...) Thanks, Ludo=E2=80=99.