Skip to content

Instantly share code, notes, and snippets.

@jsl
jsl / memoization_complexity.rb
Last active August 3, 2017 18:05
Memoization: premature optimization is the root of (at least some) evil
require 'minitest/autorun'
# Many Ruby programmers have a tendency to use memoization too frequently.
# Memoization, when used correctly, can add value to some code by making it
# faster. However, we should remember that pure functions - ones that only rely
# on inputs to produce their result are the simplest. Memoizing, where we save
# state in the form of calculated results, makes our functions impure, and we
# have to be careful that we're not introducing bugs.
describe "how memoization adds extra complexity, and why it should usually be avoided" do
diff --git a/src/Data/Diffusion/Common.hs b/src/Data/Diffusion/Common.hs
index a86307a..a8d1850 100644
--- a/src/Data/Diffusion/Common.hs
+++ b/src/Data/Diffusion/Common.hs
@@ -18,6 +18,8 @@ import Data.Hashable (Hashable())
import Data.Typeable (Typeable())
import Data.Binary (Binary())
import Control.DeepSeq (NFData)
+import Data.List (find)
+import Data.List (isPrefixOf)
@jsl
jsl / trendnet.md
Last active February 1, 2016 00:32
Using Curl to scan to preset positions in a Trendnet TV-IP6721PI

Repositioning your Trendnet TV-IP6721PI using Curl

It's easy to drive your TrendNet camera using curl. This is how you can make it scan to different preset positions.

Note that Curl must be used in Digest mode.

Assuming you have a ~/.netrc file containing your device username and password, first fetch the preset elements (look for the xml section).

curl --digest -n http://camera.local/eng/liveView.cgi
#!/usr/bin/env sh
GHC_VERSION_STRING=`ghc --version 2> /dev/null`
if [ $? -ne 0 ]
then
echo "Unable to obtain GHC version!"
exit 1
fi
@jsl
jsl / gist:6307960c0cbcd5e01b9e
Created November 20, 2014 16:50
Script for gathering data from Meetup
#!/usr/bin/env ruby
require 'rmeetup'
require 'chronic'
puts "Must define meetup url name as ARGV[0]!" if ARGV[0].nil?
CLIENT = RMeetup::Client.new do |config|
config.api_key = "XXXXXXXXX"
end
@jsl
jsl / gist:995ccbe928e50635a5d4
Created August 12, 2014 03:51
Ruby generated file from Idris -> Ruby backend
#!/usr/bin/env ruby
# Example generated from https://github.com/edwinb/idris-demos/blob/master/RLE/rle-vect.idr.
class I_VM
attr_accessor :valstack, :valstack_top, :valstack_base, :ret, :callstack
def initialize()
@valstack = []
@valstack_top = 0
@valstack_base = 0
@jsl
jsl / fix_osx_build.patch
Created February 16, 2014 16:01
Fix HaXml build on OS X
1 patch for repository http://code.haskell.org/HaXml:
Sun Feb 16 10:47:50 ECT 2014 Justin Leitgeb <justin@stackbuilders.com>
* Fix compilation on Mac OS X
The build of HaXml is currently broken on Mac OS X. Since a lot of packages
depend on HaXml this is quite inconvenient. Below is a description of the
problem that was previously posted to Stack Overflow:
http://stackoverflow.com/questions/20497348/haxml-installation-issues-on-os-x-10-9-mavericks
@jsl
jsl / replacer.rb
Last active August 29, 2015 13:56
Dictionary Replacer
require 'minitest/autorun'
require 'stringio'
class DictionaryReplacer
MARK = '$'
ESCAPE = '\\'
def initialize(to_replace, dictionary)
@to_replace = StringIO.new(to_replace)
@jsl
jsl / gist:8668369
Created January 28, 2014 14:11
Running microKanren in Guile
; You need to load a library for the assp function to run the microKanren code in Guile:
(import (rnrs))
(load "microKanren.scm")
(load "miniKanren-wrappers.scm")
(load "microKanren-test-programs.scm")
((call/fresh (lambda (q) (== q 5))) empty-state)
; $3 = ((((#(0) . 5)) . 1))