2

I know that it is rather a common pattern in many programming languages (predominantly functional) but I don't know exactly how does it call. So I have one data structure e.g. List A and other List B. List A contains some values (strings in Chinese) and I want to map this strings to List B translating them to English. The so called map and mutate. Can someone please tell how does this pattern named correctly and give some links to its implementation in objective-C , Java , Haskell, etc.

MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102
  • I answered, but then realized that I think you're asking something different to the question I answered. Can you give some clarification - for example, some example inputs together with the output you would expect? – Chris Taylor Oct 02 '13 at 14:51
  • 2
    @ChrisTaylor Thank you! No no. Your answer fully satisfy me. It is what i seek. I just thought it is called in some different way. You can post your answer once again in order I can accept it. – MainstreamDeveloper00 Oct 02 '13 at 14:58
  • Ah, okay. I've un-deleted it. – Chris Taylor Oct 02 '13 at 16:52

2 Answers2

7

This process is referred to as "mapping" or "map and mutate" (as you mentioned) and data types which can be mapped over can be instances of the Functor typeclass in Haskell (note that "functor" is used differently in C++). Additionally, in imperative languages, this process may be accomplished using a foreach-style loop.

Functional languages

Many functional language provide a default implementation of map for lists, and may provide default implementations for other data types as well. For example, the Haskell implementation of mapping over lists is:

map :: (a -> b) -> [a] -> [b]
map _ []     = []
map f (x:xs) = f x : map f xs

chineseToEnglish :: [String] -> [String]
chineseToEnglish chineseStrings = map translate chineseStrings

More complex examples exist for more complex data structure. Try searching your favorite data structure on Hoogle and looking at the source.

Imperative languages

While, in imperative languages, 3-element for loops provide the standard way to iterate over arrays, C++11, Java, and Obj-C all have more map-related for loops as well.

C++

C++11 provides an abstraction over iterators in its new ranged-for loop:

vector<string> chinese;
for (auto &s : chinese) {
    translate(s);
}

Extending the iterator class has been explained elsewhere

Java

Java provides a similar construct, but without automatic type inference or the need for explicit references:

ArrayList<LanguageString> chinese = new ArrayList();
for (LanguageString s : chinese) {
    s.translate();
}

Extending Iterable has also been explained elsewhere.

Objective-C

I'm not as familiar with Obj-C as the others I've mentioned, but that subject appears to have been discussed thoroughly at this SO post.

Community
  • 1
  • 1
Elliot Robinson
  • 1,384
  • 7
  • 16
3

This pattern is generally called map or apply depending on the language. The example I'll use is turning the list of numbers [1, 2, 3, 4, 5] into their squares, [1, 4, 9, 16, 15].

In function al programming languages this is generally straightforward. For example, in Haskell

>> let numbers = [1, 2, 3, 4, 5]
>> map (\x -> x^2) numbers
[1, 4, 9, 16, 25]

In R, which is a language with a function feel

>> numbers = c(1, 2, 3, 4, 5)
>> sapply(numbers, function(x) {x^2})
[1] 1  4  9 16 25

Or in Python

>> numbers = [1, 2, 3, 4, 5]
>> map(lambda x: x**2, numbers)
[1, 4, 9, 16, 25]

In languages that don't support first-class functions, like Java, you generally use a loop for this:

int[] numbers = {1, 2, 3, 4, 5};
int[] result  = new int[5];

for (i = 0; i < numbers.length; i++)
{
    result[i] = Math.pow(numbers[i], 2);
}
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154