- 1
IT Оффтоп #113
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+2
IT Оффтоп #113
#83: https://govnokod.ru/27296 https://govnokod.xyz/_27296
#84: https://govnokod.ru/27336 https://govnokod.xyz/_27336
#85: https://govnokod.ru/27381 https://govnokod.xyz/_27381
#86: https://govnokod.ru/27405 https://govnokod.xyz/_27405
#87: https://govnokod.ru/27429 https://govnokod.xyz/_27429
#88: https://govnokod.ru/27432 https://govnokod.xyz/_27432
#89: https://govnokod.ru/27435 https://govnokod.xyz/_27435
#90: https://govnokod.ru/27439 https://govnokod.xyz/_27439
#91: https://govnokod.ru/27449 https://govnokod.xyz/_27449
#92: https://govnokod.ru/27460 https://govnokod.xyz/_27460
#93: https://govnokod.ru/27463 https://govnokod.xyz/_27463
#94: https://govnokod.ru/27466 https://govnokod.xyz/_27466
#95: https://govnokod.ru/27473 https://govnokod.xyz/_27473
#96: https://govnokod.ru/27478 https://govnokod.xyz/_27478
#97: https://govnokod.ru/27484 https://govnokod.xyz/_27484
#98: https://govnokod.ru/27495 https://govnokod.xyz/_27495
#99: https://govnokod.ru/27504 https://govnokod.xyz/_27504
#100: https://govnokod.ru/27508 https://govnokod.xyz/_27508
#101: https://govnokod.ru/27511 https://govnokod.xyz/_27511
#102: https://govnokod.ru/27518 https://govnokod.xyz/_27518
#103: https://govnokod.ru/27526 https://govnokod.xyz/_27526
#104: https://govnokod.ru/27534 https://govnokod.xyz/_27534
#105: https://govnokod.ru/27544 https://govnokod.xyz/_27544
#106: https://govnokod.ru/27552 https://govnokod.xyz/_27552
#107: https://govnokod.ru/27554 https://govnokod.xyz/_27554
#108: https://govnokod.ru/27557 https://govnokod.xyz/_27557
#109: https://govnokod.ru/27581 https://govnokod.xyz/_27581
#110: https://govnokod.ru/27610 https://govnokod.xyz/_27610
#111: https://govnokod.ru/27644 https://govnokod.xyz/_27644
#112: https://govnokod.ru/27648 https://govnokod.xyz/_27648
+2
Complex numbers:
>> Complex()({r: 2, i: 0} / {r: 1, i: 1} + {r: -3, i: 2}))
<- {r: -2, i: 1}
Automatic differentiation:
Let f(x) = x^3 - 5x:
>> var f = x => Dual()(x * x * x - {x:5, dx:0} * x);
Now map it over some values:
>> [-2,-1,0,1,2].map(a=>({x:a,dx:1})).map(f).map(a=>a.dx)
<- [ 7, -2, -5, -2, 7 ]
i.e. f'(x) = 3x^2 - 5.
Polynoomials:
>> Poly()([1,-2,3,-4]*[5,-6]).map((c,p)=>''+c+'x^'+p).join(' + ')
<- "5x^0 + -16x^1 + 27x^2 + -38x^3 + 24x^4"
В ЙажаСцрипт завезли перегрузку операторов.
https://gist.github.com/pyrocto/5a068100abd5ff6dfbe69a73bbc510d7
+2
// https://wandbox.org/permlink/rAilQ54oYBNsHJ3W
struct blob_p(T,alias t_xmalloc,alias t_free)
{
blob!(T)* bl_p;
size_t
getlen
(
) @trusted
in
{
assert(bl_p != null);
}
do
{
return bl_p.len;
}
T*
getdata
(
) @trusted
in
{
assert(this.bl_p != null);
}
do
{
return cast(T*)bl_p.data;
}
static bool
cmp
(
typeof(this) a,
typeof(this) b
) @trusted
in
{
assert(a.bl_p != null);
assert(b.bl_p != null);
}
do
{
if (a.bl_p.len != b.bl_p.len)
{
return false;
}
if(memcmp(cast(void*)a.bl_p.data, cast(void*)b.bl_p.data, a.bl_p.len * T.sizeof) != 0)
{
return false;
}
return true;
}
bool
cmp
(
typeof(this) a
) @trusted
in
{
assert(a.bl_p != null);
assert(this.bl_p != null);
}
do
{
if (a.bl_p.len != this.bl_p.len)
{
return false;
}
if(memcmp(cast(void*)a.bl_p.data, cast(void*)bl_p.data, a.bl_p.len * T.sizeof) != 0)
{
return false;
}
return true;
}
T opIndex(size_t i)
in
{
assert(bl_p != null);
assert(bl_p.len > i);
}
do
{
return getdata()[i];
}
~this()
/*in
{
assert (cast(void*)bl_p != null);
}
do*/
{
t_free(cast(void*)bl_p);
}
Попробовал написать на "D" своего рода "массив" с известно каким размером
+2
Steps to reproduce:
var s = "a huge, huge, huge string...";
s = s.substring(0, 5);
Expected results: s takes five bytes of memory, plus some overhead.
Actual results: s takes a huge, huge, huge amount of memory.
Unfortunately, most String functions use substring() or no-ops internally: concatenating with empty string, trim(), slice(), match(), search(), replace() with no match, split(), substr(), substring(), toString(), trim(), valueOf().
My workaround is:
function unleakString(s) { return (' ' + s).substr(1); }
But it's not satisfying, because it breaks an abstraction and forces me to think about memory allocation.
https://bugs.chromium.org/p/v8/issues/detail?id=2869
Status: Assigned (Open)
Reported on: Sep 3, 2013
+2
class IntIter {
constructor(private i = 0) {}
next() {
type retType = [value: typeof this.i, done: boolean];
if (this.i < 10) {
return <retType>[this.i++, false];
}
return <retType>[this.i, true];
}
}
function main() {
let it = new IntIter();
for (const o of it)
{
print(o);
}
for (const o of "Hello")
{
print(o);
}
}
добавил поддержку ForOf для ES2015 и ES3; причем компилятор сам определяет какой вариант лучше юзать
+2
if($account['lvl']=="1"){ $exp=round($account['exp']*100/52);}
if($account['lvl']=="2"){ $exp=round((($account['exp']-52)/(110))*100,2);}
if($account['lvl']=="3"){ $exp=round((($account['exp']-135)/(832-135))*100,2);}
if($account['lvl']=="4"){ $exp=round((($account['exp']-832)/(3547-832))*100,2);}
if($account['lvl']=="5"){ $exp=round((($account['exp']-3547)/(9658-3547))*100,2);}
if($account['lvl']=="6"){ $exp=round((($account['exp']-9658)/(15478-9658))*100,2);}
if($account['lvl']=="7"){ $exp=round((($account['exp']-15478)/(18478-15478))*100,2);}
if($account['lvl']=="8"){ $exp=round((($account['exp']-18478)/(30789-18478))*100,2);}
if($account['lvl']=="9"){ $exp=round((($account['exp']-30789)/(72394-30789))*100,2);}
if($account['lvl']=="10"){ $exp=round((($account['exp']-72394)/(138789-72394))*100,2);}
if($account['lvl']=="11"){ $exp=round((($account['exp']-138789)/(214787-138789))*100,2);}
if($account['lvl']=="12"){ $exp=round((($account['exp']-214787)/(398747-214787))*100,2);}
if($account['lvl']=="13"){ $exp=round((($account['exp']-398747)/(587058-398747))*100,2);}
if($account['lvl']=="14"){ $exp=round((($account['exp']-587058)/(824585-587058))*100,2);}
if($account['lvl']=="15"){ $exp=round((($account['exp']-824585)/(1247858-824585))*100,2);}
if($account['lvl']=="16"){ $exp=round((($account['exp']-1247858)/(1558789-1247858))*100,2);}
if($account['lvl']=="17"){ $exp=round((($account['exp']-1558789)/(1985478-1558789))*100,2);}
if($account['lvl']=="18"){ $exp=round((($account['exp']-1985478)/(2245857-1985478))*100,2);}
if($account['lvl']=="19"){ $exp=round((($account['exp']-2245857)/(2785896-2245857))*100,2);}
if($account['lvl']=="20"){ $exp=round((($account['exp']-2785896)/(3685478-2785896))*100,2);}
if($account['lvl']=="21"){ $exp=round((($account['exp']-3685478)/(4169875-3685478))*100,2);}
if($account['lvl']=="22"){ $exp=round((($account['exp']-4169875)/(5125478-4169875))*100,2);}
if($account['lvl']=="23"){ $exp=round((($account['exp']-5125478)/(5999999-5125478))*100,2);}
if($account['lvl']=="24"){ $exp=round((($account['exp']-5999999)/(7145877-5999999))*100,2);}
if($account['lvl']=="25"){ $exp=round((($account['exp']-7145877)/(8791755-7145877))*100,2);}
if($account['lvl']=="26"){ $exp=round((($account['exp']-8791755)/(10691755-8791755))*100,2);}
if($account['lvl']=="27"){ $exp=round((($account['exp']-10691755)/(12791755-10691755))*100,2);}
if($account['lvl']=="28"){ $exp=round((($account['exp']-12791755)/(15191755-12791755))*100,2);}
if($account['lvl']=="29"){ $exp=round((($account['exp']-15191755)/(18091755-15191755))*100,2);}
if($account['lvl']=="30"){ $exp=round((($account['exp']-18091755)/(21191755-18091755))*100,2);}
if($account['lvl']=="31"){ $exp=round((($account['exp']-21191755)/(24491755-21191755))*100,2);}
if($account['lvl']=="32"){ $exp=round((($account['exp']-24491755)/(27991755-24491755))*100,2);}
if($account['lvl']=="33"){ $exp=round((($account['exp']-27991755)/(31691755-27991755))*100,2);}
if($account['lvl']=="34"){ $exp=round((($account['exp']-31691755)/(35791755-31691755))*100,2);}
if($account['lvl']=="35"){ $exp=round((($account['exp']-35791755)/(40391755-35791755))*100,2);}
if($account['lvl']=="36"){ $exp=round((($account['exp']-40391755)/(45591755-40391755))*100,2);}
if($account['lvl']=="37"){ $exp=round((($account['exp']-45591755)/(51491755-45591755))*100,2);}
if($account['lvl']=="38"){ $exp=round((($account['exp']-51491755)/(58191755-51491755))*100,2);}
if($account['lvl']=="39"){ $exp=round((($account['exp']-58191755)/(65791755-58191755))*100,2);}
if($account['lvl']=="40"){ $exp=round((($account['exp']-65791755)/(74391755-65791755))*100,2);}
if($account['lvl']=="41"){ $exp=round((($account['exp']-74391755)/(83991755-74391755))*100,2);}
if($account['lvl']=="42"){ $exp=round((($account['exp']-83991755)/(94591755-83991755))*100,2);}
if($account['lvl']=="43"){ $exp=round((($account['exp']-94591755)/(106191755-94591755))*100,2);}
if($account['lvl']=="44"){ $exp=round((($account['exp']-106191755)/(118791755-106191755))*100,2);}
if($account['lvl']=="45"){ $exp=round((($account['exp']-118791755)/(132391755-118791755))*100,2);}
if($account['lvl']=="46"){ $exp=round((($account['exp']-132391755)/(146991755-132391755))*100,2);}
if($account['lvl']=="47"){ $exp=round((($account['exp']-146991755)/(162591755-146991755))*100,2);}
if($account['lvl']=="48"){ $exp=round((($account['exp']-162591755)/(179191755-162591755))*100,2);}
if($account['lvl']=="49"){ $exp=round((($account['exp']-179191755)/(196791755-179191755))*100,2);}
if($account['lvl']=="50"){ $exp=round((($account['exp']-196791755)/(215391755-196791755))*100,2);}
Расчет % заполнения шкалы уровня в зависимости от опыта
+2
wrapOnException(() -> file.writeTo(env.getFiler()));
https://www.youtube.com/watch?v=nCkpzqqog4k
+2
type int = 1;
function makeRangeIterator(start = 0, end = 10000, step = 1) {
print("makeRangeIterator.");
let nextIndex = start;
let iterationCount = 0;
const rangeIterator = {
next() {
let result: [value: int, done: boolean];
if (nextIndex < end) {
result = [nextIndex, false];
nextIndex += step;
iterationCount++;
return result;
} else {
result = [iterationCount, true];
}
return result;
},
};
return rangeIterator;
}
function main() {
let it = makeRangeIterator(1, 10, 2);
let result = it.next();
while (!result.done) {
print(result.value); // 1 3 5 7 9
result = it.next();
}
print("done.");
}
Ну вот и все... позвольте мне представить самый сложный кусок когда либо компилированный моей программой. но ввиду того что "трамплины" хрен знает как работают то придется этот код "забанить" до лучших времен. Но он рабочий
+2
class S
{
print()
{
print("Hello World");
}
}
interface IPrn
{
print();
}
function run(iface:IPrn)
{
iface.print();
}
function main() {
const s = new S();
let iface = <IPrn>s;
iface.print();
run(s);
}
короче новый говнокод подоспел. Т.к. вы все тут самые умные я не раскажу в чем фича. Сами догадаетесь
+2
// https://github.com/seanbaxter/circle/blob/master/examples/README.md#tldr
// ...
// Circle's primary syntactic element is the @meta keyword, which runs the prefixed statement
// during source translation (or during template instantiation in dependent contexts).
// https://github.com/seanbaxter/circle/blob/master/examples/README.md#same-language-reflection
// duff1.cxx
void duff_copy1(char* dest, const char* source, size_t count) {
const char* end = source + count;
while(size_t count = end - source) {
switch(count % 8) {
case 0: *dest++ = *source++; // Fall-through to case 7
case 7: *dest++ = *source++; // Fall-through to case 6...
case 6: *dest++ = *source++;
case 5: *dest++ = *source++;
case 4: *dest++ = *source++;
case 3: *dest++ = *source++;
case 2: *dest++ = *source++;
case 1: *dest++ = *source++;
break;
}
}
}
// Reproduced above is a simplified version of Duff's device, an infamous memcpy function designed
// to reduce the amount of branching in the operation. (The loop is optimally interleaved with the switch,
// but I'm trying to illustrate some other points and don't want to add to the confusion.) Once we enter the
// switch, perform an assignment and unconditionally progress to the next case statement. This algorithm
// cries out for automation. The case statements have indices that run from 8 down to 1, modulo 8. Can we give it the Circle treatment?
// duff2.cxx
void duff_copy2(char* dest, const char* source, size_t count) {
const char* end = source + count;
while(size_t count = end - source) {
switch(count % 8) {
@meta for(int i = 8; i > 0; --i)
case i % 8: *dest++ = *source++;
break;
}
}
Но гомоиконности таким подкостыливанием вы естественно не добавите!