Site banner
.
Home Privacy Policy and Contact                    
.
.
Wisdom Archive
Body Mind and Soul
Faith and Belief
God and Religion
Law of Attraction
Life and Beyond
Love and Happiness
Peace of Mind
Peace on Earth
Personal Faith
Spiritual Festivals
Spiritual Growth
Spiritual Guidance
Spiritual Inspiration
Spirituality and Science
Spiritual Retreats
More Wisdom
Buddhism Archives
Hinduism Archives
Sustainability
Theology Archives
Even more Wisdom
2012 - Year 2012
Affirmations
Aura
Ayurveda
Chakras
Consciousness
Cultural Creatives
Diksha (Deeksha)
Dream Dictionary
Dream Interpretation
Dream interpreter
Dreams
Enlightenment
Essential Oils
Feng Shui
Flower Essences
Gaia Hypothesis
Indigo Children
Kalki Bhagavan
Karma
Kundalini
Kundalini Yoga
Life after death
Mayan Calendar
Meaning of Dreams
Meditation
Morphogenetic Fields
Psychic Ability
Reincarnation
Spiritual Art, Music & Dance
Spiritual Awakening
Spiritual Enlightenment
Spiritual Healing
Spirituality and Health
Spiritual Jokes
Spiritual Parenting
Vastu Shastra
Womens Spirituality
Yoga Positions
Site map 2
Site map
.

Callback computer science - Motivation

Callback computer science - Motivation: Encyclopedia II - Callback computer science - Motivation

To understand the motivation for using callbacks, consider the problem of performing an arbitrary operation on each item in a list. One approach is to obtain an iterator over the list, and then operate on successive objects obtained from the iterator. This is the most common solution in practice, but it is not ideal; the code to manage the iterator must be duplicated at each point in the code where the list is traversed. Furthermore, if the list is updated by an asynchronous process, the iterator might ...

See also:

Callback computer science, Callback computer science - Motivation, Callback computer science - Implementation, Callback computer science - Special cases

Callback computer science, Callback computer science - Implementation, Callback computer science - Motivation, Callback computer science - Special cases, libsigc++ - Callback Framework for C++ [1], Interceptor pattern

Callback computer science: Encyclopedia II - Callback computer science - Motivation



Callback computer science - Motivation

To understand the motivation for using callbacks, consider the problem of performing an arbitrary operation on each item in a list. One approach is to obtain an iterator over the list, and then operate on successive objects obtained from the iterator. This is the most common solution in practice, but it is not ideal; the code to manage the iterator must be duplicated at each point in the code where the list is traversed. Furthermore, if the list is updated by an asynchronous process, the iterator might skip over items or become corrupt during the traversal.

An alternative might be to create a new library function that performs the desired operation with appropriate synchronization. This approach still requires each new library function to contain the code to traverse the list. This solution is not acceptable for libraries intended for broad applications; the library developer cannot anticipate every application need, and the application developer should not need to know the details of the library implementation.

Callbacks solve these shortcomings. One procedure is written to traverse the list, and this procedure uses application-provided code to operate on each item. There is a clear distinction between the library and the application without sacrificing flexibility.

The following code in C demonstrates the use of callbacks for the specific case of searching an array for an item (namely, the first integer greater than 5). First, the iteration approach:

for(i = 0; i < length; i++) {
  if(array[i] > 5)
    break;
}
if(i < length) {
  printf("Item %d\n", i);
} else {
  printf("Not found\n");
}

Next, the callback approach:

/* LIBRARY CODE */
int traverseWith(int array[], size_t length, 
                 int (*callback)(int index, int item, void *param), 
                 void *param)
{
  int exitCode = 0;
  for(i = 0; i < length; i++) {
    exitCode = callback(i, array[i], param);
    if(exitCode) {
      break;
    }
  }
  return exitCode;
}

/* APPLICATION CODE */
int search(int index, int item, void *param)
{
  if(item > 5) {
    *(int *)param = index;
    return 1;
  } else {
    return 0;
  }
}

/* (in another function) */
int index;
int found;
found = traverseWith(array, length, search, &index);
if(found) {
  printf("Item %d\n", index);
} else {
  printf("Not found\n");
}

In this example the callback receives an extra parameter so that it can access data from the calling scope (a form of closure). Other languages, particularly functional programming languages, can provide this access automatically. Synchronization concerns have been omitted from these examples, but they can easily be addressed in the traverseWith function. More importantly, they can be addressed, or ignored, by changing only that function.




Adapted from the Wikipedia article "Motivation", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki

More material related to Callback Computer Science can be found here:
Main Page
for
Callback Computer Science
Index of Articles
related to
Callback Computer Science


« Back






Search the Global Oneness web site
Global Oneness is a huge, really huge, web site. Almost whatever you are searching for within health, spirituality, personal development and inspirationals - you will find it here!
Google
 
 

Rate this article!

Please rate this article with 10 as very good and 1 as very poor.

.






  » Home » » Home »