$factorial - the art of code shortening

By jaytea on Mar 24, 2011

If you have been an mIRC hobbyist for any significant length of time, you may have come to the conclusion that its scripting language holds no more mysteries and poses no more challenges for you. You may even lament the time you spent learning this shoddy language, with its flippant parser and its highly limited scope with respect to today's technology. However, if you should decide to go deeper, past the 'what's and onto the 'why's, you will find that there's a lot more to know about this seemingly simple language than you've ever realized. But why would you want to know it to such a depth? Well, let's just say, if you learn mIRC script deeply then you have learned how NOT to build a scripting language :P

I've posted this snippet to celebrate the re-introduction of SwiftIRC's challenges - a subculture of the now dwindling global mIRC scripting scene - that features a bunch of like-minded enthusiasts coming together and battling it out on the client that we all know and love. Many of the challenges concern 'code shortening': a practice that, taken out of context, is largely useless. When placed in the context of these challenges, wherein a group of people all apply this practice to meet the same end, the results can be very interesting indeed. The mIRC scripting language, its syntax, features and the nature of its interpreter, are such that it enables you to conceal great complexity in just a few short lines. The same can be said for most languages of course, but mSL is quite unique in the informality of its syntax, the diversity of its standard library and the sheer number of features that behave, in certain subtle ways, contrary to one's intuition and often to their own intended purpose.

If any of this sounds interesting, then I welcome you to participate in our ongoing challenge '$valid_msl()', the details of which can be found at:

http://forum.swiftirc.net/viewtopic.php?f=35&t=24742

/*
{
  The following is a simple demonstration of what typically occurs in a 
  challenge based on the size of code.

  The challenge, in this case, is to create a custom identifier called
  $factorial, whose format is:

  $factorial(N)[.r]

  $factorial(N) should return the factorial of N; that is, the product of
  all positive integers <= N.

  For example, $factorial(4) = 24, since 4 * 3 * 2 * 1 = 24.

  If a property is given (tested only with 'r'), then the process is reversed
  such that the return value is the N whose factorial is the given number.

  For example, $factorial(24).r = 4, since $factorial(4) = 24.

  In this particular challenge, the contestants were not required to check 
  for erroneous input.
}
*/

; Method 1 (103 characters excluding indentation and new lines)

; A simple yet elegant method that very closely follows our intuition. This 
; is how I would code it in any every day situation, and I'm sure it closely
; resembles what many others scripters are capable of producing.

alias factorial {
  var %a 0,%b 1
  while %a < $1 {
    inc %a
    %b = %a * %b
    if $prop && %b = $1 return %a
  }
  return %b
}

; Method 2 (100 characters excluding indentation and new lines)

; 3 characters and several hours later, if you are sufficiently savvy about
; the various ins and outs of mIRC syntax, we may see the following method.
; The code has completely changed, and the result is now a very carefully and
; deliberately selected combination of characters that, thanks to certain
; facts about the language and its constituents, produces the desired output.

alias factorial {
  while $1 != $5 tokenize 32 $1 $calc($2+^) $calc((1+$2)*$3 1) $prop $3 $2-
  return $6
}

Comments

Sign in to comment.
bandbonjtv   -  Feb 02, 2013

i have a script i need to shorten, and am lost. please help. i uploaded it to my snippets section

 Respond  
Firstmate   -  Mar 30, 2011

Everyone on that forum is unsurprised when jaytea wins >.>
Just about every mIRC scripting website I know of that runs challenges is dominated by jaytea.

 Respond  
jaytea   -  Mar 27, 2011

thanks TC :P the Challenge sub-forum on swift is certainly a rich part of mIRC scripting history; a fountain of ideas that, much like the old mirc.com forums, mIRC Usenet groups, and even mircscripts.org, will one day probably be relegated to the gutters of the internet. in the meantime, however, it is certainly worth checking out for information and techniques that are still relevant and useful.

 Respond  
IllogicTC   -  Mar 26, 2011

Follow the link jaytea provided at the bottom of his description, and then back up to the "Challenges" part of the forum from there. There are many more examples like the one jaytea has shown here where people solve seemingly difficult tasks, and their approaches are far-ranging from small, complex regex strings to large and convoluted, like most of my scripting looks like lol.

A very good read, all of them.

 Respond  
jaytea   -  Mar 26, 2011

haha, well spotted IllogicTC! i assure you that there was no hidden message there :P and you're exactly right, these tasks require you to think in a much more different way than you do when creating a fully featured, user friendly addon with a UI and such.

jonesy, too true, it's a problem a lot of us face :P i used to work on my entries mentally on the bus rides to and from uni. i suppose if you're genuinely interested in something then you tend to find the time for it. being exposed to this type of coding intrigued me early on, and i'm not sure i would've become a programmer without that interest. in high school, i dropped Biology and took up Computing at the very last minute thinking "let's try this out!". i wonder where i'd be today if i hadn't done that :O

 Respond  
IllogicTC   -  Mar 25, 2011

I was reading the description, and I had my serious face on until I reached the 3rd sentence. I do understand it is supposed to be read as "However, if you should..." but it made me laugh with the "However, f you...".

Seriously though. Another excellent snippet of wisdom from Jaytea, and a great nod toward challenging people to try things you don't see in your everyday script.

 Respond  
Jonesy44   -  Mar 24, 2011

If only I had the spare time :p

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.