Skip to content

Instantly share code, notes, and snippets.

@autotelicum
autotelicum / gist:1933036
Created February 28, 2012 15:16
Acme patch: Add Ctrl-keys in the same way as Cmd-keys for copy, paste, cut and undo.
--- text.c 2012-02-28 21:29:19.000000000 +0700
+++ text.bak.c 2012-02-28 20:51:03.000000000 +0700
@@ -745,12 +745,10 @@
q0++;
textshow(t, q0, q0, TRUE);
return;
- case 0x03: /* ^C: copy FIX */
case Kcmd+'c': /* %C: copy */
typecommit(t);
cut(t, t, nil, TRUE, FALSE, nil, 0);
@autotelicum
autotelicum / amb.coffee
Created October 25, 2011 04:06
Ambiguous amb function with permutations and eight queens solver
# Amb was first proposed by John McCarthy, the inventor of LISP, in 1963.
show = console.log
copy = (array) -> array.slice()
pick = (v) -> v[Math.floor Math.random() * v.length]
remove = (array, value) -> array.filter (v,i,o) -> v isnt value
fail = -> throw fail
@autotelicum
autotelicum / CoffeeLint.coffee
Created September 21, 2011 18:44
Simple check for non-ASCII identifiers
_ = require 'underscore'
cs = require('coffee-script').CoffeeScript
fs = require 'fs'
read = (f) -> if fs.statSync(f).isFile() then fs.readFileSync f, 'utf8'
tokens = cs.tokens read _.last process.argv
ids = _.uniq (token[1] for token in tokens when token[0] is 'IDENTIFIER')
console.log 'Identifiers that contain non-ASCII characters:'
console.log (id for id in ids when id.match /[^\w]/)
@autotelicum
autotelicum / gist:1225615
Created September 18, 2011 22:12
Y-combinator in CoffeeScript
show = console.log
Y = (f) -> ((g) -> g g) (h) -> (args...) -> f(h(h)) args...
factorial = Y (recurse) -> (x) -> if x is 0 then 1 else x * recurse x-1
show factorial 5
@autotelicum
autotelicum / gist:1191352
Created September 3, 2011 15:33
Python to CoffeeScript commands for the acme editor (sam command language)
Sam command helpers for acme-sac to avoid some manual
editing when converting Python to CoffeeScript.
Imperfect and incomplete, check manually after use.
1. Reduce indentation to half (4->2)
2. Remove colons at the end of common statements
3. Rename elif to else if
4. Change function definitions to arrow declarations
5. Change python ranges to array ranges
@autotelicum
autotelicum / gist:1155779
Created August 19, 2011 01:24
How do you add 's' to a word when there is more than one of something?
# How do you add 's' to a word when there is more than one of something?
# It is often seen in JavaScript to use the ternary operator but that is coding
# like it was PHP at the expense of future maintenance and internationalization.
# Only case 2 handles negative numbers.
show = console.log # console.debug # alert
test = (points) ->
@autotelicum
autotelicum / coffeekup-canvas-template.coffee
Created August 17, 2011 16:49
CoffeeKup HTML5 canvas template
# Template for CoffeeKup HTML5 document with canvas
kup = require 'coffeekup'
webpage = kup.render ->
doctype 5
html ->
head ->
meta charset: 'utf-8'
title 'WEBPAGE'
style '''
@autotelicum
autotelicum / samplesFunctional.coffee
Created August 1, 2011 18:43
CoffeeScript samples for Functional from Oliver Steele
# CoffeeScript samples for Functional from Oliver Steele
# -------------------------------------------------------------------
# Documentation: http://osteele.com/sources/javascript/functional/
# Repository: https://github.com/osteele/functional-javascript
# -------------------------------------------------------------------
# Directly import the Functional library since it is not CommonJS enabled
@autotelicum
autotelicum / partialFreeVars.coffee
Created August 1, 2011 15:18
Partial application with free vars
#!/usr/local/bin/node /Users/eh/bin/coffee
# Adjust according to install and chmod +x script
# Compatibility with both node.js and browsers
show = if exports? then console.log else alert
# Step-by-step derivation of a short version of
# partial function application with free vars.
# Inspired by http://blog.mirotin.net/tag/coffeescript