• What Are You Working On? April 2015
    1,741 replies, posted
[QUOTE=geel9;47496411]except mine is better and i posted it before you :| [img]https://feen.us/cwum9u.png[/img][/QUOTE] how is yours better when they're virtually the same?
[QUOTE=adnzzzzZ;47496433]Print 100 to 1 with a loop that starts like this: "for (i = 0;". Pick any language, but the loop has to start from i = 0.[/QUOTE] [code] int main() { for( int i=0; i!=1; ) { printf( "%d\n", i = i<1 ? 100 : i - 1 ); } } [/code]
[QUOTE=adnzzzzZ;47496433]Print 100 to 1 with a loop that starts like this: "for (i = 0;". Pick any language, but the loop has to start from i = 0.[/QUOTE] [code]for (i = 0; i < 100; i++) console.log(100 - i)[/code] :?
[QUOTE=The Inzuki;47496473]how is yours better when they're virtually the same?[/QUOTE] literally just combined the if/else branch if you wanted to do it even better you could do if(n < 2) return n; Oh, even better way of doing the 100-1: [code] for(int i = 0; i < 1; i++) { print("100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321"); } [/code]
[QUOTE=Trumple;47496080]Ah, one of [i]those[/i] I always feel a bit dumb when I don't even understand what the thing someone is posting about is, let alone the code Looks clever and neat though! I'm going to have to read up on recursive decent parsers now...[/QUOTE] The most interesting part is probably how you can use the LINQ comprehension syntax's pattern matching together with extension methods and variant interfaces to transform various abstract collections (in the linguistic sense, here they are "collections" of [I]potential[/I] matches) into a new compatible one that doesn't necessarily share any type parameters with the original ones, and all that without requiring the new ones explicitly. I haven't implemented too much in this regard yet though, so some common methods are likely missing. (It took me a while to wrap my head around this and even now I'm mostly just following Sprache's code to add LINQ compatibility. It's a bit easier for me because I have actual [I]IEnumerable<>[/I]s to work with: [code]public static class Pattern_LINQifier { public static Pattern<TScanner, TResult> SelectMany<TScanner, TResult1, TResult2, TResult>(this IPattern<TScanner, TResult1> generator, Func<TResult1, IPattern<TScanner, TResult2>> selector, Func<TResult1, TResult2, TResult> projector) { return new Pattern<TScanner, TResult>(scanner => from result1 in generator.MatchOn(scanner) from result2 in selector(result1).MatchOn(scanner) select projector(result1, result2)); } public static Pattern<TScanner, TResult> Select<TScanner, TGeneratorResult, TResult>(this IPattern<TScanner, TGeneratorResult> generator, Func<TGeneratorResult, TResult> projector) { return new Pattern<TScanner, TResult>(scanner => from result in generator.MatchOn(scanner) select projector(result)); } }[/code]) Since ScanT fails silently by default (simply not returning any matches), I added a helper function that makes making them "loud" easy: [code]var requiredTicket = Template.Require<ITracker<int, char>, int, IEnumerable<char>>( Template.ItemSequence("Ticket"), failureHandler: (start, scanner) => { throw new FormatException(string.Format("Required \"Ticket\" at {0} but mismatched at {1} on {2} instead of {3}.", start + 1, scanner.Position, scanner.Current, "Ticket"[scanner.Position - start - 1])); }); var TicketTicketTicked = new ScannableString("Ticket Ticket Ticked"); var TicketTicketTicket = from t1 in requiredTicket from _1 in Template.One(' ') from t2 in requiredTicket from _2 in Template.One(' ') from t3 in requiredTicket select new Nothing(); var matches = TicketTicketTicket.MatchOn(TicketTicketTicked.GetScanner()); foreach (var match in matches) { // Do nothing. }[/code][code]Unhandled Exception: System.FormatException: Required "Ticket" at 14 but mismatched at 19 on d instead of t. at ScanT_Test.Program.<Main>b__19(Int32 start, ITracker`2 scanner) in c:\Programmierkram\Project ScanT\ScanT Test\Program.cs:line 55 at ScanT.Template.<RequireTemplate>d__3c`3.MoveNext() in c:\Programmierkram\Project ScanT\ScanT\Template.cs:line 110 at System.Linq.Enumerable.<SelectManyIterator>d__11`3.MoveNext() at ScanT_Test.Program.Main(String[] args) in c:\Programmierkram\Project ScanT\ScanT Test\Program.cs:line 68[/code] [identifiers not final] It's still quite ugly here, but you could wrap most of that [I]requiredTicket[/I] definition into a [I]RequiredSequence<TItem>(TItem[] sequence)[/I] method to make it work with any [I]ITracker<int, TItem>[/I] (which is just [I]IEnumerable<TItem>[/I] with an additional read-only [I]Position[/I] property). All of these helpers are also methods that could be written outside the library, so if a shortcut is missing you can just make your own template.
[QUOTE=geel9;47496411]except mine is better and i posted it before you :| [img]https://feen.us/cwum9u.png[/img][/QUOTE] missed the fucking point it was a joke about copying it off stackoverflow
[QUOTE=TheEyes;47496527]missed the fucking point it was a joke about copying it off stackoverflow[/QUOTE] so it was.
[QUOTE=geel9;47496497]literally just combined the if/else branch if you wanted to do it even better you could do if(n < 2) return n; Oh, even better way of doing the 100-1: [code] for(int i = 0; i < 1; i++) { print("100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321"); } [/code][/QUOTE] true, just put it all on one line then
[QUOTE=adnzzzzZ;47496433]Print 100 to 1 with a loop that starts like this: "for (i = 0;". Pick any language, but the loop has to start from i = 0.[/QUOTE] My personal favorite: [code]for(int i=0; i<100; ++i) printf("%d\n", i==0?100:i==1?99:i==2?98:i==3?97:i==4?96:i==5?95:i==6?94:i==7?93:i==8?92:i==9?91:i==10?90:i==11?89:i==12?88:i==13?87:i==14?86:i==15?85:i==16?84:i==17?83:i==18?82:i==19?81:i==20?80:i==21?79:i==22?78:i==23?77:i==24?76:i==25?75:i==26?74:i==27?73:i==28?72:i==29?71:i==30?70:i==31?69:i==32?68:i==33?67:i==34?66:i==35?65:i==36?64:i==37?63:i==38?62:i==39?61:i==40?60:i==41?59:i==42?58:i==43?57:i==44?56:i==45?55:i==46?54:i==47?53:i==48?52:i==49?51:i==50?50:i==51?49:i==52?48:i==53?47:i==54?46:i==55?45:i==56?44:i==57?43:i==58?42:i==59?41:i==60?40:i==61?39:i==62?38:i==63?37:i==64?36:i==65?35:i==66?34:i==67?33:i==68?32:i==69?31:i==70?30:i==71?29:i==72?28:i==73?27:i==74?26:i==75?25:i==76?24:i==77?23:i==78?22:i==79?21:i==80?20:i==81?19:i==82?18:i==83?17:i==84?16:i==85?15:i==86?14:i==87?13:i==88?12:i==89?11:i==90?10:i==91?9:i==92?8:i==93?7:i==94?6:i==95?5:i==96?4:i==97?3:i==98?2:1);[/code]
I managed to make an int.Parse() method that's faster than C#'s native one. Unsure of what I might be missing though. It also relies on a static power-of-10 table, which I imagine might be against spec. Anyways it runs 1,000,000 iterations of my method 0.058 seconds faster than 1,000,000 iterations of int.Parse(). Performance that MATTERS, people. (Actually, it's ~2x faster than int.Parse) Here's my shitty code [code] public static int[] table = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; public static int NewIntParse(string input) { int len; if (input == null || ((len = input.Length) == 0)) return 0; //Or throw an exception I guess int ret = 0; //USELESS OPTIMIZATIONS? WHO KNOWS. int mult = 1; int i = 0; if (input[0] == '-') { mult = -1; i = 1; } for (; i < len; i++) { int c = (input[i] - '0'); if (c < 0 || c > 9) throw new ArgumentException("Input string is not in correct format"); if (c == 0) continue; ret += c * table[(len - 1) - i]; } return ret * mult; } [/code]
Why not this: [code] public static int NewIntParse(string input) { int len; if (input == null || ((len = input.Length) == 0)) return 0; //Or throw an exception I guess int ret = 0; //USELESS OPTIMIZATIONS? WHO KNOWS. bool neg = false; int i = 0; if (input[0] == '-') { neg = true; i = 1; } for (var power = 1; i < len; i++, power *= 10) { int c = (input[i] - '0'); if (c < 0 || c > 9) throw new ArgumentException("Input string is not in correct format"); if (c == 0) continue; ret += c * power; } return neg ? -ret : ret; } [/code] Is array lookup faster than multiplication?
[QUOTE=Sidneys1;47496560]My personal favorite: --holy fucking shit--[/QUOTE] This is beautiful... [sp]you monster[/sp]
[QUOTE=Sidneys1;47496686]Why not this: [code] public static int NewIntParse(string input) { int len; if (input == null || ((len = input.Length) == 0)) return 0; //Or throw an exception I guess int ret = 0; //USELESS OPTIMIZATIONS? WHO KNOWS. bool neg = false; int i = 0; if (input[0] == '-') { neg = true; i = 1; } for (var power = 1; i < len; i++, power *= 10) { int c = (input[i] - '0'); if (c < 0 || c > 9) throw new ArgumentException("Input string is not in correct format"); if (c == 0) continue; ret += c * power; power *= 10; } return neg ? -ret : ret; } [/code] Is array lookup faster than multiplication?[/QUOTE] Yeah, yours is slower. Good idea though.
[QUOTE=Protocol7;47496316]Starting on a WPF app for my word vomit library just for some practice, what do you all think of the direction i'm going so far? [IMG]http://i.imgur.com/Zs6sn30.png[/IMG] edit: shit i already see my window controls aren't even vertically centered in the title bar.[/QUOTE] hey! I'm working on something similar :v [IMG]http://i.imgur.com/7v2tDJ6.gif[/IMG]
[QUOTE=geel9;47496708]Yeah, yours is slower. Good idea though.[/QUOTE] How about the return neg ? -ret : ret;? Should be faster than multiplication as in theory it only flips the sign bit. [editline]xxx[/editline] I suppose mine would be useful if you couldn't include a static array, like you said.
[QUOTE=Sidneys1;47496719]How about the return neg ? -ret : ret;? Should be faster than multiplication as in theory it only flips the sign bit.[/QUOTE] It's a comparison, though. I haven't run a test on that but I imagine it's slower than a simple multiplication. [editline]10th April 2015[/editline] yeah the static table is definitely weird and probably breaks a lot of conventions so yours is the best option for a sane environment :v:
[QUOTE=geel9;47496726]It's a comparison, though. I haven't run a test on that but I imagine it's slower than a simple multiplication. [editline]10th April 2015[/editline] yeah the static table is definitely weird and probably breaks a lot of conventions so yours is the best option for a sane environment :v:[/QUOTE] Also, this breaks with digit grouping symbols (',' in U.S., ' ' in some other countries, etc) :/
[QUOTE=geel9;47496606]I managed to make an int.Parse() method that's faster than C#'s native one. Unsure of what I might be missing though. [...][/QUOTE] Culture dependence and [I]int.MinValue[/I]. You can easily solve the latter by starting with negative numbers and flipping the sign for positive ones. The static table is fully thread-safe but are you sure it's actually faster than replacing [I]ret += c * table[(len - 1) - i];[/I] with [I]ret = ret * 10 + c;[/I] and iterating backwards? Culture dependence is far more difficult to solve. Various scripts use different integer representations (which may include additional (optional) separator chars) and Unicode digits, so accounting for all formats of a given locale isn't easy and most likely what causes the .NET implementation to be slightly slower.
[QUOTE=General J;47496716]hey! I'm working on something similar :v [IMG]http://i.imgur.com/7v2tDJ6.gif[/IMG][/QUOTE] Oh boy. Reminds me of the one I made. Sample output, 36 rounds: [code]pube licker shitface dipshit dumbass homo fat-fuck cuntface dipshit Dr. Chunk Of Shit chunk of poop homo dumb-fuck homo douchenozzle asshole asshole homo dipshit Uncle DoucheCanoe scat master homo cocklicker motherfucker Big Asshole fuck dumbass pile of shit scat master homo King Fat-Fuck asshole pile of shit cunt homo shitass dipshit[/code]
[img]http://www.facepunchstudios.com/wp-content/uploads/2015/04/2015-04-10_17-47-47-1080x675.jpg[/img] [url]http://www.facepunchstudios.com/2015/04/10/arcade-devblog-12/[/url]
WPF is so much better than winforms holy shit I'm itching really badly to work on my AI but I'm not allowing myself to work on any other project until this WPF frontend is done, but apparently uni game design classes are hardcore and take up a shitload of time so I'm scrambling to work on that even
[QUOTE=General J;47496716]hey! I'm working on something similar :v [IMG]http://i.imgur.com/7v2tDJ6.gif[/IMG][/QUOTE] Mine's entirely configurable, I have a blog post in it here: [url]http://www.hintcode.com/fore_development/[/url] I've changed a bit about how it handles the dictionary and keeping the dictionaries in memory, I just happen to really like UI work.
I feel like a spoilt kid. I am here developing video games and studying math yet I find it unfulfilling, everyday I am more dreaming of making robots and stuff. I need a breakthrough really fast.
[QUOTE=Protocol7;47497248]Mine's entirely configurable, I have a blog post in it here: [url]http://www.hintcode.com/fore_development/[/url] I've changed a bit about how it handles the dictionary and keeping the dictionaries in memory, I just happen to really like UI work.[/QUOTE] We're both named Nick and we both made random text generation libraries. That's kind of amusing. :v: Oh and have a star. Good stuff you got there.
Things that happen when trying to implement text rendering with OpenGL :v: [img]http://i.imgur.com/kDV61vh.png[/img]
Testing my first multiplayer game. Sorry for dark video :/ [video=youtube;MkmifMpdWKM]http://www.youtube.com/watch?v=MkmifMpdWKM[/video]
[QUOTE=Berkin;47497744]We're both named Nick and we both made random text generation libraries. That's kind of amusing. :v: Oh and have a star. Good stuff you got there.[/QUOTE] Rant absolutely inspired me, so that's funny how it works out. It's also an unfortunate side effect that Rant is 4 letters and Fore is 4 letters, though mine alludes more towards having foresight, since the current word dictates what type of word the next word will be. I finished my UI, though, and moved generation entirely to the library. However my punctuation modifier class doesn't seem to punctuate. Oh well. Wrote that one in 5 minutes, to be expected. First iteration: [IMG]http://i.imgur.com/RVQxDva.png[/IMG] My window buttons change accordingly as well: [IMG]http://i.imgur.com/ptrj30i.png[/IMG] Even nicely get highlighted when you mouse over: [IMG]http://i.imgur.com/6Xz7LQy.png[/IMG]
I'm rewriting almost the entire Rant codebase from scratch and writing an AST parser (finally) to replace the old, rather strange interpreter. I'm hoping that this will take care of many of the performance problems Rant has, as well as make the code a lot more maintainable. I'm already starting to see how well the refactor is working out; this is my entire VM loop: [code]public RantOutput Run(double timeout) { var callStack = new Stack<IEnumerator<RantAction>>(); // Push the AST root callStack.Push(_pattern.Action.Run(this)); top: while (callStack.Any()) { // Get the topmost call stack item var action = callStack.Peek(); // Execute the node until it runs out of children while (action.MoveNext()) { if (callStack.Count == RantEngine.MaxStackSize) throw new RantRuntimeException(_pattern, null, $"Exceeded the maximum stack size ({RantEngine.MaxStackSize})."); // Push child node onto stack and start over callStack.Push(action.Current.Run(this)); goto top; } // Remove node once finished callStack.Pop(); } return new RantOutput(_rng.Seed, _startingGen, _mainOutput.Channels); }[/code] No recursion is used here; it's emulated using a stack of iterators. Will add in the timeout functionality later.
Does anyone know of any good resources on generating and using ASTs? I've never really found anything helpful before, but I'd certainly like to.
Was bored, made a LISP. [IMG]https://i.imgur.com/udQoguZ.png[/IMG] Well, most of one. I wrote a lexer and an interpreter, but I haven't written a parser yet. [IMG]https://i.imgur.com/6lV0JVC.png[/IMG]
Sorry, you need to Log In to post a reply to this thread.