- Define
map
-like function - Define
reduce
-like function
In this lab, we're going to practice building our own versions of the
Enumerable methods that do map
-like and reduce
-like work. In coding these,
we'll sense the non-DRY (Don't Repeat Yourself) quality of writing map
- and
reduce
-based functions and want a better way. You should not use the
built-in Ruby methods map
or reduce
to solve this lab.
It's also a chance to know that if we ever go to a language that doesn't have awesome Enumerables built-in, we could write a replacement function easily.
For the next few lessons, we're only going to talk about Enumerables in the
context of Array
s. While Hash
es also feature methods that follow the
"Character of Enumerable Methods," for ease of learning, we're going to focus
only on Array
s.
As mentioned in the "Character of Enumerables," we need to visit each member of
a collection. This is common to all Enumerable methods. In the case of map
,
we're going to produce a new Array
after "transforming" or applying "work"
to each element. An example would be "multiply each number in this Array
by
-1
, returning a new Array
of the input Array
"negative-ized."
Here's a tabular representation:
Operation | Element 0 | Element 1 | Element 2 |
---|---|---|---|
Base Set | 1 | 2 | 3 |
"Negativize" Function Result | -1 | -2 | -3 |
Grand Result = [-1, -2, -3]
Naming History "Map" comes from mathematics where it means:
- Taking an independent variable
- Plugging it into an equation
- Getting a result back
Mathematicians would say you're mapping a value in the domain to a value in the range.
If this sounds vaguely familiar, you might have learned it in algebra when learning to graph on the Cartesian coordinate system.
- Take a value on the x axis
- Plug it into a function like
y=mx + b
- Get a y value
Hopefully you're having an "Ah-hah!" moment from that and might be considering sending your algebra teachers a thank-you note.
As mentioned in the "Character of Enumerables," we need to visit each member of
a collection. This is common to all Enumerable methods. In the case of
reduce
, we're going to accumulate the results of the "work" to produce a new,
single value. An example would be "sum up an Array
of numbers." You combine
each element into the new aggregate total by updating the aggregate by using an
expression like total = total + current_element
. This updating an aggregator
value and returning it at the end is the essence of reduce.
The reduce
function should be given a starting point as an argument.
Here's a tabular representation of reduce
-ing an Array
of [1, 2, 3]
with a
starting point of 0
.
Operation | Element 0 | Element 1 | Element 2 |
---|---|---|---|
Base Set | 1 | 2 | 3 |
Add to previous result function result | 1 | 3 | 6 |
Grand Result = 6
Naming History This idea of "reduce" comes from lots of places, but we like to think about it coming from the realm of cooking where we make a "reduction" by applying work (aka "heat") until what's left over is the thing we want.
Inspired by @steveluscher
In this lab, we're going to write several map
-like and reduce
-like
methods and put them in lib/my_code.rb
:
map_to_negativize(source_array)
map_to_no_change(source_array)
map_to_double(source_array)
map_to_square(source_array)
Remember, all map
methods return a new Array
.
reduce_to_total(source_array, starting_point)
reduce_to_all_true(source_array)
reduce_to_any_true(source_array)
Remember, all reduce
methods return a value.
In this lab you've created your own implementation of methods that work like
two of the most powerful Enumerable methods: map
and reduce
. You've also
experienced some of the non-DRY code that this requires.