Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to Java 2 Programming Lecture 5 The Collections API.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
Principles of Object-Oriented Software Development The language Smalltalk.
Programming with Collections Collections in Java Using Arrays Week 9.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 Collections Working with More than One Number or Data Type or Object.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
12-Jul-15 Lists in Java Part of the Collections Framework.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
3. Standard Classes. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify everything — by reifying its entire implementation model, Smalltalk.
Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag,
Domain Modeling (with Objects). Motivation Programming classes teach – What an object is – How to create objects What is missing – Finding/determining.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CS107 References and Arrays By Chris Pable Spring 2009.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Built-in Data Structures in Python An Introduction.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
Understanding Data Types and Collections Lesson 2.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
1 Advanced Object Oriented Systems (CM0318) Lecture 8 (Last updated 15th February 2002)
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Collections Dwight Deugo Nesa Matic
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Sixth Lecture ArrayList Abstract Class and Interface
Fundamentals of Programming II Overview of Collections
CSCI-255 LinkedList.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Feedback from Assignment 1
Smalltalk – a Pure (and first) OOPL
Introduction to Smalltalk
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Dictionary.
Smalltalk on a Dime.
Introduction to Computer Science
Introduction to Java Collection
Presentation transcript:

Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.

What are Collections? Collections provide the means for managing and manipulating groups of objects. Common collections include:  Array: fixed-size ordered group.  Linked List: dynamic-size ordered group. Insertions and removals are done in the extremes.  Set: an unordered group of unique objects.  Dictionary: like set, but each element is a key- value pair. Elements are accessed by their keys.  String: can be considered to be a special form of Array, where the elements must be characters.  Heaps and other forms of sorted collections, which require content that can be compared to itself.

Classification of Collections Collections differ from each other by their space complexity, the operations they support and the time complexity for each of these operations. Disregarding complexity, we can assign attributes to collections based on the operations they support: 1. Can we access items in the order they were inserted? Ordered (String) / Unordered (Set) 2. Can we change the collection size? Dynamic (LinkedList) / Fixed (Array). 3. Can we change the collection content? Mutable (Array) / Immutable (?). 4. Can the collection hold multiple copies of the same object? Yes (Bag) / No (Set). 5. What kind of objects can the collection hold? Must they all be of the same type (homogeneous) or not (heterogeneous)? 6. How do we access the items in the collection? By value, by index, by key? There are even more attributes, outside our scope…

Key Collection Classes in Squeak. 4 Set SequenceableCollection* OrderedCollection ArrayedCollection* Object* Collection* Interval String Dictionary Array This is only a partial view, Squeak class libraries contain over 90 collection classes! Bag

Some Collection Methods Are defined, redefined, optimized or forbidden (!) in subclasses Accessing size, capacity, at: anIndex, at: anIndex put: anElement Testing isEmpty, includes: anElement, contains: aBlock, occurrencesOf: anElement Adding add: anElement, addAll: aCollection Removing remove: anElement, remove: anElement ifAbsent: aBlock, removeAll: aCollection Enumerating do: aBlock, collect: aBlock, select: aBlock, reject: aBlock, detect: aBlock, detect: aBlock ifNone: aNoneBlock, inject: aValue into: aBinaryBlock Converting asBag, asSet, asOrderedCollection, asSortedCollection, asArray, asSortedCollection: aBlock Creation with: anElement, with:with:, with:with:with:, with:with:with:with:, withAll: aCollection

So how do I… ? To check how to create a collection / add to a collection / remove from a collection / access an item in a collection etc. just open the class in the System Browser and check its methods. The methods are conveniently organized in categories (for example, there’s a category called “adding”). If you can’t find the method you expected there, check in its super class! Many operations are shared between collections (specifically, many are inherited from Collection). We’ll go over the methods for converting and for enumerating.

Converting Send asSet, asBag, asSortedCollection etc. to convert between kinds of collections Send keys, values to extract collections from dictionaries Use various factory methods to build new kinds of collections from old kinds. – Dictionary newFrom: {1->#a. 2->#b. 3->#c}

Basic Enumeration The method do: aBlock is the most basic enumerating mechanism. The block must be a single-parameter block for invoking it with the current value. C++Smalltalk / Squeak int sum = 0; for (int i = 0 ; i < c.length() ; i++) sum += c.get(i); // c’s type must support the length() // and get(int) operators sum := 0. c do: [ :x | sum := sum + x] // Practically all collections support // do: so this will always work

Advanced Enumerations There are many additional, more advanced and very useful enumeration methods. Some of them are: collect:, like ML’s map: #(1 2 3) collect: [:x|x*x]  #(1 4 9) – The new collection will be of the same type of the old one. select:, like ML’s filter (it also has an opposite, reject: ): #(1 2 3) select: [:x|(x rem: 2) = 1]  #(1 3) – The new collection will be of the same type of the old one. inject:into:, which returns a scalar and is like folding in ML: #(1 2 3) inject: 1 into: [:total :next|total * next]  6 – Is this a left fold or a right fold?

Advanced Enumerations – Cont’d Confused? Check the code! All the advanced enumerations are based on do: and are very simple to read. For example, this is the implementation of occurancesOf: occurrencesOf: anObject ^self inject: 0 into: [ :x :y | ( y = anObject ) ifTrue: [ x + 1 ] ifFalse: [ x ] ]

For Loops Iterating over a collection is nice, but what if we really do want to iterate according to index, such as with C++ for loops? Smalltalk does not offer a straightforward way to do that. We can use the whileTrue: method of blocks, but a more convenient solution is to use the Interval collection class. An interval is basically an array of numbers, but its advantage is that it’s very easy to create one from numbers.

Interval Creation and Usage To create an interval, the simplest way is to use the to: or to:by: methods of Number : – 0 to: 10 instead of #( ) – 0 to: 10 by: 2 instead of #( ) – Yes, it’s inclusive Number also has the methods to:do: and to:by:do: which make things even shorter: C++Squeak for (int i = 0 ; i < 10 ; i += 2) buffer[i] = 0; 0 to: 9 by: 2 do: [:i | buffer at: i put: 0]