Skip to content

Instantly share code, notes, and snippets.

@showell
showell / scoping.coffee
Created December 22, 2011 22:20
scoping in CoffeeScript
# This code demonstrates how CS scoping works within a file. Scroll down to the end to
# see how cross-file scoping works.
# Here are the rules.
#
# Scoping is all lexical. Read the file from top to bottom to determine variable scopes.
#
# 1) When you encounter any variable in the top-level nesting, its scope is top level.
# 2) Inside a function, if you encounter a variable name that still exists in an outer scope, then that
# variable name refers to the variable in the outer scope. (This is "closure".)
@showell
showell / fib_rounding.py
Created April 24, 2023 12:59
Show rounding errors in Fibonacci series
import math
ROOT5 = math.sqrt(5)
PHI = (1 + ROOT5) / 2
def closed_fib(n):
v = (PHI**n) / ROOT5
return int(v)
@showell
showell / mit_license.html
Created August 26, 2012 17:59
MIT license vs. Public Domain
Paul Miller asked on Twitter: "Why use MIT license instead of public domain? The only difference is copyright notice inclusion requirement. Is it really needed?" I only know Paul from a few online interactions. His profile says that he is from the Ukraine. I am from the U.S.
I mostly agree with the spirit of Paul's question. The license doesn't really grant you many rights that aren't already covered by most modern democracies, whether it's written law or legal precedent. It's only a minor nuisance to include copyright notices from a purely pragmatic standpoint, but it's irksome nonetheless. When I include the MIT license with software that I wrote, I am first annoyed that the lawyers won. More importantly, I feel that including the license reframes the debate about free software in the wrong direction. In my opinion, software should be naturally free, and you would only ever include licenses with non-free software.
I would support a law that said all software was implicitly covered by the MIT licen
@showell
showell / file_system.py
Created November 11, 2012 02:38
python: file system abstracted as dictionary
import glob
import os
class Directory:
def __init__(self, path = '/'):
self.path = path
def __getitem__(self, key):
fn = os.path.join(self.path, key)
if os.path.exists(fn):
@showell
showell / heap.py
Created December 1, 2011 18:44
heapsort in Python
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def is_heap(a):
n = 0
m = 0
while True:
for i in [0, 1]:
m += 1
if m >= len(a):
@showell
showell / foo.md
Last active November 10, 2019 18:26
Elm values and functions

If you're coming from a language like or JavaScript (JS) or Python, you may used to be a mental model where functions (or lambdas) can actually have zero parameters. In those languages nothing gets evaluated until you invoke the functions.

zero parameters

Here's JS:

@showell
showell / foo.md
Last active November 10, 2019 18:17

If you're coming from a language like or JavaScript (JS) or Python, you may used to be a mental model where functions (or lambdas) can actually have zero parameters. In those languages nothing gets evaluated until you invoke the functions.

zero parameters

Here's JS:

@showell
showell / foo.txt
Created November 10, 2019 18:07
Elm functions vs. Elm values
If you're coming from a language like or JavaScript (JS) or Python, you may
used to be a mental model where functions (or lambdas) can actually have
zero parameters. In those languages nothing gets evaluated until you
invoke the functions.
## zero parameters
Here's JS:
~~~ js
@showell
showell / .bashrc
Created November 22, 2017 17:14
bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
@showell
showell / design.html
Created April 7, 2011 17:55
game of life w/functional JS style
<a href="game_of_life.html">Game of Life</a>
<h3>Computer Programs are...</h3>
Poetry for Robots? Maybe...but how about?
<ul>
<li>Algorithms</li>
<li>Configuration</li>