[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Collection utilities

William James

6/8/2015 11:47:00 AM

G. Garza wrote:

> > * (with-collectors (foo)
> > (loop for pos upfrom 1
> > for l in '((a b c) (one two three) (you and me) (girl))
> > do (collect pos :into foo)
> > do (loop for sym in l
> > do (collect pos :into foo))
> > finally (return foo)))
> > (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)
>
> ;;; Or....
>
> * (loop for pos upfrom 1
> for l in '((a b c) (one two three) (you and me) (girl))
> collect pos
> append l)
> (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)

Gauche Scheme:

(concatenate
(map
cons
(lrange 1)
'((a b c) (one two three) (you and me) (girl))))

===>
(1 a b c 2 one two three 3 you and me 4 girl)


Another way:

(use srfi-42 :only (append-ec))

(append-ec (:list x (index i) '((a b c) (one two three) (you and me) (girl)))
(cons i x))
===>
(0 a b c 1 one two three 2 you and me 3 girl)


Another way:

(gathering ((b bagger) (i stepper 1))
(dolist (x '((a b c) (one two three) (you and me) (girl)))
(b (i))
(b x 'append)))

===>
(1 a b c 2 one two three 3 you and me 4 girl)
5


Given:

(define stepper
(case-lambda
((start step)
(let ((i (- start step)))
(lambda () (set! i (+ step i)) i)))
((start) (stepper start 1))
(() (stepper 0 1))))

(define (bagger)
(let ((bag '()))
(case-lambda
(() (reverse bag))
((x) (push! bag x))
((x what)
(case what
((append) (set! bag (append (reverse x) bag)) x)
((front cons) (set! bag (append bag (list x))) x)
((new) (if (member x bag)
#f
(begin (set! bag (cons x bag)) x)))
((sum) (set! bag (cons x bag)) (apply + bag))
(else (error "Bad args. to bagger:" x what)))))))

(define-syntax gathering
(syntax-rules ()
([_ ((name maker option ...) ...) body ...]
(let ( (name (maker option ...)) ... )
body ...
(values (name) ...)))))

--
In Sweden, the leadership seems to show Europe's most extensive cultural
self-denial, or rather -- a deeply felt self-loathing, which many feel is the
perfect recipe for driving a peaceful nation to destruction.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
1 Answer

William James

1/16/2016 8:47:00 AM

0

WJ wrote:

> G. Garza wrote:
>
> > > * (with-collectors (foo)
> > > (loop for pos upfrom 1
> > > for l in '((a b c) (one two three) (you and me) (girl))
> > > do (collect pos :into foo)
> > > do (loop for sym in l
> > > do (collect pos :into foo))
> > > finally (return foo)))
> > > (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)
> >
> > ;;; Or....
> >
> > * (loop for pos upfrom 1
> > for l in '((a b c) (one two three) (you and me) (girl))
> > collect pos
> > append l)
> > (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)

MatzLisp (Ruby):

[[:a,:b,:c],[:one,:two,:three],[:you,:and,:me],[:girl]].each_with_index.
flat_map{|xs,i| [i+1, *xs]}
===>
[1, :a, :b, :c, 2, :one, :two, :three, 3, :you, :and, :me, 4, :girl]

--
Government is not reason, it is not eloquence, it is force; like fire, a
troublesome servant and a fearful master. Never for a moment should it be left
to irresponsible action. --- George Washington, speech of January 7, 1790
Use this [sword] for me, if I rule well; if not, against me. --- Trajan