Instantly share code, notes, and snippets.

View simple-bootstrapping.py
def bootstrap(np_array, func, n_samples=1000, size=None):
size = size if size else len(np_array)
return np.apply_along_axis(func, 1, np.random.choice(np_array, size=(n_samples, size)))
# Example use
bootstrap(some_array, np.mean)
View edit-distance.rkt
#lang racket
(module+ test
(require rackunit))
(module+ test
(check-equal? (editions "" "") empty)
(check-equal? (editions "a" "a") (list (op-match #\a)))
(check-equal? (editions "ab" "ab") (list (op-match #\a) (op-match #\b)))
(check-equal? (editions "a" "") (list (op-insert #\a)))
(check-equal? (editions "" "b") (list (op-delete #\b)))
View traffic.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from itertools import takewhile
from random import random
MAX_VELOCITY = 5
def space():
return " "
View rubocop.yml
AllCops:
TargetRubyVersion: 2.4
DisplayCopNames: true
DisplayStyleGuide: true
Style/FrozenStringLiteralComment:
AutoCorrect: true
Style/RegexpLiteral:
Exclude:
View define-pattern-example-of-use.rkt
(define-pattern (inner a) (list a (... ...))) ; This line defines a new pattern
(define inner-lists (list (list 1 2) (list 3 4))) ; Same as (define inner-lists '((1 2) (3 4)))
(match inner-lists
[(inner (list _ x _ ...)) (printf "x = ~a\n" x)]) ; Matches inner lists, returns (2 4)
View pattern-extension-macro-simple.rkt
(define-syntax-rule (define-pattern (name args ...) substitution)
(define-match-expander name
(syntax-rules ()
[(_ args ...) substitution])))
View pattern-extension-simple.rkt
(define-match-expander inner
(syntax-rules ()
[(inner x) (list x (... ...))]))
View generator-example.py
def squared(coll):
for x in coll:
yield x ** 2 # "yield makes this a generator
for x in squared(range(10)):
print(x)
View generators-to-the-rescue.py
from neo4j.v1 import GraphDatabase
import csv
def query(cypher):
with GraphDatabase.driver("bolt://localhost:7687") as driver:
with driver.session() as session:
with session.begin_transaction() as tx:
for record in tx.run(cypher):
yield record # <- Generator here!
View deep-nesting-example.py
from neo4j.v1 import GraphDatabase
import csv
cypher = "MATCH yadda-yadda-yadda"
header = ["x", "y"]
with open(file, "w") as output:
writer = csv.writer(output)
writer.writerow(headers)
with GraphDatabase.driver("bolt://localhost:7687") as driver: