COMPUTER SCIENCE 2P05 Programming Languages Data Abstraction and - - PowerPoint PPT Presentation

computer science 2p05
SMART_READER_LITE
LIVE PREVIEW

COMPUTER SCIENCE 2P05 Programming Languages Data Abstraction and - - PowerPoint PPT Presentation

COMPUTER SCIENCE 2P05 Programming Languages Data Abstraction and Encapsulation Brock University Brock University (Data Abstraction and Encapsulation)Programming Languages 1 / 25 Abstraction Recall that weve covered numerous forms of


slide-1
SLIDE 1

COMPUTER SCIENCE 2P05

Programming Languages Data Abstraction and Encapsulation

Brock University

Brock University (Data Abstraction and Encapsulation)Programming Languages 1 / 25

slide-2
SLIDE 2

Abstraction

Recall that we’ve covered numerous forms of abstraction, both procedural and data. For a problem of any significant size, it becomes intractable to consider “all things” simultaneously Sometimes we write functions, methods, etc. to simplify how we can write algorithms, and sometimes we want to use a single object as a substitute for a more complicated arrangement of information

Brock University (Data Abstraction and Encapsulation)Programming Languages 2 / 25

slide-3
SLIDE 3

Abstract Data Types

ADTs

Remember all the way back to first-year? ADTs? Would anyone care to take a stab at defining what an ADT is?

Brock University (Data Abstraction and Encapsulation)Programming Languages 3 / 25

slide-4
SLIDE 4

Data types that are abstracted!

(Helpful, right?)

Yes, an ADT is abstract. ...and? Well, let’s look at what we know a good ADT will typically include: A well-defined interface

◮ e.g. is a stack without a push or pop really a stack?

Information-hiding

◮ We’d rather use getters and setters (accessors and updaters), rather

than directly manipulating information

⋆ Both because it reduces the chances of unexpected side effects, and

because we can control what is/isn’t allowed

◮ We remember how we achieve this in Java, right?

Often the ability to have multiple implementations

◮ Though this largely relates back to the first point Brock University (Data Abstraction and Encapsulation)Programming Languages 4 / 25

slide-5
SLIDE 5

Reminder on encapsulation

(Before we get to the fun stuff)

As a quick reminder: when we talk about encapsulation, we’re talking about bundling up state information, and the operations you can perform

  • n that information, together

This is, of course, closely related to closures

◮ You can look at a closure as being an example of encapsulation ◮ For this lecture, we’ll be looking at a different example: defining

  • perators to operate on records to create ADTs

Brock University (Data Abstraction and Encapsulation)Programming Languages 5 / 25

slide-6
SLIDE 6

Sample data structure

We’re going to need a sample ADT. Something we can use for a couple examples Let’s go with a basic group-queue: We’ll add records in groups

◮ These groups will remain effectively atomic for their time within the GQ

We can also remove a group, which follows FIFO compared to the

  • ther groups

We can traverse the GQ

◮ This does not mean the GQ itself will be printing! Traversal has many

applications; not all of which are printing!

We can concatenate two GQs into a larger single one We can retrieve the ith group We can ask if an entire group (group member?) is within the GQ Seems like a good start. We can revise it if necessary.

Brock University (Data Abstraction and Encapsulation)Programming Languages 6 / 25

slide-7
SLIDE 7

Our Group Queue in Java

How would we go about making such a thing in Java? First, we’d probably want to write an interface, right?

◮ That interface would include abstract method headers ◮ We’d define English-ish methods to correspond to the operators

We’d provide one or more concrete implementations

◮ These would need to satisfy the interface’s promises ◮ We’d need to pick constructors that make sense ⋆ We might even use a little constructor chaining

We’d probably also define some exceptions, but... next (lecture) week Considering this is the kind of thing we’ve been doing since 1P03, we can skip actually making this, right?

Brock University (Data Abstraction and Encapsulation)Programming Languages 7 / 25

slide-8
SLIDE 8

Okay, then how about another language?

Like, random suggestion... Python?

Can we write an interface in Python? What about an abstract class? Let’s answer the latter first: Yes, we can... sort of We could expliclty create a class and provide dummy implementations that gripe if you try to instantiate them

◮ Think: something roughly analogous to the typical remove() method

  • f an Iterator in Java

Alternatively, there’s an abc (Abstract Base Class) module for formalizing this Also, this is one of those you need to do it differently between Python 2 and 3 things More reading here:

◮ https://docs.python.org/2.7/library/abc.html ◮ http://masnun.rocks/2017/04/15/interfaces-in-python-protocols-and-abcs/

(We could’ve just as easily answered this for C++, which does have e.g. pure virtual functions, but we’d be here all day if we wanted to cover everything)

Brock University (Data Abstraction and Encapsulation)Programming Languages 8 / 25

slide-9
SLIDE 9

Yeah, but what about interfaces?

Well, that’s the thing. What is the primary difference between an interface and an abstract class? Perhaps that an abstract class is directly involved in implementation? Maybe an interface is purely-abstract, and, e.g. defines an ADT? (You could get lots of answers for this question!) Perhaps this means that what most matters for an interface is that it defines the type according to how you use it?

Brock University (Data Abstraction and Encapsulation)Programming Languages 9 / 25

slide-10
SLIDE 10

Let’s test this theory!

Let’s see how quickly we can define the following (let’s say in Java, not that it should matter much): A stack A queue An Iterator An Iterable

Brock University (Data Abstraction and Encapsulation)Programming Languages 10 / 25

slide-11
SLIDE 11

What does this mean?

Quack quack!

This term has come up before, but Python uses Duck-Typing If it walks like a duck, and quacks like a duck, then it’s a duck How does this help us? First, we need to actually write our data structure. To the code!

Brock University (Data Abstraction and Encapsulation)Programming Languages 11 / 25

slide-12
SLIDE 12

That example is incomplete!

This is true! We haven’t learned enough to finish it yet! Let’s look at an easy one: being able to access the ith element It may not seem like it, but this is actually an operator Specifically, the [] operator Recall the [] we’ve seen the most: slices for lists (or substrings for strings) Depending on the exact usage and number of parameters, there are several related operators Let’s try it out?

Brock University (Data Abstraction and Encapsulation)Programming Languages 12 / 25

slide-13
SLIDE 13

Let’s expand a bit more?

What else can we do with the []? Let’s see!

Brock University (Data Abstraction and Encapsulation)Programming Languages 13 / 25

slide-14
SLIDE 14

Just a bit more before the real fun!

Before we get to the real fun, let’s look at checking membership, and also try using a more conventional operator?

Brock University (Data Abstraction and Encapsulation)Programming Languages 14 / 25

slide-15
SLIDE 15

How did that concatenation work?

We just relied on the concatenation provided by the list type. Don’t re-invent the wheel if you don’t need to However, how confident are we that it really worked? Put another way, do we remember the difference between a deep copy and a shallow copy?

Brock University (Data Abstraction and Encapsulation)Programming Languages 15 / 25

slide-16
SLIDE 16

Traversal time!

Okay, so it’s finally time for some iteration! And... what defines something as iterable? IT’S A DUCK!

◮ Wait, not quite ◮ It’s iterable if it can give an iterator (and can, if desired, be its own

iterator)

◮ How/when? It has an

iter that’s called by the iter() function

⋆ If it’s its own iterator, it’ll just return self

Okay... then what’s an iterator? In Python 2: It has a next function, called by next() In Python 3: It has next , still called by next() Let’s try it out?

Brock University (Data Abstraction and Encapsulation)Programming Languages 16 / 25

slide-17
SLIDE 17

It broke

Indeed it did. That’s because, traditionally, iterators keep going until they break. That’s not a bug; it’s a feature When we’re done iterating, we just throw (well, raise) a StopIteration exception! Let’s see what we mean?

Brock University (Data Abstraction and Encapsulation)Programming Languages 17 / 25

slide-18
SLIDE 18

Generators

Okay, so then, what’s a generator? Remember the list comprehensions we’ve briefly touched on a couple

  • f times?

◮ If not, we can write a quick example

The problem with them is they create the entire list; they allocate everything in advance This is fine if we want all the values simultaneously, but if we only need one at a time, that can be a memory hog (as well as front-loading our computation time when we could be spreading it

  • ut)

A generator is an object that can provide each item as you need it. It behaves like an iterator (and actually creates one for you). Let’s first look at a generator expression, as it’s very close to the list comprehension.

Brock University (Data Abstraction and Encapsulation)Programming Languages 18 / 25

slide-19
SLIDE 19

Are those the only generators?

  • Nope. There’s also a big one we hinted at very early on.

Remember how we briefly discussed coroutining last week? For the subset of problems where we just want one thing to keep producing elements, and another to keep consuming them, with control being handed off back and forth (it comes up more than you’d think), we have a reasonable alternative: yield The yield statement turns the current function into a generator

◮ Whenever a yield is encountered, execution within that function is

suspended until something pulls out the next token via the generator’s next

◮ Since it creates an iterator for you, it will raise a StopIteration

if/when it exhausts

Perhaps two quick examples?

Brock University (Data Abstraction and Encapsulation)Programming Languages 19 / 25

slide-20
SLIDE 20

That’s it, right?

We don’t want to look at a ridiculous example, do we? By the way, Python 3 has a much more flexible yield from, which is good for effectively chaining generators.

Brock University (Data Abstraction and Encapsulation)Programming Languages 20 / 25

slide-21
SLIDE 21

Let’s play with a trivial closure for a second

Totally no reason for this...

Brock University (Data Abstraction and Encapsulation)Programming Languages 21 / 25

slide-22
SLIDE 22

Pour some syntactic sugar on meee!

This is a good time to introduce some syntactic sugar. Generally speaking, we say a feature is syntactic sugar when:

◮ It makes it easier to write (or read) a snippet of code ◮ It’s an optional rephrasing to turn it into a more natural language

syntax

◮ It (usually) has a more complex — but equivalent — ‘traditional’ way

  • f writing it

Example? For-each loops in Java (which, of course, could just be expressed according to their Iterators explicitly) How does this apply here? There’s a decorator for exactly this type of situation. Let’s take a look!

Brock University (Data Abstraction and Encapsulation)Programming Languages 22 / 25

slide-23
SLIDE 23

What other decorators do we have?

Several, actually, but let’s just look at one more set of neat ones.

Brock University (Data Abstraction and Encapsulation)Programming Languages 23 / 25

slide-24
SLIDE 24

Additional reading

If only we had a week dedicated to reading coming up...

Bounded parametric types in Java:

◮ https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html ◮ http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ103

Built-in functions in Python:

◮ https://docs.python.org/2/library/functions.html

Python’s operator functions:

◮ https://docs.python.org/2.7/library/operator.html

Some neat uses of decorators in Python:

◮ https://www.codementor.io/sheena/advanced-use-python-decorators-class-function-du107nxsv Brock University (Data Abstraction and Encapsulation)Programming Languages 24 / 25

slide-25
SLIDE 25

Questions?

Comments?

Anyone have a sudden urge to learn a module on your own?

◮ Seriously, who does/doesn’t feel like independently learning a library to

make something neat?

Brock University (Data Abstraction and Encapsulation)Programming Languages 25 / 25