I’m John C Bland II

Husband, Father, Tech Author, Deacon.
Founder ofΒ Katapult Media, and full-stack polyglot developer.
Political Free Agents Podcast Host.

I create. I launch.

YouTube Channel

I post regular fun on YouTube like me playing the bass and anything else I find fun.Β 

Get Something Built

All project work goes through Katapult Media. Business is open. Let’s chat.

AS3 Tip: Pass …rest arguments w/ other arguments to a function

That’s about as good a title as I could come up with here. πŸ™‚

Background

Let’s say you have a function expecting …rest arguments. Firstly, if you’re not familiar with …rest arguments, read this. Here is a code example of the issue (hand-written and untested):

protected function fun1(...rest):void{
	fun2.apply(null, rest);
}
 
protected function fun2(some:*, thing:*, here:*, ...rest):void{
	trace(some, thing, here, rest);
}
 
fun1("here", "are", "my", "args", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

This code passes 14 arguments to fun1, which is completely legit. fun1 then passes all 14 arguments to fun2. The trace output would be:

"here", "are", "my", "args", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

That’s easy, right? Β Let’s throw a monkey wrench in this puppy real quick.

The Problem

Taking the previous example, let’s swap the calls. fun2 will not call fun1 and the initial function call will be to fun2. Here is the code:

protected function fun1(...rest):void{
	trace(rest);
}
 
protected function fun2(some:*, thing:*, here:*, ...rest):void{
	var newArg:* = 1;
	fun1.apply(null, [some, thing, here, newArg, rest]);
}
 
fun2("here", "are", "my", "args", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Now, you may say: “What’s wrong with that?” To that I’d say: “A LOT!” The monkey wrench is newArg. See, apply(..) takes an array as the second argument which works well unless you’re passing in rest.

The trace would be:

"here", "are", "my", "args", [1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

This is very subtle but notice the last part of the trace is a multidimensional array consisting of newArg and the rest argument passed from fun2.

A Solution

This is a quick and easy way to solve this problem. What you want to do is actually pass in every argument in the rest argument as a single argument, not as an array, but we’re still passing in an array to the apply(…) function. Here is one way to tackle this puppy using array manipulation:

protected function fun1(...rest):void{
	trace(rest);
}
 
protected function fun2(some:*, thing:*, here:*, ...rest):void{
	var newArg:* = 1;
	fun2.apply(null, [some, thing, here, newArg].concat(rest));
}
 
fun2("here", "are", "my", "args", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Ahh…notice the second line of the fun2 function. we’re passing in an array of arguments and concatenating the rest argument with the arguments array.

Here is the output trace from that:

"here", "are", "my", "args", 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

That is exactly what you want to see, in this case. πŸ™‚

Conclusion

This one just bit me in the butt so I thought I’d blog it to help someone else.

What other ways do you use to solve this problem? I’m sure this isn’t the only way.

Proof, meet Pudding. #stevejobs

Just a quick note here. The videos speak for themselves. They prove the marketing wheel is a big beast and people flock to what is believed to be true vs what is true, reference to Seth Godin’s book as enlightened by @simpulton. Think for yourselves people!

Steve Jobs HTML5 web experience on the iPad
– @iBrent

https://www.youtube.com/watch?v=rfmbZkqORX4

HTML5 vs Flash – Developer Perspective
michaelsv10

https://www.youtube.com/watch?v=aVjIsL8qwNw

Disclaimer:
I in no way am discounting every point Jobs made. He made some good one’s but he is far from being accurate, IMO.

Testing @Anywhere

Twitter just launched @Anywhere and I’m testing to see if it works.

@johncblandii
@katapultmedia
@shadowtactics

All of these twitter id’s should now show you a hover box with stats and a follow button. Let me know how it works for you.

Comcast Update

Two techs left about 30 minutes ago and my connection is still good. They went through a million changes though: changed connectors, ran new cable to main box (two houses down since one behind us has a bee hive), replaced outside splitter, replaced modem battery, replaced inside splitter, etc.

Through all of that I’m still nervous as all get out because they tried leaving 3 times and the connection dropped. I’m not at all convinced but time will tell. I still have to be here from 8 am to 8 pm on Friday (nice time slot Comcast!!) so they can bury the cable under ground. Until then we have a cable line running through our yard and two others. πŸ™

Comcast has me unhappy!!!! (off-topic; venting)

I’m not the one to blast service providers because things happen, even with Katapult and our customers, but today just took the wind from my sails. At this point I’m irritated beyond belief with their bad service.

Quick History

  • I wanted AT&T U-verse since Verizon Fios wasn’t available but neither was U-verse. πŸ™ The only service in our area is Comcast. They’re reputable and have long history so should be just fine, right? So I signed up.
  • Signed on July 3, 2009 (or something like that)
  • Service was scheduled to cut on July 10, 2009
  • My brother knows a tech so he got someone out for me on July 9, 2009 (last Thursday)

I was happy as ALL get out on Thursday. I mean…I had internet!! WOO HOO! Being in a new house with nothing but an AT&T 3G card (which is a whole other story) is not how I planned on starting my new life in Houston but hey…was only required until I was connected and on Thursday…I had my connection!

Friday morning I woke up, grabbed my laptop with slight glee in my heart, and commenced to read Gmail (a morning ritual for me) before I started my morning. Well…this was odd…I couldn’t connect. Hrmm…weird. I went through troubleshooting my laptop because I surely couldn’t have service problems less than 12 hours from initial connect…right? πŸ™ Sadly {insert sigh here} my connection was down. I called Comcast, they sent the signal, and we were off and running. I figured…”ehh…just a glitch in the matrix.”

Saturday morning I woke up, grabbed my laptop with slight fear in my heart, and commenced to read Gmail and again…no connection. This time I went straight to the modem. Down!! I had to drive from Houston to Sinton to grab my worldly possessions from storage so I said “Whatever!” and kept trucking through my day. Got back that night (after a trailer wreck, tow, and unloading of the trailer; eventually at 5 AM) and fooled with the modem a bit. No go.

Sunday morning I woke up and skipped even trying to connect. I went straight to the modem and tried the troubleshooting tasks I learned through the years (unplug, wait N seconds, power up, try again) and nothing worked. I called Comcast. They sent the signal, fooled with stuff, etc and it came back up. I complained big time about the inconvenience so they credited our account. We were good then…DOWN! I called back and they couldn’t restore service so I was down all day. Later that day I thought I’d try again. We got it back that night so I just knew we’d be good the next day. A tech couldn’t come out until Tuesday (which they scheduled on the first call) so I was sure I’d be good the next day since we were connected Sunday night.

Monday morning I woke up scared as heck! It was an actual work day and I was afraid I’d spend a day without internet again but this time it would hurt my productivity. Well…I did. I was without a connection all day. Then I remembered…”I have an online session scheduled for 8!” CRAP! I couldn’t do it on 3G without terrible lag, etc so I called Comcast. Service came up at 7:20’sh. Called into tech support since service went down at 7:40’sh. Service was up and good throughout the session (don’t ask how but it was) and didn’t go down until 10:30’sh. At this point I’m like “Whatever!” again since the tech was coming today. I let it go and went 3G for the night.

Today I woke up and within a few minutes the tech rang the doorbell. SWEET! He came in checked signals, connections, etc and determined my coax was the problem from the wall to the router. Changed the coax, restarted modem, and my connection was GOLDEN! πŸ˜€ {insert “Singing in the Rain” video clip here since, mentally, I was swinging from pillars}

I talked to the rep about my connection speed. I have only tested at a max of 11 megs down but was sold on a plan including 21 megs down. I WANT MY EXTRA 10 MEGS!! He said I’m only setup for the Performance plan not the Performance Plus and the fastest Comcast offers is 16 megs down. WHAT!??!?!?!? He says he can order the upgrade but it is best I do it. I told him that’s not what I was sold so I will call in and check on everything.

When I called in I got to tech support and let them know about my speed issues. While on the phone with Comcast, which was an hour later, my connection…{sigh}…died. πŸ™ So I had the rep put in an order for another tech to come out, which is scheduled for tomorrow, as well as expedite a tech to come out today. Supposedly the tech will address my speed issues but the most I’ll get is the 16 unless a faster speed is available in my area, which is unlikely or at least an easy excuse for them to say “16 is it buddy!” I’ll fight it though since we were sold on a 21 megs down plan that, apparently, doesn’t exist.

5 days + intermittent internetΒ  = one seriously frustrated customer who uses the web to live

I won’t say don’t get Comcast. Once they get me fixed up I’m sure all will flow well but my experience has been CRAP. It is unfortunate many people are having the same or similar problems. I would expect much more from a reputable company like Comcast. Their service should be much stronger than it is.

Comment response delay, my bad

I changed mail servers a lil’ bit ago and I have yet to get WordPress sending emails properly again. Well…to be honest…I haven’t tried due to time.

I’ll keep an eye out for comments and try to get my email fixed soon.

Outage: Switching Mail Servers

I’ve switched mail servers for Katapult and some others I managed to Google Apps (https://www.google.com/apps/intl/en/group/index.html). This has been a GREEEEAAAAAAT decision as my spam has decreased to 0 from 25-50/day (basically 3 to 6 per hour most of the time).

So, my personal email address (tied to this domain) will be down for a bit as I am making the switch right now. I know…middle of the day isn’t the best but spam can push you to places you never thought you’d go. πŸ˜‰ lol.

AS3: Converting Date() to the Epoch

The Flash Player uses the same epoch as Unix (January 1, 1970). An epoch is “a particular period of time marked by distinctive features, events, etc.” (source: Dictionary.com)

One thing to note is the date pertains to the UTC (univeral time coordinated: “Universal time, taking into account the addition or omission of leap seconds by atomic clocks each year to compensate for changes in the rotation of the earth.” [source: Dictionary.com]). Flash can handle this just fine. Let’s get to the code (starting to itch with all of these definitions).

One option to get the UTC date:

var now:Date = new Date();
var epoch:Number = Date.UTC(now.fullYear, now.month, now.date, now.hours, now.minutes, now.seconds, now.milliseconds);
trace(now, epoch/1000, Math.round(epoch/1000));

The only real thing to note is the epoch/1000 is there because Flash uses milliseconds and epoch is in seconds so we divide by 1000 to get the seconds since the epoch. Now, that is how I first did it until I RTFM (read the freaking manual). It felt so dirty and unnecessary. Here is the easiest way to do it.

var now:Date = new Date();
var epoch:Number = Math.round(now.valueOf()/1000);
trace(now.valueOf(), epoch); //1238595716133 1238595716

Notice the first trace shows a lot more numbers since it is milliseconds since the epoch. The second number is the epoch/1000 and rounded. Don’t forget to round or you’ll get three decimal places. Date.value() is the trick here. By definition: “Returns the number of milliseconds since midnight January 1, 1970, universal time, for a Date object.” (source: Adobe LiveDocs)

Anyways…that’s it. No need to pontificate (been wanting to use that word; lol) anymore about such a simple task.

UPDATE

Since someone on Twitter made the statement about getTime() I guess I wasn’t clear. Let me clarify.

The above is NOT the only way to do it. Yes, you can use new Date().getTime() or new Date().time (preferred over getTime() since it really is a property). They both yield the same result as new Date().value(). Dividing by 1000 and rounding is still required since we’re still fooling with milliseconds since epoch.

Thanks.

Quick Posts Coming…

So I’ve been Twittering (https://twitter.com/johncblandii) a lot lately, as usual, and I noticed I tend to Twitter my dev’ thoughts more than blog them. I want to get back to blogging them so this is an FYI.

You will see some quick posts here soon. By quick I mean random things I figure out or discover while developing or quick posts about dev approaches, design patterns, or whatever. I’ll still get a meaty post in here at times but you’ll see quicker posts here/there.

Flex 3 Remoting one coming in a second. πŸ˜‰