Skip to content

Instantly share code, notes, and snippets.

View emlyn's full-sized avatar

Emlyn Corrin emlyn

View GitHub Profile
@emlyn
emlyn / primes.clj
Last active March 23, 2016 23:24
Generate unbounded lazy sequence of primes in Clojure (and unbounded iterator of primes in Python)
#!/usr/bin/env inlein
'{:dependencies [[org.clojure/clojure "1.8.0"]]
:jvm-opts []}
;; to run, use inlein: https://github.com/hyPiRion/inlein (or paste into a repl)
(defn primes
"Generate a lazy infinite sequence of primes, using method from (improved to skip even numbers):
https://web.archive.org/web/20150710134640/http://diditwith.net/2009/01/20/YAPESProblemSevenPart2.aspx"
{"+auto switch":{"color":"#99dd99","defaultProfileName":"direct","name":"auto switch","profileType":"SwitchProfile","revision":"155a03f540b","rules":[{"condition":{"conditionType":"HostWildcardCondition","pattern":"*internal.swiftkey.com"},"profileName":"AWS"},{"condition":{"conditionType":"HostWildcardCondition","pattern":"ec2-*"},"profileName":"AWS"},{"condition":{"conditionType":"HostWildcardCondition","pattern":"ip-10-*"},"profileName":"AWS"},{"condition":{"conditionType":"HostWildcardCondition","pattern":"10.*"},"profileName":"AWS"}]},"-confirmDeletion":true,"-downloadInterval":1440,"-enableQuickSwitch":false,"-exportLegacyRuleList":false,"-monitorWebRequests":false,"-quickSwitchProfiles":["system","auto switch"],"-refreshOnProfileChange":true,"-revertProxyChanges":true,"-showInspectMenu":true,"-startupProfileName":"","schemaVersion":2,"+AWS":{"bypassList":[{"conditionType":"BypassCondition","pattern":"<local>"}],"color":"#dd3333","fallbackProxy":{"host":"localhost","port":8157,"scheme":"socks5"},"name":
@emlyn
emlyn / mml.rb
Last active December 15, 2015 09:58
Sonic Pi MML interpreter (inspired by https://gist.github.com/xavriley/87ef7548039d1ee301bb)
define :music do |txt, vol_scale=100|
octave = 5
length = 4
use_bpm 120
txt = txt.downcase.gsub(/^mml@| |;$/, '').gsub(/#/, '+').gsub(/w/, 'r')
while not txt.empty?
r = txt.scan(/^(.)([+-]?)(\d*)(\.?)(.*)$/)
cmd, opt, num, dot, txt = r[0]
#print cmd, opt, num
case cmd
use_bpm 94
live_loop :drums do
# 1 e & a 2 e & a 3 e & a 4 e & a
cymbal = bools(0,0,0,0, 0,1,0,1, 0,0,0,0, 0,1,0,0)
hard = bools(0,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0)
soft = bools(0,0,0,0, 0,0,0,1, 0,1,0,1, 0,0,0,1)
kick = bools(1,0,1,0, 0,0,0,0, 0,0,1,0, 0,1,0,0)
with_fx :reverb, room: 0.4, damp: 0.1, amp: 1.5 do
16.times do
@emlyn
emlyn / innovation1.rb
Created October 9, 2015 11:50
Sonic Pi
use_bpm 180
use_random_seed 42
live_loop :drum do
a,b = [[:Ab,:major], [:F, :minor], [:C, :minor], [:G, :minor]].choose
cue :key, a: a, b: b
if true then
with_fx :reverb do
4.times do
sample :drum_tom_mid_hard, amp: ring(4,2,3,2).tick
import os
try:
sc
except NameError:
from pyspark import SparkContext, SparkConf
sc = SparkContext(conf=SparkConf().setAppName("lol"))
dirs = ['/home/hadoop/.versions/spark-1.4.0.b/classpath/emrfs/',
'/home/hadoop/.versions/2.4.0-amzn-5/share/hadoop/common/lib/']
@emlyn
emlyn / dancing dream.rb
Last active February 15, 2018 09:16
Toby's Sonic Pi songs
use_bpm 120
use_synth :dsaw
p = [[:c4,:M7],[:a4,:m7],[:d4,:m7],[:g4,:dom7],[:e4,:m7],[:a4,:dom7],[:d4,:m7],[:g4,:dom7]]
in_thread do
sleep 8
12.times do
sample :loop_breakbeat, rate: sample_duration(:loop_breakbeat)/4
sleep 4
@emlyn
emlyn / emlyn.rb
Created July 1, 2015 23:13
From my first attempt at live coding, at the SwiftKey music night.
use_bpm 130
live_loop :tick do
root, type = [[:d4,:m7],[:g4,:dom7],[:c4,:M7],[:f4,:M7],[:b4,:'m7+5'],[:e4,:dom7],[:a4,:m7]].tick(:tick)
cue :boom, root: root, type: type
with_fx :reverb, amp: 2 do
sample :drum_cymbal_closed, amp: 3
sleep 0.5
3.times do
sample :drum_cymbal_closed
;; Doesn't work...
;; TODO: allow (truncate n (t/minutes 10)): "2014-06-27 12:34:56.789" -> "2014-06-27 12:30:00.000"
(defmacro mutator [method val]
`(fn [^MutableDateTime mdt#] (~method mdt# val)))
(defn truncate [^DateTime dt units]
(loop [^MutableDateTime mdt (.toMutableDateTime dt)
[[unit method] & more] [[t/years nil]
[t/months (mutator .setMonthOfYear 1)]
@emlyn
emlyn / base64_fixedpoint.py
Last active February 24, 2023 22:32
Base64 Fixed Point Calculator in Python
#!/usr/bin/env python3
import sys
def get6bits(bytes, offset):
"Get 6-bit value from offset in bits in a string"
byte_offset = offset // 8
bit_offset = offset % 8
if bit_offset <= 2:
# The 6 bits of input are all in the same byte: