Posts

Javascript factorial - performance considerations

I was playing with writing the best and shortest factorial function in javascript. Here are some results for you: (results from Mac OS running Chrome Version 55.0.2883.95 in Developer Console) f = n => n ? n * f(n - 1 ) : 1 ; console.time( 'factorial recursive' ); f( 9999 ); console.timeEnd( 'factorial recursive' ); VM272 : 1 factorial recursive : 0.733 ms However, a purely recursive function quickly runs into memory issues: console.time( 'factorial recursive' ); f( 9999999 ); console.timeEnd( 'factorial recursive' ); VM102 : 1 Uncaught RangeError : Maximum call stack size exceeded A better version is to use lodash reduce: f = n => n ? _(_.range( 1 ,n + 1 )).reduce((p,i) => p * i) : 1 ; console.time( 'factorial reduce' ); f( 9999 ); console.timeEnd( 'factorial reduce' ); VM301 : 1 factorial reduce : 1.724 ms console.time( 'factorial reduce large' ); f( 999999 ); console.time

The beauty of Haskell

Ah, the beauty of Haskell, solving the graceful labeling node generation (brute force mind you, but still...) vonKoch edges = do let n = length edges + 1 nodes <- permutations [ 1 .. n ] let nodeArray = listArray ( 1 , n ) nodes let dists = sort $ map ( \ ( x , y ) -> abs ( nodeArray ! x - nodeArray ! y ) ) edges guard $ and $ zipWith ( /= ) dists ( tail dists ) return nodes (solution found here: http://www.haskell.org/haskellwiki/99_questions/Solutions/92 )

AngularJS Paginator

So today I've finished my first AngularJS directive. And what can I say, I'm quite impressed, way to go Google team ! What I ended up with is a component that can paginate through any REST database listing by placing a single line of HTML on your page.  http://github.com/MarianVasile/AngularJS-Paginator  AngularJS Paginator author: Marian Vasile, Dec 2012 This component paginates an ajax database service, with a single HTML line, and some CSS. Steps for integration: In your listing HTML file add this line to the top and the bottom of the actual listing div or table <paginator current-page="{{ listing.current_page }}" total_pages="{{ listing.total_pages }}" call="searchListing" wclass="current_page"></paginator> Adjust the code in your Angular controller for the paginator scope (the same scope as the listing itself) to pass the page to $resource - see the repapp-controller.js for instructions On the ser

VIM changing file SE context

I don't know why this works but I've wasted pretty much the whole day trying to put my ambition against my ignorance. If you encounter a problem with vim saving all your web files with a context type of user_home_t instead of inherited httpd_sys_content_t here's how I fixed mine (after much research, uninstallation and recompilation from sources, and eventually brute force one by one removal of all .vim/*): Simply removing the following two lines from my .vimrc fixed the issue: set backupdir=~/.vim/backup set directory=~/.vim/backup Why creating the backup was changing my file selinux context? Why only on my freshly installed CentOS 6? I'm tired, and even though I won, I am not enlightened. Much like everything in real life.

Manual pages optimized for search as well as for reading

A lot of online manual pages today, especially Objective-C are written with full object description in the main body, which makes it very hard to read but very good for search engines. To mend things, we could implement a graceful degradation of certain terms into shorter versions, while using HTML5 abbr and CSS to hide or fade out the full description. We can even offer the option to apply such contractions or not, based on user preferences (some minds may be more comfortable reading the full description). All contractions could be programmatically formatted by the blog (app) engine, based on a key:value pair list of abbreviations and clarifications. Here's an excerpt from such a man page taken from developer.apple.com: Instance and Data Life-Cycles It is important to understand that the life-cycle of the data a managed object represents is largely independent of the lifetime of individual managed object instances. In order to add a record to a persistent store, you must alloc

To escape or not to escape. Part 2.

After testing and playing with the remap_string function on a real project, I've made some changes and fixes: I've introduced an encoding signature, such that you cannot re-encode by mistake an object that was already encoded. Currently I'm using the ord(ESC)ord(ESC) as signature, which in principle should not affect any future application of this function. I have changed the multi-byte UTF8 signature to 'y0y'. The previous signature (0y similar to 0x notation for hex chars) was not completely isolated and could have produced wrong decoding under certain conditions. I have included here the helper functions encode() and decode() which encode strings, arrays and objects. These two functions are currently using a global var "non_encoding_fields" to skip the object or array keys which should not be encoded. /* * Replace all non alpha characters with numeric codes * I'm using this to convert all data before storing into mongoDB / mySQL */ function

To escape or not to escape

I was trying to find a better way to store strings in my database, and after hours of researching string escape, mysql_real_escape_string, preg_quote and other variants, I decided to rethink the whole process. I needed a solution that would completely eliminate the escaping issue and would work with any database (sql or noSQL such as MongoDB). In the first run I came up with this line: preg_replace("/([^A-Za-z ,]{1})/eu", "' ' . ord(substr('\\1', strlen('\\1')-1, 1)) . ' '", $text); It converts all non alpha characters to their respective numeric codes, and also undoes the escaping of double quote characters which preg_replace will do when using /e modifier. However, as elegant and simple it may appear, this solution works only for ASCII encoded strings. To make it more universal I need to parse UTF-8. So here comes stage two: /* * replace all non alpha characters with numeric codes */ function remap_string( $text, $decode=false )