Map and FOOP context functions

For the Compleat Fan
Locked
Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Map and FOOP context functions

Post by Jeff »

Can these not be used in map and other higher order functions, like apply and filter?
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Code: Select all

(define (Class:Class) 
	(cons (context) (args)))

(new Class 'Rectangle)
(new Class 'Circle)

(define (Rectangle:area p)
	(mul (p 3) (p 4)))

(define (Circle:area c)
	(mul (pow (c 3) 2) (acos 0) 2))

(set 'myrect (Rectangle 5 5 10 20))
(set 'mycircle (Circle 1 2 10))

(map (curry :area) (map eval '(myrect mycircle))) => (200 314.1592654)

(map (curry :area) '((Rectangle 5 5 10 20)(Circle 1 2 10))) => (200 314.1592654)
remember that:

Code: Select all

(:area myrect)

; same as

(: area myrect)
So its really the ':' operator which gets applied by 'map' or 'apply'. For that reason the 'area' parameter has to be curried.

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

Post by Jeff »

Gotcha. Thank you.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Locked