Difference between revisions of "HOM"

From GNUstepWiki
Jump to navigation Jump to search
 
(Added some examples)
Line 5: Line 5:
 
However, it is possible to implement HOM (using trampolines), but HOM is not a language feature at this point.
 
However, it is possible to implement HOM (using trampolines), but HOM is not a language feature at this point.
  
'''What does HOM look like?'''
+
 
 +
 
 +
== What does HOM look like? ==
  
 
Suppose you have an [[NSArray]] myArray containing objects you want to call a message on. Normally you would have to do something like
 
Suppose you have an [[NSArray]] myArray containing objects you want to call a message on. Normally you would have to do something like
Line 19: Line 21:
 
Simply put: the ''-do'' message tells myArray to call ''-doSomething'' on each of its objects.
 
Simply put: the ''-do'' message tells myArray to call ''-doSomething'' on each of its objects.
  
'''HOM related messages'''
 
  
* ''-each''
 
Iterates through each object, similar to ''-objectEnumerator''.
 
  
* ''-collect''
+
== HOM related messages ==
Returns an array of the responses to the argument message.
+
 
 +
To further explain how HOM works, here's a list of usual methods including a simple example.
 +
 
 +
We start by assuming there's an array
 +
 
 +
  NSString *dummy;
 +
  NSArray *array;
 +
 
 +
  dummy = @"one two three four five six seven eight nine ten";
 +
  array = [dummy componentsSeparatedByString: @" "];
 +
 
 +
Now let's see how to use HOM methods on that array and what the results will be:
 +
 
  
* ''-select''
+
* -select
 
Returns members of the array if they respond YES to the argument message -- which necessarily returns a BOOL.
 
Returns members of the array if they respond YES to the argument message -- which necessarily returns a BOOL.
  
* ''-reject''
+
  NSArray *array2 = [[array select] hasPrefix: @"t"];
 +
  NSLog([array2 description]);
 +
 
 +
This will give us all numbers starting with a ''t''
 +
 
 +
  (two, three, ten)
 +
 
 +
 
 +
* -reject
 
The inverse of ''-select''; it returns the elements responding NO.
 
The inverse of ''-select''; it returns the elements responding NO.
 +
 +
  NSArray *array3 = [[array2 reject] hasSuffix: @"e"];
 +
  NSLog([array3 description]);
 +
 +
will return all numbers in ''array2'' that do '''not''' end with an ''e''
 +
 +
  (two, ten)
 +
 +
 +
* -collect
 +
Executes the argument message and returns an array of the responses to the argument message.
 +
 +
  NSArray *array4 = [[array3 collect] stringByAppendingString: @" book"];
 +
  NSLog([array4 description]);
 +
 +
will add ''book'' to all objects in ''array3''
 +
 +
  ("two book", "ten book")
 +
 +
 +
* ''-do''
 +
Executes the argument message.
 +
 +
  NSArray *array5 = [[array collect] mutableCopy];
 +
  [[array5 do] appendString: @" eggplants"];
 +
  NSLog([array5 description]);
 +
 +
will create a mutable copy of ''array'' and append ''eggplants'' to each object.
 +
 +
  (
 +
    "one eggplants",
 +
    "two eggplants",
 +
    "three eggplants",
 +
    "four eggplants",
 +
    "five eggplants",
 +
    "six eggplants",
 +
    "seven eggplants",
 +
    "eight eggplants",
 +
    "nine eggplants",
 +
    "ten eggplants"
 +
  )
 +
 +
As you can see, ''-do'' is quite similar to ''-collect'' but works on the objects inside the collection (here ''array5'' -- the mutable copy of ''array'') instead of returning a new array (as ''-collect'' does).
 +
 +
 +
* ''-each''
 +
Returns each object, similar to ''-objectEnumerator''.
 +
 +
  NSArray *string = [[@"I like to eat " collect]  stringByAppendingString: [array5 each]];
 +
 +
This is a stupid example (because I do not like eggplants) that would give us
 +
 
 +
  (
 +
    "I like to eat one eggplants",
 +
    "I like to eat two eggplants",
 +
    "I like to eat three eggplants",
 +
    "I like to eat four eggplants",
 +
    "I like to eat five eggplants",
 +
    "I like to eat six eggplants",
 +
    "I like to eat seven eggplants",
 +
    "I like to eat eight eggplants",
 +
    "I like to eat nine eggplants",
 +
    "I like to eat ten eggplants"
 +
  )
 +
  
 
* ''-performAfterDelay:''
 
* ''-performAfterDelay:''
 
Performs the argument message after a specified delay.
 
Performs the argument message after a specified delay.
 +
 +
  [[someArray performAfterDelay: 5] delayedMessage];
 +
  
 
* ''-ignoreExceptions''
 
* ''-ignoreExceptions''
 
Traps and ignores any exception that may occur during execution of the argument message.
 
Traps and ignores any exception that may occur during execution of the argument message.
 +
 +
  [[someArray ignoreExceptions] messageThatMayRaiseAnException];

Revision as of 12:48, 20 September 2005

HOM (short for High Order Messaging) is a mechanism for encapsulating control structures and other programming patterns. It is similar to blocks in Smalltalk and higher order functions in functional languages.

Objective-C does not have blocks. They're not a part of the language, although Brad Cox's Task Master Paper outlines them nicely (and the Portable Object Compiler implements them).

However, it is possible to implement HOM (using trampolines), but HOM is not a language feature at this point.


What does HOM look like?

Suppose you have an NSArray myArray containing objects you want to call a message on. Normally you would have to do something like

 [myArray makeObjectsPerformSelector: @selector(someOtherMessage)];

With HOM you simply write

 [[myArray do] someOtherMessage];

instead.

Simply put: the -do message tells myArray to call -doSomething on each of its objects.


HOM related messages

To further explain how HOM works, here's a list of usual methods including a simple example.

We start by assuming there's an array

 NSString *dummy;
 NSArray *array;
 
 dummy = @"one two three four five six seven eight nine ten";
 array = [dummy componentsSeparatedByString: @" "];

Now let's see how to use HOM methods on that array and what the results will be:


  • -select

Returns members of the array if they respond YES to the argument message -- which necessarily returns a BOOL.

 NSArray *array2 = [[array select] hasPrefix: @"t"];
 NSLog([array2 description]);

This will give us all numbers starting with a t

 (two, three, ten)


  • -reject

The inverse of -select; it returns the elements responding NO.

 NSArray *array3 = [[array2 reject] hasSuffix: @"e"];
 NSLog([array3 description]);

will return all numbers in array2 that do not end with an e

 (two, ten)


  • -collect

Executes the argument message and returns an array of the responses to the argument message.

 NSArray *array4 = [[array3 collect] stringByAppendingString: @" book"];
 NSLog([array4 description]);

will add book to all objects in array3

 ("two book", "ten book")


  • -do

Executes the argument message.

 NSArray *array5 = [[array collect] mutableCopy];
 [[array5 do] appendString: @" eggplants"];
 NSLog([array5 description]);

will create a mutable copy of array and append eggplants to each object.

 (
   "one eggplants",
   "two eggplants",
   "three eggplants",
   "four eggplants",
   "five eggplants",
   "six eggplants",
   "seven eggplants",
   "eight eggplants",
   "nine eggplants",
   "ten eggplants"
 )

As you can see, -do is quite similar to -collect but works on the objects inside the collection (here array5 -- the mutable copy of array) instead of returning a new array (as -collect does).


  • -each

Returns each object, similar to -objectEnumerator.

 NSArray *string = [[@"I like to eat " collect]  stringByAppendingString: [array5 each]];

This is a stupid example (because I do not like eggplants) that would give us

 (
   "I like to eat one eggplants",
   "I like to eat two eggplants",
   "I like to eat three eggplants",
   "I like to eat four eggplants",
   "I like to eat five eggplants",
   "I like to eat six eggplants",
   "I like to eat seven eggplants",
   "I like to eat eight eggplants",
   "I like to eat nine eggplants",
   "I like to eat ten eggplants"
 )


  • -performAfterDelay:

Performs the argument message after a specified delay.

 [[someArray performAfterDelay: 5] delayedMessage];


  • -ignoreExceptions

Traps and ignores any exception that may occur during execution of the argument message.

 [[someArray ignoreExceptions] messageThatMayRaiseAnException];