Skip to content

Instantly share code, notes, and snippets.

Map and HashMap

Learning objectives:

@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;
# 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 / 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

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.
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 ––––––
@pcantrell
pcantrell / assuming.md
Last active July 10, 2018 05:47
assuming

Goals

  • Create a standard way for devs to document what assumption led them to use an unsafe operation (e.g. force unwrap).
  • Cover all trapping operations (!, try!, as!, []) through a single mechanism. (Lack of this seems to have been Lattner’s primary beef with the previous proposal.)
  • Make this mechanism work as both:
    • developer documentation — clear, readable, and not excessively noisy in the code itself — and
    • runtime diagnostic, included in console / crash logs to expedite debugging.

Solution

// Siesta’s Resource class represents the current “best information available”
// about a RESTful resource. Other parts of the code can observe changes to
// that state (started loading, received new data, received error, etc):
public protocol ResourceObserver
{
func resourceChanged(_ resource: Resource, event: ResourceEvent)
// (plus other stuff I’m ignoring)
}