- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
function* foo() {
let i = 1;
yield ++i;
yield ++i;
}
function main() {
for (const o of foo()) {
print(o);
}
print("done.");
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
function* foo() {
let i = 1;
yield ++i;
yield ++i;
}
function main() {
for (const o of foo()) {
print(o);
}
print("done.");
}
вот... решил самую крутую проблему в итераторах... а ты (С/C++) так можешь?
+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
+1
function *foo()
{
yield 1;
yield 2;
yield 3;
}
function main()
{
for (const o of foo())
{
console.log (o);
}
}
main();
давайте посмотрим, как TS/JS имплементирует "елды". изначально код выглядит красиво. но заглянем под капот что генерит tsc для выполнение такого кода. мне придется разбить этот ужос
+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; причем компилятор сам определяет какой вариант лучше юзать
+1
type a = 1;
type i = a & a;
type j = a | a;
function main() {
let v1: i;
let v2: j;
assert(sizeof(i) != sizeof(j));
print("done.");
}
ну все я понял вам скучно... как насчет такого примера?
−1
type int = 1;
type int64 = 4294967296;
declare function GC_get_heap_size(): int64;
function main() {
let i = 0;
for (i = 0; i < 10000000; ++i) {
const p = new int[1];
if (i % 100000 == 0) print("Heap size = ", GC_get_heap_size());
}
}
вот такая лабуда подкатили.. называется GC.. для тех кто не любит GC я добавил опцию -nogc :)
вот так можно JIT запускать без компиляции
tsc.exe --emit=jit --shared-libs=TypeScriptGCWrapper.dll 1.ts
tsc.exe --emit=jit -nogc 1.ts
0
function main() {
const o = {
v: "Hi",
toString() {
return this.v;
},
};
print(o);
}
Хочу напомнить о себе, а то люди начали забывать :)...
+7
console.log(parseInt(0.5)); // 0
console.log(parseInt(0.05)); // 0
console.log(parseInt(0.005)); // 0
console.log(parseInt(0.0005)); // 0
console.log(parseInt(0.00005)); // 0
console.log(parseInt(0.000005)); // 0
console.log(parseInt(0.0000005)); // 5
https://ideone.com/YMWUGq
Возможно баян, спижжено с https://vk.com/wall-72495085_1267978
+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);
}
короче новый говнокод подоспел. Т.к. вы все тут самые умные я не раскажу в чем фича. Сами догадаетесь