Skip to content

Instantly share code, notes, and snippets.

@yuanyan
yuanyan / poisson.js
Created May 29, 2011 06:12
Poisson distribution
//Poisson distribution
//http://en.wikipedia.org/wiki/Poisson_distribution
function poisson(expectvalue){
var n = 0, //循环计数
limit = Math.exp(-expectvalue), // e -v, 其中v是期望值
x = Math.random(); //生成 0-1之间随机数
while(x > limit){
@yuanyan
yuanyan / un-nested
Created September 8, 2011 15:18
un-nested callback
// callback-nested
Database.connect("db",function(db){
db.open("table",function(table){
table.select("name",function(val){
console.log(val);
});
});
});
// un-nested
@yuanyan
yuanyan / gist:1240875
Created September 25, 2011 17:34
Object.clone
Object.clone(obj,deep){
if(obj == null || typeof(obj) != 'object')
return obj;
if(deep){
var temp = obj.constructor(); // changed
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}else{
@yuanyan
yuanyan / disqus.html
Created August 12, 2012 15:34
Third-party JavaScript
<script type="text/javascript">
var disqus_shortname = 'madscript'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
@yuanyan
yuanyan / node-fs.js
Last active December 18, 2015 17:39
var fs = require("fs");
var path = require("path");
// mian code, 请替换路径地址
ncp('./path/to/source', './path/to/dest', function(){
rmrf('./path/to/source');
})
// copy from ncp, 请折叠
function ncp (source, dest, options, callback) {
@yuanyan
yuanyan / console.js
Last active December 19, 2015 05:09
console API
// http://getfirebug.com/wiki/index.php/Console_API
// https://developers.google.com/chrome-developer-tools/docs/console-api#consolelogobject_object
// https://developer.mozilla.org/en-US/docs/Web/API/console
console.log("%cRed text, %cgreen text, %cblue text", "color:red", "color:green", "color:blue");
console.log("%c",
"font-size: 1px; padding: 130px 232px; line-height: 260px;background: url(http://i.imgur.com/oGiMR.gif); background-size: 464px 260px; color: transparent;")
var template = 'hi';
exports.run = function(){
template('hi')
}
exports.template = function(hi){
console.log(hi)
}
@yuanyan
yuanyan / atom-ruby-snippets.cson
Created March 4, 2014 11:38
Ruby Snippets for Atom
'.source.ruby':
'describe (String)':
'prefix': 'des'
'body': 'describe "${1:subject}" do\n $0\nend'
'describe (type)':
'prefix': 'dest'
'body': 'describe ${1:Type} do\n $0\nend'
'describe (type, string)':
'prefix': 'dests'
'body': 'describe ${1:Type}, "${2:description}" do\n $0\nend'
@yuanyan
yuanyan / fixow.sh
Created March 20, 2014 12:22
Fix Duplicate "Open With"
alias fixow='/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user;killall Finder;echo "Open With has been rebuilt, Finder will relaunch"'
@yuanyan
yuanyan / fixaudio.applescript
Last active November 20, 2015 15:55
fixaudio.applescript
on run {input, parameters}
do shell script "" with administrator privileges
set the rootPwd to text returned of (display dialog "请输入您的管理员密码:" default answer "" with hidden answer)
try
do shell script "echo " & rootPwd & "|sudo -S killall coreaudiod"
do shell script "echo " & rootPwd & "|sudo -S kextunload /System/Library/Extensions/AppleHDA.kext"
do shell script "echo " & rootPwd & "|sudo -S kextload /System/Library/Extensions/AppleHDA.kext"
do shell script "sudo -k" --logout root
display dialog "操作已经成功完成,您的系统应该能够恢复声音。
如果问题依旧,请直接重启系统。" buttons {"好"} with icon 1 with title "成功"