Skip to content

Instantly share code, notes, and snippets.

@jcinnamond
jcinnamond / Crabs.hs
Created December 24, 2020 11:28
aoc23 in haskell
module Crabs
( Cup, Current, Position
, simulate
, readCircle
) where
import Data.Tuple ( swap )
import Data.List (elemIndex )
-- Everything is an int, so create some type aliases to make the argument lists
Pry.config.prompt = [proc { "ruby> " },
proc { " | " }]

In my Extreme Object-Oriented Ruby talk at Full Stack Fest there was a bug in the live coding at around 29 minutes. When I was running the code it was failing with undefined method 'then' for false:FalseClass.

The source of the bug is quite subtle. The boolean operator starts with:

If.new(threat_level == Four)

That looks like it should work, but ten minutes earlier (when I'd been writing the comparisons for numbers) I forgot to save the file before moving on. The live evaluation worked when I was looking at numbers because it copies the content from my editor, runs it through xmpfilter and then puts the result back into the editor, so it doesn't require me to save the file. But later on when I was using the number for this comparison it became a problem. The comparison fell back to Ruby's built-in behaviour for == which, in this case, was returning Ruby's built in false rather than an instance of the False class that I had written.

# Processes are running, they run forever, never die, and no new
# processes get spawned. Each process eats memory at a constant,
# individual rate - process p_i (with 0 <= i < n) consumes 1 byte
# after every d(p_i) seconds. The total amount of available disk space
# is denoted by X.
#
# For each given input configuration (read from stdin), calculate the
# ETA in seconds. HINT - it is not the average A configuration is
# encoded as a single line like this:
#
class Zero
def pred
self
end
def succ
Number.new(self)
end
def +(other)
package main
import (
"fmt"
)
type Fizzbuzzer interface {
next() Fizzbuzzer
String() string
}
module BasicContract
def self.included(base)
contract = Contract.new(base)
contract.require :ping
contract.require :pong => [:name]
contract.require :pung
contract.check!
end
end
;; Alignment for ruby 1.9 style hashes
;; From:
;; h = {
;; key1: "v1",
;; longer_key: "v2",
;; }
;; To:
;; h = {
;; key1: "v1",
;; longer_key: "v2",
@jcinnamond
jcinnamond / go-play-45092f22.go
Created September 29, 2013 16:35
Go json and types
package main
import (
"encoding/json"
"errors"
"fmt"
"reflect"
)
func compAny(a, b interface{}) bool {
@jcinnamond
jcinnamond / go.el
Created August 20, 2013 07:28
Local Go playground in emacs
(defun go-create-playground ()
"Creates a new temporary file with a skeletal Go application"
(interactive)
(let ((filename (make-temp-file "go-play-" nil ".go")))
(find-file filename)
(rename-buffer (generate-new-buffer-name "Go Playground"))
(insert (concat "package main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(\"This file is located in " filename "\")\n}"))
(save-buffer)
(previous-line)
(end-of-line)