Potpourri

Spread the Love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

In this post, I’ll go over some knick-knacks to further your understanding of programming.

Main(&nbsp&nbsp) &#8212 It's Not Just a Story, It's an Algorithm!

In our LittleRedRidingHood program, you may have noticed that the story opener, main(  ), looks a lot like a verb.  That’s because it is!  Remember, your computer program serves double duty as both a story AND a set of instructions for the machine to execute.  In primitive programming languages such as C, main(  ) is independent and floating around all by itself.  In OOP-languages like Java, however, it’s buried inside a noun that represents your story:

public class TitleOfYourStory
{
    public static void main( String[  ] args )
    { … }

    …
}

Are You There, God?&nbsp&nbspIt's Me, A Constructor Method

I also briefly talked about constructors.  Let’s discuss them a bit more.  They’re methods that construct new objects — and are invoked with the new keyword:

rufus = new Dog(  );

This tells the computer to create a new object using the blueprint Dog and name it “rufus”.  Some languages use the keywords constructor or intialize rather than new.

Why do constructor methods look different from most other methods?  The reason is because constructors are class methods rather than object methodsObject methods are tied to individual nouns, while class methods are tied to the blueprint itself.  Now, this may seem strange, but if you think about it for a moment, it actually makes sense.  Because constructors represent the process of creating new objects, they can’t be part of the objects they’re constructing — since those objects don’t exist yet.  For example, imagine you’re constructing a new building:

freedomTower = new Building(  );

You can’t stick the constructor method into freedomTower, because the building hasn’t been built yet.  Programming languages solve this by sticking the constructor into the blueprint, Building.

Once freedomTower is built, then you can throw in all the the other methods such as, open_door(  ), turn_electricity_on(  ), send_elevator_to_lobby(  ), etc.

Yet, even with this explanation, the idea of sticking a verb into a blueprint still seems weird, so I prefer a different analogy.  I like to think of all constructor methods as part of an omnipotent Creator object that lives inside your computer.  The Creator is responsible for building the entire digital world.

So it’s sitting there, a little bored, a little sleepy, waiting for something to do.  When you insert your program into the computer, the Creator looks through it, and whenever it sees the keyword new, it invokes its constructor method to create a new object based on the blueprint you specify.

If I were to design a new programming language, I would make this analogy explicit.  Instead of using new, a programmer would need to summon the Creator to instantiate new objects:

Creator.construct_new( Dog, "rufus" );
Creator.construct_new( Building, "freedomTower" );

I would even allow the programmer to rename the Creator to anything he wants.

Creator.rename( "God" );

God.construct_new( Man, "adam" );
God.construct_new( Woman, "eve" );

Or, even better:

Creator.rename( "WC" );

WC.construct_new( World, "earth" );

Yeah, I really like the sound of that.

The Importance of Code Libraries

If you crack open any computer program, chances are you’ll see a few statements at very the top that look like this:

import java.util.ArrayList;
import Draw.SquigglyLines;

These are code libraries — which are essentially code that someone else has written and is allowing you to incorporate into your story.  The keyword import tells the computer to look for a file named java.util.ArrayList and one named Draw.SquigglyLines — and add the necessary code into your program.

This may not seem like a big deal, but these are probably the most important statements you’ll ever write.  If done right, 99.9% of the code in your program should come from these libraries.  The code that you yourself actually create should account for a tiny fraction of the whole program.  If someone’s already invented the wheel, why bother re-inventing it?  Just import Draw.Wheel.

The Avengers: Endgame is the perfect example of how to utilize code libraries:

import IronMan, IronMan2, IronMan3;
import CaptainAmericaTheFirstAvenger, CaptainAmericaTheWinterSoldier,
       CaptainAmericaCivilWar;
import Thor, ThorTheDarkWorld, ThorRagnorak;
import TheIncredibleHulk;
import MarvelsTheAvengers, AvengersAgeOfUltron, AvengersInfinityWar;

… main( ) /* "Here's the story * of a titan named Thanos, * who possessed six very lovely Infinity Stones. * He used those stones * to garbage_collect( halfTheUniverse ) … ." */ { ironMan = new SuperHero( from_movie( IronMan, IronMan2, IronMan3 ) ); ironMan.state = strandedInSpace; captainAmerica = new SuperHero( fromMovie( CaptainAmericaTheFirstAvenger, CaptainAmericaTheWinterSoldier, CaptainAmericaCivilWar ) ); captainAmerica.state = stillPiningForPeggyCarter; fatThor = new SuperHero( from_movie( Thor, ThorTheDarkWorld, ThorRagnorak ) ); fatThor.state = guiltRidden; hulk = new SuperHero( cast_as( from_movie( TheIncredibleHulk ), markRuffalo ) ); hulk.state = professor;
… }

Voilà!  A two billion dollar movie — with the previous 21 doing all the heavy lifting!

Okay, now that we’ve covered some important concepts regarding the machine side of machine learning, let’s finally do some actual machine learning!  In the next post, we’re going to build a brain!

0 0 votes
Rate This Article!
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x