lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Luke once stated:
> That's a shame.
> 
> Are these "advanced concepts" difficult to learn or do they just
> depend on handful of other terms? Seed was in the lectures' definition
> of fold, and then the next slide confused confused me further:

  Some, like "map" or "fold", are not difficult but depend upon other concepts;
others like "monad" are so opaque as to defy definition (a monad is just a
monoid in the category of endofunctors, or so I'm told, but I'm not a
mathematician or type theorist).

> But I believe that map never takes any parameters other than the
> function it is called with: square builds a new function from a map
> function that never sees the table. 

  Maybe "a" particular implementation of map() will do that, but not all. 
In general, you have three related concepts:  map, filter, reduce (or fold).

	map: maps a list of values to new values, for instance:

		x = { 1 , 2 , 3 , 4}
		y = map(x,function(x) return x * x end)
		-- y has { 1 , 4 , 9 , 16 }

	filter: filters out values that fit some criteria into a new list.

		y = filter(x,function(x) return x % 2 == 0 end)
		-- y has { 2 , 4 }

	reduce (aka fold): maps a list of values to a single result, such
	       as the sum of all items:

		y = reduce(x,function(a,b) return a + b end)
		-- y is now 10

  The think to keep in mind is that in general, map() need not work "left to
right" (it can do it right to left, or all at the same time, etc.). 
reduce() will usually go "left to right" and if you need the opposite
direction, there might be a reducer() function; filter works simularly, but
as always, check the manual before using.

  Also, every instance I've seen (in other langauges; other implementations)
map() will also always take the data and a function to run over the items;
same for filter() and reduce().

  -spc (Remember, the meanings behind these terms will come in time, 
	except for monads ... )