Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Mapping by N elements
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
WJ  
View profile  
 More options Oct 31 2012, 3:28 am
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 31 Oct 2012 07:27:49 GMT
Local: Wed, Oct 31 2012 3:27 am
Subject: Re: Mapping by N elements

Samir Barjoud wrote:
> Is there any function or macro defined by Common Lisp that does the
> following?

> Example usage:
> ............
> (map-by (x y) '(1 2 3 4 5 6)
>         (+ x y))

> => (3 7 11)

> (map-by (x y z) '(1 2 3 4 5 6)
>         (* x y z))

> => (6 120)

Racket:

## func must return 2 values.
(define (map-by func lst)
  (let loop ((lst lst) (result '()))
    (if (null? lst)
      (reverse result)
      (begin
        (let-values ([(val new-list) (func lst)])
          (loop new-list (cons val result)))))))

(map-by ($(lst) (values (apply + lst) (rest lst))) (range 8))
--> '(28 28 27 25 22 18 13 7)

(map-by ($$(x y tl ...) (values (+ x y) tl)) (range 8))
--> '(1 5 9 13)

;; Group consecutive numbers or non-numbers.
(map-by (match-lambda
          [(list (? number? n) ..1 tl ...) (values n tl)]
          [(list (? (negate number?) x) ..1 tl ...) (values x tl)])
        '(a b c 2 3 4 x y 5 z))
--> '((a b c) (2 3 4) (x y) (5) (z))

Given:

(define-syntax-rule ($ expr ...) (lambda expr ...))
(define-syntax-rule ($$ (pat ...) expr ...)
  (match-lambda [(list pat ...) expr ...]))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
danielrupistraliza...@yahoo.es  
View profile  
 More options Oct 31 2012, 2:12 pm
Newsgroups: comp.lang.lisp
From: danielrupistraliza...@yahoo.es
Date: Wed, 31 Oct 2012 11:12:48 -0700 (PDT)
Local: Wed, Oct 31 2012 2:12 pm
Subject: Re: Mapping by N elements
El miércoles, 31 de octubre de 2012 08:28:27 UTC+1, WJ  escribió:

Better in common-lisp

(defmacro max-by (args lst &body body)
    (let ((n (length args)))
      `(loop for l = ,lst then (nthcdr ,n l) while l  collect (apply (lambda ,args ,@body) (subseq l 0 ,n)))))

(max-by (x y) '(1 2 3 4 5 6) (+ x y))
(3 7 11)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
danielrupistraliza...@yahoo.es  
View profile  
 More options Oct 31 2012, 4:08 pm
Newsgroups: comp.lang.lisp
From: danielrupistraliza...@yahoo.es
Date: Wed, 31 Oct 2012 13:08:41 -0700 (PDT)
Local: Wed, Oct 31 2012 4:08 pm
Subject: Re: Mapping by N elements
a

El miércoles, 31 de octubre de 2012 19:12:49 UTC+1, danielrupi...@yahoo.es  escribió:

A better solution

(defmacro max-by (args lst &body body)
  `(loop for ,args on ,lst by (lambda(x)(nthcdr ,(length args) x)) collect
        (progn ,@body)))

(max-by (x y) '(1 2 3 4 5 6) (+ x y))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »