Loop that collects results?

Notices and updates
Locked
kinghajj
Posts: 30
Joined: Sun Jul 15, 2007 2:37 pm

Loop that collects results?

Post by kinghajj »

Is there a loop that returns a list of the results of each iteration? For example:

Code: Select all

(collect (n (sequence 1 10))
    (* n 2))
; => (2 4 6 8 10 12 14 16 18 20)
I'm sure there are other ways to do this, like using map and curry. I could probably implement this as a macro, if there is no current way to do this.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Code: Select all

(map (fn (n) (* n 2)) (sequence 1 10))
;-> (2 4 6 8 10 12 14 16 18 20)
map will collect it for you.

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

(map (curry * 2) '(1 2 3...))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Code: Select all

(map << (sequence 1 10))
;-)

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Code: Select all

int main
{
    int seq[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int result[10];
    int i;
    for (i = 0; i < 10; i++)
    {
        result[i] = seq[i] * 2;
    }
    return 0;
}
[/code]
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

jrh
Posts: 36
Joined: Mon Nov 14, 2005 9:54 pm
Location: Portland, Oregon

Post by jrh »

Jeff wrote:

Code: Select all

int main
{
    int seq[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int result[10];
    int i;
    for (i = 0; i < 10; i++)
    {
        result[i] = seq[i] * 2;
    }
    return 0;
}
I think I see what you mean!

---

jrh
Posts: 36
Joined: Mon Nov 14, 2005 9:54 pm
Location: Portland, Oregon

Re: Loop that collects results?

Post by jrh »

Code: Select all

> (rest (filter (lambda (x) (= (% x 2)0)) (sort (unique (rand 21 210)))))

(2 4 6 8 10 12 14 16 18 20)
---

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

that's so silly, i love it!

jrh
Posts: 36
Joined: Mon Nov 14, 2005 9:54 pm
Location: Portland, Oregon

Post by jrh »

Don't let that computer just sit there. Generate some cpu cycles!

Code: Select all

> (map eval (transpose (append (transpose (dup (list (sym (char 42)) 2) 10)) (list (rest (index string? (explode "C++  sucks!")))))))

(2 4 6 8 10 12 14 16 18 20)
---

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

double_elements.so:

Code: Select all

void double_list_elements(int seq[], int seq_length, int result[])
{
    int i;
    for (i = 0; i < seq_length; i++)
    {
        result[i] = seq[i] * 2;
    }

}
my_script.lsp

Code: Select all

(import "double_elements.so "double_list_elements")
(set 'seq (sequence 1 10))
(set 'set2 '(array 10 (sequence 1 10))
(double_list_elements seq (length seq) seq2))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Locked