Skip to content

Instantly share code, notes, and snippets.

@jmdeldin
jmdeldin / lazy.rb
Created December 15, 2014 22:55
ruby laziness
require 'benchmark/ips'
START = 1
FINISH = 1_000_000
HALF = FINISH / 2
Benchmark.ips do |x|
x.report('lazy') do
(START..FINISH).lazy.include?(HALF)
end
@jmdeldin
jmdeldin / sum.ml
Created May 8, 2015 23:17
List summation
let sum lst =
let rec aux acc rest = match rest with
| [] -> acc
| x::xs -> aux (x+acc) xs in
aux 0 lst;;
sum [3;1;4;1;5;9];;
@jmdeldin
jmdeldin / fd3.sh
Created July 24, 2015 22:02
Using file descriptor 3
#! /bin/sh
echo "$0 [FILE] -- print a message to STDOUT or FILE" >&2
if [ -z $1 ]; then
exec 3>&1 # stdout
else
exec 3>"$1" # file
fi
(defun make-password ()
(interactive)
(let* ((cmd "egrep '{6,}' /usr/share/dict/words | shuf -n 4 | tr \"\\n\" ' '")
(pass (replace-regexp-in-string " $" "" (shell-command-to-string cmd))))
(message pass)
(kill-new pass)))
class Book
include Neo4j::ActiveNode
property :title
has_many :in, :spaces, rel_class: :BookAuthor
end
class Author
include Neo4j::ActiveNode
module StringStripper
def strip(*keys)
before_validation do
keys.each do |k|
self[k] = self[k].to_s.gsub(/[[:space:]]/, '') if self[k].present?
end
end
end
end
def foo(arg1:, arg2: :foo)
puts "arg1 = %s, arg2 = %s" % [arg1, arg2]
end
foo(arg1: "HI")
foo(arg1: "HI", arg2: "MOM")
foo(**{arg1: "HI", arg2: "MOM"})
(defun jm/yeller ()
(interactive)
(set-background-color "#FFFFDD"))
(defun jm/minty ()
(interactive)
(set-background-color "#F0FFF0"))
class Book
include Neo4j::ActiveNode
property :title
has_many :in, :spaces, rel_class: :BookAuthor
end
class Author
include Neo4j::ActiveNode
$grid = Array.new(10, "X").map { |r| Array.new(10, "X") }
def $grid.print
puts map(&:join)
end
def $grid.draw(row, col, size: 2)
bounds = $grid.length
fail "out of bounds #{row}+#{size} > #{bounds}" if row+size > bounds
shape = ' '