Skip to content

Instantly share code, notes, and snippets.

# How can we harmonize the types of a Square class and a Rectangle class
# so that it's possible to write a rescale function that works on both?
#
# As we saw in class Wed, if we’re doing this in Java, inheritance cannot
# solve this problem in a simple way: both `Square extends Rectangle`
# and `Rectangle extends Square` end up violating the Liskov Substitution
# Principle. While there is a nice “is-a” relationship here — all squares
# are rectangles! — that does not translate nicely into Java’s type system.
#
# In class today, we saw that having one inherit from the other doesn’t
@pcantrell
pcantrell / mastodon-macos.css
Last active December 8, 2023 20:35
Stylesheet to make Mastodon more macOS-like
body.app-body.layout-multiple-columns.theme-mastodon-light,
body.app-body.layout-multiple-columns.theme-default,
body.app-body.layout-multiple-columns.theme-contrast {
overflow: hidden !important;
}
body.app-body.layout-multiple-columns.theme-mastodon-light .columns-area,
body.app-body.layout-multiple-columns.theme-default .columns-area,
body.app-body.layout-multiple-columns.theme-contrast .columns-area {
margin: 0 -1px !important;

Turning programmer errors into type errors

An example problem

In Wordy, suppose that BinaryExpressionNode were designed like this, with the operator modeled as a string:

public class BinaryExpressionNode extends ExpressionNode {
    // Valid operator types include "addition", "subtraction", "multiplication",
 // "division", and "exponentiation". Other strings are not valid operators.
@pcantrell
pcantrell / metaprogramming.rb
Last active September 24, 2021 23:17
An example of metaprogramming in Ruby
# ------ Base class with metaprogramming ------
# This might make more sense if you skip ahead to the 🦄🦄🦄🌈🌈🌈 first
# and study the desired results, then come back here.
class Animal
def initialize(name)
@name = name
end
@pcantrell
pcantrell / gist:4113853
Created November 19, 2012 21:01
Property observers in Javascript
// Model with property observers:
Model = function() {}
Model.prototype.observe = function(prop, observer) {
this._observers = this._observers || {}
if(!this._observers[prop]) {
this._observers[prop] = []
@pcantrell
pcantrell / ruby-closures.rb
Created January 8, 2012 21:07
Closures in Ruby
# CLOSURES IN RUBY Paul Cantrell http://innig.net
# Email: username "cantrell", domain name "pobox.com"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments,
# then trying to guess the output of the code!
# A closure is a block of code which meets three criteria:
import Foundation
// WHY HAVE A TIMER SYNCED TO DISPLAY REFRESH?
//
// Sparked by this question from Nick Lockwood:
// https://twitter.com/nicklockwood/status/1131650052701786114
class TimerPhasingSimulation
{
// –––––– Heart of the simulation ––––––

Study these screenshots:

https://innig.net/tmp/nr-screenshots/

Fork this gist (one fork per team).

Sketch out an object model for this application. Don't worry about view, network, database, etc.; your concern is only to represent the essential information of this application as classes and attributes. You can use the following attribute types:

  • numbers, strings, dates
  • other model classes