BH and Mary need to talk about Katex style. --MF, 9/12/18
A software library is a collection of procedures that can be used in programs.
Using libraries simplifies the development of new programs. When you use procedures that you already know work correctly, you reduce the amount of time you need to spend coding, the number of possible bugs your code can have, and how much of your project you need to test.
You created your even?
block using another custom block, divisible by?
. If you want to use even?
in another project one day, you must export both blocks in order for even?
to work properly.
If you ever see this red Obsolete!
block in code you have imported, it means that a required block was not exported. You'll have to go back to the original project and export again being sure to select all of the blocks needed by the blocks you want.
even?
predicate to develop an odd?
predicate for your library.You can create algorithms from scratch or by combining or modifying existing algorithms. When you are thinking of using existing code, you can either use your own code or use code from someone else's library.
keep
), and use it to answer these questions:
divisors
block using keep
.divisors
block
for
or for each
, but it will require less code to build it with keep
on Unit 2 Lab 3 Page 5: Keep
ing Items from a List.)mod
, but you already built mod
), so divisible by?
will be simpler to use.number of divisors
block.keep
to answer each of these questions that were introduced above:
You've now made a small library, and you might want to write a list of instructions for how to use the functions in your library (for example, divisors of
takes a positive integer as input and reports a list of numbers). The instructions form an Application Program Interface for the library.
An application program interface (API) documents what a programmer needs to know about using a library: it's a description of each procedure's purpose, inputs, and outputs (but not its algorithms).
A common kind of API is a web API in which the library exists on someone else's computer. For example, the Google Maps API describes how to embed a Google Map on your own website.
A web API is just a call to a procedure on another machine. For example, these are three different notations for the same procedure call, which looks up the number of searches for "BJC" in the US:
EXPLORE (q = BJC, geo = US)
pipe
works. I don't believe it's going to work as a pre-teaching for 5.3 because they are likely to forget stuff, so the question is: what does it offer here? And can we do it better? For that matter, is divisors
even a particularly elucidating example of using pipe
? What if we offered it in the TG and combined the stuff before this todo with the MOD stuff on the previous page? Or maybe keep the two pages separate for the sake of breathing room, but move some of the quizlets and review questions at the end of that page to the end of this page (e.g. #7-9)... --MF, 8/12/19
Nested functions (that is, functions used inside of other functions, like the way you used divisible by?
and numbers
inside of keep
) can be hard to read. Snap! offers another way to write code with multiple functions. You can use the function from the "Frequency Distribution Analysis" library to write your code one function at at time.
pipe
function sends input number through numbers from (1) to ()
to report a list of numbers and then that list goes through keep items such that ((input number) divisible by ()?) from ()
to get the numbers from that list that are divisible by the input number.
Notice that the empty input slots in each function are filled by the output of the previous function (or filled by the starting data in the case of the first function):
numbers
.keep
(that is, in keep
's input list (here, the output of numbers
) so that keep
can decide whether each number will be kept.Brian means some kind of horizontal diagram: 8 -->1, 2, 3, 4, 5, 6, 7, 8 --> 1, 2, 4, 8 --MF, 1/15/20
Pipe
is a higher order function; it is a function that takes functions as input. You've seen a higher order function before: keep
(in Unit 2 Lab 3).pipe
version of divisors
above. keep
before numbers
?number of divisors
block using the pipe
function.
number of divisors 2
or pipe version of number of divisors
, or you can rewrite the same block and just drag off the original report instruction but leave it inside the number of divisors
block unattached from the hat block so will be saved in there but it won't run.You don't ever have to use pipe
again, but if you find it hard to write a function with a bunch of nested reporters, you can always import pipe
again.