Category talk:Iteration: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎OpenMP looping?: new section)
(Explain why list iteration is not just Map)
Line 3: Line 3:
: Loop/Map is already there, as [[Apply a callback to an Array]] (see the Common Lisp solution!) I don't know what Loop/Fold means, but maybe it's also already there under another name. --[[User:Ce|Ce]] 13:28, 18 April 2008 (MDT)
: Loop/Map is already there, as [[Apply a callback to an Array]] (see the Common Lisp solution!) I don't know what Loop/Fold means, but maybe it's also already there under another name. --[[User:Ce|Ce]] 13:28, 18 April 2008 (MDT)
::The articles [[Sum and product of array]] contains some examples of '''Fold'''(or reduce). '''Fold''' apply a binary function(ZxZ->Z) to each elements of a collection with an accumulator, the result value is set to the accumulator, and the final value of accumulator after iterated all elements is the result of '''Fold''' (the accumulator initialized to 1.0 in Product case, and 0.0 in Sum case). '''Map''' is similar, but with an unary function Z->Z. There is also '''Filter/Select'''(compare [[Select from Array]]) similar to '''Map''', with an unary boolean function, but don't keep elements if result value is false. -- [[User:Badmadevil|badmadevil]] 05:58, 19 April 2008 (MDT)
::The articles [[Sum and product of array]] contains some examples of '''Fold'''(or reduce). '''Fold''' apply a binary function(ZxZ->Z) to each elements of a collection with an accumulator, the result value is set to the accumulator, and the final value of accumulator after iterated all elements is the result of '''Fold''' (the accumulator initialized to 1.0 in Product case, and 0.0 in Sum case). '''Map''' is similar, but with an unary function Z->Z. There is also '''Filter/Select'''(compare [[Select from Array]]) similar to '''Map''', with an unary boolean function, but don't keep elements if result value is false. -- [[User:Badmadevil|badmadevil]] 05:58, 19 April 2008 (MDT)
:: FWIW, [[Apply a callback to an Array]] isn't Loop/Map because there's no requirement to reassemble the values into another array (or sequence/list, etc.) In [[SML]]-like type-theoretic terms, Map is
::: <math>(\alpha \rightarrow \beta) \rightarrow \alpha\, \mathrm{list} \rightarrow \beta\, \mathrm{list}</math>
:: whereas that task allows
::: <math>(\alpha \rightarrow \mathrm{unit}) \rightarrow \alpha \, \mathrm{list} \rightarrow \mathrm{unit}</math>
:: These are quite different type signatures, though Map can be used for the other by throwing away the final result. –[[User:Dkf|Donal Fellows]] 12:40, 4 January 2010 (UTC)


What about "implied loops" like the one Fortran can use to initialize arrays or in the where construct? (Maybe implied is not correct... but the final operation in other languages like C is a ''for'' loop, but in these languages it is not written explicitly); e.g.
What about "implied loops" like the one Fortran can use to initialize arrays or in the where construct? (Maybe implied is not correct... but the final operation in other languages like C is a ''for'' loop, but in these languages it is not written explicitly); e.g.

Revision as of 12:40, 4 January 2010

I'm not sure about Loop/Map and Loop/Fold. I've never heard of them before. Can someone else fill in tasks for them? --Mwn3d 12:33, 14 April 2008 (MDT)

Loop/Map is already there, as Apply a callback to an Array (see the Common Lisp solution!) I don't know what Loop/Fold means, but maybe it's also already there under another name. --Ce 13:28, 18 April 2008 (MDT)
The articles Sum and product of array contains some examples of Fold(or reduce). Fold apply a binary function(ZxZ->Z) to each elements of a collection with an accumulator, the result value is set to the accumulator, and the final value of accumulator after iterated all elements is the result of Fold (the accumulator initialized to 1.0 in Product case, and 0.0 in Sum case). Map is similar, but with an unary function Z->Z. There is also Filter/Select(compare Select from Array) similar to Map, with an unary boolean function, but don't keep elements if result value is false. -- badmadevil 05:58, 19 April 2008 (MDT)
FWIW, Apply a callback to an Array isn't Loop/Map because there's no requirement to reassemble the values into another array (or sequence/list, etc.) In SML-like type-theoretic terms, Map is
whereas that task allows
These are quite different type signatures, though Map can be used for the other by throwing away the final result. –Donal Fellows 12:40, 4 January 2010 (UTC)

What about "implied loops" like the one Fortran can use to initialize arrays or in the where construct? (Maybe implied is not correct... but the final operation in other languages like C is a for loop, but in these languages it is not written explicitly); e.g. <lang c>for(i=0; i<10; i++) {

 if ( ( arr[i] % 5 ) == 0 ) {
    arr[i] = 9
 } else {
    arr[i] = 3
 }

}</lang>

in Fortran(90/95) would be

<lang fortran>where ( mod(arr, 5 ) == 0 )

 arr = 9

elsewhere

 arr = 3

end where</lang>

And to initialize an array with numbers from 0 to 100 one can write

<lang fortran>integer, dimension(101) :: a = (/ (j,j=0,100) /)</lang>

Is there any possibile classification that makes sense to other languages too? --ShinTakezou 00:17, 18 February 2009 (UTC)

OpenMP looping?

It would be nice to have some examples of OpenMP looping constructs in here (probably as subsections of the C and Fortran language examples) since they're interesting in their own right and are fairly common extensions. —Dkf 11:38, 23 May 2009 (UTC)