Skip to content

Instantly share code, notes, and snippets.

@fsword
fsword / fiber_exam.rb
Created June 6, 2017 16:06
fiber练习,模拟管道功能
class PipeElement
attr_accessor :source
def initialize
@delegator = Fiber.new do
process
end
end
def | other
@fsword
fsword / registry1.ex
Created August 15, 2014 09:29
exactor
defmodule Registry1 do
use ExActor.GenServer
definit do: initial_state(HashDict.new)
defcast create(name), state: names, do: put_name(names,name)
defcall lookup(name), state: names, do: reply(HashDict.fetch(names,name))
def put_name(names, name) do
@fsword
fsword / sample.py
Created July 23, 2014 06:27
sample code to explain the fail design of default argument support in python
class Member:
def __init__(self,name='',group=''):
self.name = name
self.group = group
class Team:
def __init__(self, names):
self.members = [Member(n,str(hash(self))) for n in names]
def join_us(self,member=Member()):
self.members.append(member)
@fsword
fsword / y.escript
Last active August 29, 2015 14:03
Y Combinator
#!/usr/bin/env escript
main(_Args) ->
Y = fun(Generator) ->
Gen = fun(X) ->
fun(Args) ->
(Generator(X(X)))(Args)
end
end,
Gen(Gen)
@fsword
fsword / output.py
Created June 26, 2014 10:35
python stdout auto flush
import time,sys,os
# usage:
# ssh localhost 'python <this_file>'
def test():
j = True
while j:
time.sleep(1)
print("Hello:"+str(j))
print "Bye"
@fsword
fsword / ssh_test.erl
Created May 26, 2014 06:54
验证ssh库的缺省配置能获得多少数据?
-module(ssh_test).
-export([go/0]).
go() ->
ssh:start(),
Host = "127.0.0.1",
Port = 22,
Options = [{user,"john"},
{silently_accept_hosts, true},
{user_interaction, false},
@fsword
fsword / .gitignore
Last active August 29, 2015 13:58
simple telnet server
*.swp
*.beam
@fsword
fsword / wrapper_sample.py
Created November 22, 2013 05:50
python decorate example
#!/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 虚拟机简单测试
-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 举例(没涉及场景,因此先用最简单的方式实现,不考虑字符串超长等情况)
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;
}
}