Name: Anonymous 2013-10-20 22:56
Post a random function that you made. May be from any project you've done or make one impromptu.
Any QUALITY is allowed. No bullying!
Any QUALITY is allowed. No bullying!
Storage.prototype.connect = function(callback) {
var self = this,
dbFileName = this.baseDir + "/database.db";
this.sequelize = new Sequelize(null, null, null, {
dialect: "sqlite",
storage: dbFileName
});
this.sequelize.authenticate().complete(function(err) {
if (err) {
return callback(err);
}
self.defineModels();
self.sequelize.sync().complete(callback);
});
};
Array.prototype.last = function() {
return this[this.length - 1];
};
//@unused
function discretize(min, max, pass) {
var len = (max - min)/pass + 1, arr = new Array(len);
for(var i = 0; i < len; i++) {
arr[i] = min + pass*i;
}
return arr;
}
function problem(params) {
return Math.pow(Math.E, -params.y);
}
function invert(f) {
return function(params) {
return 1/f(params);
};
}
function rk_form(f, h) {
return function(params) {
return h*f(params);
};
}
function next_K(accum, c, h, xn, yn, rk_f) {
accum.push(rk_f({
x: xn + c*h,
y: yn + c*accum.last()
}));
return accum;
}
function reduce_next_K(h, xn, yn, rk_f) {
return function(accum, c) {
return next_K(accum, c, h, xn, yn, rk_f);
};
}
function generate_Ks(f, h, xn, yn) {
var rk_f = rk_form(f, h);
return [0.5, 0.5, 1.0].reduce(
reduce_next_K(h, xn, yn, rk_f),
[rk_f({
x: xn,
y: yn
})]);
}
function sum_Ks(k_arr) {
return (k_arr[0] + 2*k_arr[1] + 2*k_arr[2] + k_arr[3])/6;
}
function runge_kutta_4_next(f, h, xn, yn) {
return sum_Ks(generate_Ks(f, h, xn, yn));
}
function runge_kutta_4(f, h, x0, xn, y0) {
var x = x0, y = y0;
console.log(x, " -> ", y);
while(x < xn) {
y += runge_kutta_4_next(f, h, x, y);
x += h;
console.log(x, " -> ", y);
}
}
(f = fopen(*++argv, "r")) || error("fopen", "Couldn't open %s", *argv);
open(...) || die $!
.
def _scan(self, chan, nick, msg):
store = json.load(open(FNAME))
delta = {}
for nick_ in re.findall('([a-zA-Z0-9]+)\+\+', msg):
nick_ = nick_.lower()
if nick_ == nick: continue
delta[nick_] = delta.get(nick_, 0) + 1
for nick_ in re.findall('([a-zA-Z0-9]+)\-\-', msg):
nick_ = nick_.lower()
if nick_ == nick: continue
delta[nick_] = delta.get(nick_, 0) - 1
for nick_ in sorted(delta.keys()):
store[nick_] = store.get(nick_, 0) + delta[nick_]
helpers.msg(self.client, chan, '%s now has %d points of karma.'
% (nick_.lower(), store[nick_]))
json.dump(store, open(FNAME, 'w'))
str[-1]
. But don't presume to tell the rest of us what to do just because you can't handle the fact that somewhere, somebody might be writing str[-1].I'm more worried about myself accidentally writing something like that. Well, not as obviously silly as that, of course, but still enough to cause terrible security issues.
*++a = *++b;
help solve real world problems. *++a = *a
real world problemsHere you go: You have a [set of] specific hardware target[s], and you have an algorithm in which you need to deal with a range of data, stored in a specific type of memory (think main memory vs flash or such). You want to access a piece of data, and you have a pointer to it, but if your pointer is `far away' from a known location that you just accessed from the hardware, it's faster to just recompute the data from registers, since the formula is simple and known. So you write code like this, where
loaded_data
and unknown_location
reside in this special memory area:void foo(short *loaded_data, short *unknown_location, short param_a,
short param_b, short *outp, size_t len)
{
/* ... */
skdist = loaded_data - unknown_location;
if (ABS(skdist) > SEEK_CUTOFF) {
/* rewrite to match your idea of `fast enough' as needed */
*outp = (param_a + (param_b * *loaded_data)) / (param_b + 5);
} else {
*outp = *unknown_location;
}
outp++;
unknown_location++;
/* ... */
}
foo
behaves the same in all cases, or at least it should write the same meaningful value to outp in all cases, despite invoking undefined behavior in its interior. From a correctness point of view, it's fine. I guess I was referring more to undefined behavior affecting correctness, although there are probably some reasonable examples there as well. Like atomic assignment. Although things like that should at least be wrapped in macros. HCF
instruction whenever it encountered undefined behaviour. Plugin.prototype.verifyNickname = function (msg: string, cb: boolean): void {
let con: object = this.con,
nickname: string = msg[0];
if (!nickname) cb(false);
let error: number = setTimeout(function(){cb(false)}, 3000);
con.raw('STATUS '+nickname);
con.once('status', function (nick: string, result: string){
clearTimeout(error);
if (~~result !== 3) cb(false);
else cb(true);
});
}
splitByHour =
let f h depth =
let hourOf = .time >> Date.fromTime >> Date.hour in
List.partition (hourOf >> (>) h) >> \(less, more) ->
if (h // 3) % 2 == 0 then
let d = depth // 2 in
Dict.union
(f (h - d) d less)
(f (h + d) d more)
else
Dict.fromList
[ h => List.sortBy .time more
, (h - 3) => List.sortBy .time less
]
in f 12 12
instance Functor PluginT where
fmap = undefined
instance Applicative PluginT where
pure x = undefined
(<*>) = undefined
instance Monad PluginT where
(>>=) = undefined