Skip to content

Instantly share code, notes, and snippets.

@fsword
fsword / fiber_exam.rb
Created June 6, 2017 16:06
fiber练习,模拟管道功能
View fiber_exam.rb
class PipeElement
attr_accessor :source
def initialize
@delegator = Fiber.new do
process
end
end
def | other
@fsword
fsword / wrapper_sample.py
Created November 22, 2013 05:50
python decorate example
View wrapper_sample.py
#!/usr/bin/env python
def dec(fn):
def inner_fn(*args):
result = fn(*args)
print "the result is: " + result
return result
return inner_fn
@fsword
fsword / ipctest.erl
Created November 21, 2013 04:49
erlang 虚拟机简单测试
View ipctest.erl
-module(ipctest).
-export([oneway/0, consumer/0, pingpong/0]).
oneway() ->
N = 10000000,
Pid = spawn(ipctest, consumer, []),
Start = erlang:now(),
dotimes(N - 1, fun () -> Pid ! message end),
Pid ! {done, self()},
receive ok -> ok end,
@fsword
fsword / Misc.java
Created September 23, 2013 19:23
左耳朵耗子的面试题StrToInt 举例(没涉及场景,因此先用最简单的方式实现,不考虑字符串超长等情况)
View Misc.java
public class Misc {
public static int[] StrToInt(String str) {
if(str == null) return new int[0];
int[] array = new int[str.codePointCount(0,str.length())];
for(int i=0; i < array.length; i++){
array[i] = str.codePointAt(i);
}
return array;
}
}
@fsword
fsword / reduce_sample.rb
Created August 27, 2013 13:30
reduce sample
View reduce_sample.rb
class Order
attr_accessor :type, :sub_orders
def initialize type
self.type = type
self.sub_orders = []
end
# 统计订单及其包含子订单的数量,按照订单类型归类
def count_items
result = sub_orders.reduce({}) do |result, order|
@fsword
fsword / multi_try.erl
Last active December 10, 2015 02:28 — forked from anonymous/multi_try.erl
View multi_try.erl
-module(multi_try).
-export([dothese/1]).
dothese(FuncArray) ->
Me = self(),
lists:foreach(fun(F) -> spawn(fun() -> F(Me) end) end, FuncArray),
spawn(fun() -> timer:sleep(200), Me ! timeout end),
Result = get_result("",length(FuncArray)),
io:format("result: ~p~n", [Result]).
@fsword
fsword / gist:4161295
Created November 28, 2012 13:29 — forked from acwright/gist:1944639
Sinatra / ActionMailer / Sendgrid / Heroku
View gist:4161295
require 'sinatra'
require 'action_mailer'
class Mailer < ActionMailer::Base
def contact
mail(
:to => "test@example.com",
:from => "test@example.com",
:subject => "Test") do |format|
format.text
@fsword
fsword / gist:3876987
Created October 12, 2012 02:25
ruby tailcall optimization
View gist:3876987
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
eval <<end
def fact(n, result = 1)
if n == 1
result
else
View gist:3203913
def foo
111
rescue
222
ensure
333
end
puts foo # got 111
View gist:3058698
module MyTool
module Tree
class << self
def loop root, &block
return false unless File.exist? root
block_given? ? exec(root, &block) : collect(root)
end
def exec root, &block