Skip to content

Instantly share code, notes, and snippets.

Set the Mac OS X SOCKS proxy on the command line

a.k.a. what to do when your ISP starts blocking sites :(

Set the SOCKS proxy to local SSH tunnel

networksetup -setsocksfirewallproxy "Ethernet" localhost 8080

To clear the domain and port

@404pnf
404pnf / koa.coffee
Last active August 29, 2015 14:19 — forked from geta6/koa.coffee
'use strict'
koa = require 'koa'
app = koa()
note = console.log
time_logger = (next) ->
start = Date.now()
yield next
note "耗时: #{Date.now() - start} 毫秒"
## Configure eth0
#
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=A4:BA:DB:37:F1:04
TYPE=Ethernet
BOOTPROTO=static
# treating a function as data
putter = ->(message) { puts message }
def deliver_message(message, handler)
handler.(message)
end
deliver_message("Hello, code as data!", putter)
# => Hello, code as data!
# Very (very) naive wordt segmentation algorithm for Chinese
# (or any language with similar characteristics, works at the
# character level.)
class Partitioner
attr_reader :ngrams
# +ngrams+ Enumerable list of ngrams
def initialize(ngrams, lookahead = 6)
@lookahead = lookahead
@ngrams = {}
class FizzBuzz
def fizz
loop do
["", "", "Fizz"].each{|s| yield s}
end
end
def buzz
loop do
["", "", "", "", "Buzz"].each{|s| yield s}
# require 'benchmark'
# # ==> true
# def ascending?(data, opts={})
# return false if data.nil?
# (data.size-1).times do |i|
# if block_given?
# return false if yield(data[i]) > yield(data[i+1])
# else
@404pnf
404pnf / gist:19b4ef978be621144253
Created May 26, 2014 06:59
ruby coll, ascending? and friends
# my proposal to functional-ruby gem
def in_order?(compare_fn)
-> col, &blk do
if blk
col.map {|e| blk[e] }
.each_cons(2)
.all? { |e1, e2| e1.send(compare_fn, e2) }
else
col.each_cons(2).all? { |e1, e2| e1.send(compare_fn, e2) }
@404pnf
404pnf / gist:10964044
Created April 17, 2014 08:19
for the sake of another way doing fizbuzz
# for the sake of another way doing fizbuzz
# must call fiz last since since it returns n by default
>> fiz = -> n { n % 3 == 0 ? 'fiz' : n }
=> #<Proc:0x00000101be66d8@(irb):11 (lambda)>
>> buzz = -> n { n % 5 == 0 ? 'buzz' : nil }
=> #<Proc:0x00000101bb7a68@(irb):12 (lambda)>
>> fizbuzz = -> n { n % 5 == 0 && n % 3 == 0 ? 'fizbuzz' : nil }
=> #<Proc:0x00000102065970@(irb):13 (lambda)>
>> (1..40).map { |e| fizbuzz[e] or buzz[e] or fiz[e] }
@404pnf
404pnf / timer
Last active January 4, 2016 02:19
ruby function to time a function
# found at <https://github.com/jdantonio/functional-ruby/blob/master/lib/functional/utilities.rb>
# Run the given block and time how long it takes in seconds. All arguments
# will be passed to the block. The function will return two values. The
# first value will be the duration of the timer in seconds. The second
# return value will be the result of the block.
#
# @param args [Array] zero or more arguments to pass to the block
#
# @return [Integer, Object] the duration of the operation in seconds and