Category talk:Iteration: Difference between revisions

From Rosetta Code
Content added Content deleted
m (remove last sententce, as not sure if it is true (I don't really understood what a callback is))
(fitting fortran where, i,i=A,B "loops")
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)

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? --[[User:ShinTakezou|ShinTakezou]] 00:17, 18 February 2009 (UTC)

Revision as of 00:17, 18 February 2009

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)

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)