Skip to content

Instantly share code, notes, and snippets.

View jwmerrill's full-sized avatar

Jason Merrill jwmerrill

View GitHub Profile
run(`wget http://julialang.org/images/logo.png`)
# Open png file for reading
fp = ccall(:fopen, Ptr{Uint8}, (Ptr{Uint8}, Ptr{Uint8}), "logo.png", "r")
# Read first 8 bytes
initial_bytes = Array(Uint8, 8)
ccall(:fread, Ptr{Uint8}, (Ptr{Uint8}, Uint8, Uint8, Ptr{Uint8}), initial_bytes, 1, 8, fp)
# Check that we actually have a png file
@jwmerrill
jwmerrill / gist:6502014
Created September 9, 2013 21:50
Example of validating an e-mail address by looking up the SMTP server and seeing how it responds to RCPT TO: messages.
jason:knox jm843$ host gmail.com
gmail.com has address 74.125.239.53
gmail.com has address 74.125.239.54
gmail.com has IPv6 address 2607:f8b0:4010:801::1016
gmail.com mail is handled by 30 alt3.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 10 alt1.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 20 alt2.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 5 gmail-smtp-in.l.google.com.
gmail.com mail is handled by 40 alt4.gmail-smtp-in.l.google.com.
jason:knox jm843$ nc alt3.gmail-smtp-in.l.google.com 25
@jwmerrill
jwmerrill / a-series-representation-benchmark
Last active December 30, 2015 18:09
Benchmark of PowerSeries.jl
using PowerSeries
function series_loop()
accum = 0.0
for i = 1:10000
accum += sin(Series(1.0*i, 1.0)).ep
end
end
function dual_loop()
@jwmerrill
jwmerrill / fzero_vs_bisect_benchmark.jl
Created February 15, 2014 01:16
Benchmark simple bisection against Roots.fzero polyalgorithm
using Roots
# Alternative "mean" definition that operates on the binary representation
# of a float. Using this definition, bisection will never take more than
# 64 steps.
function _middle(x::Float64, y::Float64)
# Use the usual float rules for combining non-finite numbers
if !isfinite(x) || !isfinite(y)
return x + y
end
@jwmerrill
jwmerrill / gbm.jl
Last active June 29, 2018 04:49
Faster geometric brownian motion
function genS_jl(I)
s0 = 600.0
r = 0.02
sigma = 2.0
T = 1.0
M = 100
dt = T/M
a = (r - 0.5*sigma^2)*dt
b = sigma*sqrt(dt)
@jwmerrill
jwmerrill / viterbi_2.jl
Created September 21, 2014 18:02
Viterbi algorithm
const states = ["Healthy", "Fever"]
const obsnames = ["normal", "cold", "dizzy"]
const start_probability = [0.6, 0.4]
const transition_probability = [
0.7 0.3
0.4 0.6
]
@jwmerrill
jwmerrill / graythresh_kmeans.jl
Created November 10, 2014 02:18
Kmeans thresholding of a grayscale image
function graythresh_kmeans(img)
# Note, without the type annotation, this is slow because
# mean appears not to be type stable here
thresh = mean(img)::Gray{Float32}
while true
lastthresh = thresh
nplus = 0
nminus = 0
splus = zero(typeof(thresh))
@jwmerrill
jwmerrill / elm-runtime.diff
Last active August 29, 2015 14:09
Modification of elm's runtime to allow drawing more consistently at 60 fps
9345,9351c9345,9374
- var previousDrawId = 0;
- function domUpdate(newScene) {
- previousDrawId = ElmRuntime.draw(previousDrawId, function(_) {
- Render.update(elm.node.firstChild, savedScene, newScene);
- if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
- savedScene = newScene;
- });
+
+ // domUpdate is called whenever the main Signal changes. On domUpdate,
'use strict';
var Elm = {}; Elm.Native = {}; Elm.Native.Graphics = {};
var ElmRuntime = {}; ElmRuntime.Render = {};
Elm.Native.Array = {};
Elm.Native.Array.make = function(elm) {
elm.Native = elm.Native || {};
elm.Native.Array = elm.Native.Array || {};
if (elm.Native.Array.values) return elm.Native.Array.values;
if ('values' in Elm.Native.Array)
return elm.Native.Array.values = Elm.Native.Array.values;
jwm@jason ~/s/elm-platform> runhaskell src/BuildFromSource.hs master
Writing a default package environment file to
/Users/jwm/src/elm-platform/Elm-Platform/master/cabal.sandbox.config
Creating a new sandbox at /Users/jwm/src/elm-platform/Elm-Platform/master
Cloning into 'elm-compiler'...
remote: Counting objects: 17753, done.
remote: Compressing objects: 100% (100/100), done.
remote: Total 17753 (delta 56), reused 0 (delta 0)
Receiving objects: 100% (17753/17753), 5.44 MiB | 1.20 MiB/s, done.
Resolving deltas: 100% (10044/10044), done.