Skip to content

Commit

Permalink
Move discussion of map-indexed earlier.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeisenberg committed Mar 30, 2017
1 parent 32d4411 commit 5b45ba6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions _sources/closures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
.. |---| unicode:: U+2014 .. em dash, trimming surrounding whitespace
:trim:

Graphing Program: Drawing
Graphing Program: Closures
'''''''''''''''''''''''''''

All of the functions that do the actual drawing of the graph (axes, lines, dots, etc.), need to convert graph coordinates (*x*, y*) to the corresponding canvas coordinates, so the program needs a function like ``(to-screen [[x y]])``. But wait |---| this function needs to know the minimum and maximum *x* and *y* as well as the width and height of the canvas. You don’t want to “hard code” the numbers into the function for a particular canvas size (300 x 200) and graph dimensions (*x* from 0 to 7 and *y* from 0 to 25)::
All of the functions that do the actual drawing of the graph (axes, lines, dots, etc.), need to convert graph coordinates (*x*, *y*) to the corresponding canvas coordinates, so the program needs a function like ``(to-screen [[x y]])``. But wait |---| this function needs to know the minimum and maximum *x* and *y* as well as the width and height of the canvas. You don’t want to “hard code” the numbers into the function for a particular canvas size (300 x 200) and graph dimensions (*x* from 0 to 7 and *y* from 0 to 25)::

(defn to-screen [[x y]]
(let [sx (+ (* (/ (* 0.85 300) 7) x) (* 0.075 300))
Expand Down
50 changes: 0 additions & 50 deletions _sources/graphing_flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,3 @@ The program will have to:
* Draw dots for the minimum temperatures
* Draw a connected line for the maximum temperatures
* Draw dots for the maximum temperatures

Before moving on to all the drawing, let’s do the splitting. The trick here is to use the ``map-indexed`` function. It works like ``map``, except that the function you provide has two parameters: the “index number” and the item from the collection being mapped. Here is an example that takes a list of words and returns a vector of upper-cased words and their position in the list (starting at zero):

.. activecode:: map_indexed_example
:language: clojurescript

(defn counted-upper [index item]
(vector index (.toUpperCase item)))

(map-indexed counted-upper ["Cat" "DoG" "birD"])

Now, see if you can do the split of the temperatures. Hint: use ``map-indexed`` twice. The first time, extract the first item in the temperature pair (the low temperature). The second time, extract the second item in the temperature pair. Remember that the index stars at zero, so you will want to add one; you can use the ``inc`` function for that.

.. container:: full_width

.. tabbed:: q1

.. tab:: Question

.. activecode:: split_indexed_question
:language: clojurescript

(def temperatures [[3 9] [2 13] [4 10] [4 9] [4 12] [9 20] [16 21]])

(defn splitter[data]
; your code here
)

(splitter temperatures)

.. tab:: Answer

.. activecode:: split_indexed_answer
:language: clojurescript

(def temperatures [[3 9] [2 13] [4 10] [4 9] [4 12] [9 20] [16 21]])

(defn index-min [index temperature-pair]
[(inc index) (first temperature-pair)])

(defn index-max [index temperature-pair]
[(inc index) (last temperature-pair)])

(defn splitter [data]
[(into [] (map-indexed index-min data))
(into [] (map-indexed index-max data))])

(splitter temperatures)


54 changes: 52 additions & 2 deletions _sources/project_intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,56 @@ When I wrote the original program, it was “custom built” to draw the graph d
[[[1 3] [2 2] [3 4] [4 4] [5 4] [6 9] [7 16]]
[[1 9] [2 13] [3 10] [4 9] [5 12] [6 20] [7 21]]]

Back in the chapter on :doc:`the reduce function </reduce_multi>`, you developed code to do a simple split into a minimum and maximum vector. Here’s the code again, using destructuring to make the code a bit more readable; see if you can modify it to do what we want here. Hint: start the result vector as ``[[] [] 1]``; the third item keeps count of which day you are working with.
Back in the chapter on :doc:`the reduce function </reduce_multi>`, you developed code to do a simple split into a minimum and maximum vector. While you could use ``reduce`` to create these vectors with the numbering as the first entry in each pair, you can also use the ``map-indexed`` function. It works like ``map``, except that the function you provide has two parameters: the “index number” and the item from the collection being mapped. Here is an example that takes a list of words and returns a vector of upper-cased words and their position in the list (starting at zero):

.. activecode:: map_indexed_example
:language: clojurescript

(defn counted-upper [index item]
(vector index (.toUpperCase item)))

(map-indexed counted-upper ["Cat" "DoG" "birD"])

Now, see if you can do the split of the temperatures. Hint: use ``map-indexed`` twice. The first time, extract the first item in the temperature pair (the low temperature). The second time, extract the second item in the temperature pair. Remember that the index stars at zero, so you will want to add one; you can use the ``inc`` function for that.

.. container:: full_width

.. tabbed:: q1

.. tab:: Question

.. activecode:: split_indexed_question
:language: clojurescript

(def temperatures [[3 9] [2 13] [4 10] [4 9] [4 12] [9 20] [16 21]])

(defn splitter[data]
; your code here
)

(splitter temperatures)

.. tab:: Answer

.. activecode:: split_indexed_answer
:language: clojurescript

(def temperatures [[3 9] [2 13] [4 10] [4 9] [4 12] [9 20] [16 21]])

(defn index-min [index temperature-pair]
[(inc index) (first temperature-pair)])

(defn index-max [index temperature-pair]
[(inc index) (last temperature-pair)])

(defn splitter [data]
[(into [] (map-indexed index-min data))
(into [] (map-indexed index-max data))])

(splitter temperatures)




.. container:: full_width

Expand Down Expand Up @@ -80,6 +129,7 @@ In addition to splitting up the data, the program needs functions to:
- Draw labels near the axes

* Draw a point.
* Draw a line between two points.
* Draw a line connecting a series of points.
* Draw a dot at each of the points.

All this talk of drawing |---| so how *do* you draw from ClojureScript? You use the API (application program interface) for the ``<canvas>`` element.

0 comments on commit 5b45ba6

Please sign in to comment.