Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active December 18, 2019 23:00
Show Gist options
  • Save jki127/2ee4a6cfadcaa77e82eb4963bcc489e9 to your computer and use it in GitHub Desktop.
Save jki127/2ee4a6cfadcaa77e82eb4963bcc489e9 to your computer and use it in GitHub Desktop.

Pharo / Smalltalk - Programming Languages - Oct 10th, 2019

Table of Contents

Midterm Topics Overview:

  • Scope
    • dynamic
    • static
  • lifetime
  • calling conventions
    • pass by reference
    • pass by value
  • OOP things
    • Inheritance
    • Method Resolution
    • VMT
  • Tail-Call Recursion
  • Haskell
  • Recursion
  • Polymorphism
  • Higher-Order Functions
  • Types
  • Typeclasses
  • Laziness

Pharo Programming Language

  • Calling a method is known as sending a message

Math

Order of Operations is not kept unless you use parenthesis

3+2*2 "returns 10"
5 factorial.
|foo|
foo := 3*4+2
foo factorial.

Arrays

Indexes start at 1

|myarray|
myarray := #(4 2 1).
myarray.
anotherarray := {4. 3. 2. 1.}
concatarray := myarray , anotherarray.

Strings, Characters, Comments

"This is a comment"
|mystr|
mystr := 'here is some text'.
mystr at: 4. "equivalent to mystr[4] in python"
mystr at: 4 put: $q "puts q at index 4"

Reverse a string

mystr reverse.

Other methods

Every object supports the class method

4 class. "return SmallInteger"

@ method

(4 @ 9) class.

to: method

5 to: 20 "returns interval 5 through 20 inclusive"

Unary Messages

4 class.
'whatever' reverse.
9 negated.

Binary Messages

Binary messages always use symbols (ex: + , @)

3 + 4
'hello', 'world'
9 @ 2

Keyword Messages (everything else)

3 max: 9.
'hello' at: 2.
'hello' endsWith: lo.
'hello there' copyFrom: 2 to: 4. "returns ell"
mystr at: 3 put: $q.

Order of operations in Pharo

  1. Parentheses
  2. Unary
  3. Binary
  4. Keyword
  5. (from left to right)

Classes

Right-click on the leftmost pane in the System Browser to create a new class

Class Definition

Object subclass: #Person
	instanceVariables: 'name age'
	classVariableNames: ''
	package: 'Proglang'

Defining Methods

Age getter:

  • ^ represents return
age
	"answers the age of this person"
	| temporary variable names |
	^age

Age setter:

age: newAge
	age := newAge.

Name getter:

name
	^name.

isTooOld method:

  • We're using the age getter method instead of the age instance variable, but either one would work
isTooOld:
	^self age < 65

another implementation that checks size of a Person's name:

isTooOld:
	^(self age > 65) and: [self name size > 20].
|myperson|
myperson := Person new.
myperson age: 99; name; 'bob'.
myperson.

Hello World

|test|
test := [Transcript show: 'Hello World']
test value.

Block Closures

{ 'hello'. 'noodle'. 'spatula'. } do: [ :word | 
	Transcript show: 'The word of the day is ', word; cr.
]
5 to: 20 do: [:num | Transcript show: num]
|myarray|
myarray := { 'hello'. 'noodle'. 'spatula'. }

|mycoll|
mycoll := OrderedCollection new.
mycoll add: 3.
mycoll add: 9.
mycoll add: 23.

"alternative"
mycoll := OrderedCollection newFrom: #(3 9 23).

Conditionals

3 = 9 ifTrue: [ Transcript show: 'yes it is'; cr. ]
	  ifFalse: [Transcript show: 'no it isn't'; cr. ].

printString

  • an instance method printString defines how an object should be represented as a string

#proglang-f19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment