Category Archives: Uncategorized

Hacker School Day 8.Friday: On which I “implemented” a “stack” in JavaScript!

Yes!!!

This afternoon, in a matter of just a few hours, I learned:

  • What is meant by a “stack”: a data structure in which you pile things on top, much like books. These are not meant for accessing data from the earliest elements — they are intended for pushing (adding items to the top) and popping (taking items off the top).
  • What it means to “implement” your own “__________” (in this case a stack): build your own version of the thing, ideally using fewer built-in functions or structures than would be handy. (See below for an example.)
  • What a “linked list” is: (sorta) A data structure in which you store each element and it’s “pointer” to the element before it. This means in order to get from element “a” at the beginning of a stack, to element “f” that you’ve stacked up to now, you’ve got to go from “f” to “e”, then from “e” to “d”, etc., until you get back to “a”. Not sure what situations this would be super-helpful for (same way I feel about a stack), but I guess it’s good if you only need the most recent few elements? (Comments, enlightenment, and such are most welcome!)
  • Node is easy to install with homebrew: literally (first remember to update and doctor your brew):
    ~$ brew update
    ~$ brew doctor
    ~$ brew install node

    DONE! Now I can run js files in the terminal!

On “Implementing” a “Stack”:

Okay, so in JavaScript (as in most higher-level programming languages), there are built-in tools that do the stack-y things you need to do already, so a Super Cheaty Implementation of a stack in JS might be something like this:

function CheatingStack () {
	this.contents = [];
}
CheatingStack.prototype.push = function (item) {
	this.contents.push(item);
};
CheatingStack.prototype.pop = function () {
	return this.contents.pop();
}
CheatingStack.prototype.isEmpty = function () {
	return (this.contents.length === 0);
}

That makes use of built in methods push() and pop(), which put an item on and pop an item off the stack, respectively.

That sort of implementation is neither challenging nor interesting, so naturally I had to try harder. The facilitator, AO, who was helping me with this hinted that soon in SICP (the fundamental book of computer programming that I’m reading), the author will break all data down into pairs, and he showed me how pairs could just be “implemented” in JavaScript as an object with two parameters and two properties. From there, with some on-paper reasoning help from my lovely fellow student RS, I figured it out! I then tested it using console.log(), but I would like to implement more unit-testing methods that AO demonstrated. The trick (for the non-technical readers that I someday hope to have) is to create a tiny “Item” object that merely stores a value and a “pointer” (in this case, the item that comes before it). Then in your stack when you push a new element in, you make a new “item” object for it containing itself as the value and the last head object of your list as what it points to. All you have to track in your “stack” is what object you are currently pointing to. Then when you want to pop an item off the top of your list, you grab that head object, and fetch from inside it the object it’s set to point to and make that your new head. In my initial round of code, I was also storing an extra property called “prev” in my stack, but another facilitator, AK, kindly showed me that was entirely unnecessary! (Another great reason to show someone your code immediately.) For the technically inclined and curious, here’s a link to the code: Implementation of a Stack in JavaScript (gist).

Hacker School Day 3: on which I realized that every day now is the best day of my life

Today I boldly decided to challenge myself to work through SICP, the classic text for learning the foundations of computer programming. There is a group of fellow students that are working through it together, so I seized the opportunity and joined the club despite my mild terror at the density of the book and the pressure of a book club. Luckily a few other folks joined late with me today, so the group postponed discussion of the first exercises until tomorrow. I was/am tickled at the familiar challenge of a formidable book that will surely cause my brow to furrow.

The morning was quickly consumed by checkins, the SICP meeting, and some preliminary SICP reading. At noon I joined a gaggle of javascripters seeking wisdom from the uber-cool M, who let us throw all kinds of questions her way while she patiently (and humorously) demonstrated the intricate puzzles of javascript prototypal inheritance. Don’t ask me what that phrase means because I have too many disjointed thoughts on the matter to tell you anything lucid. I might assign myself a mini project of explaining (perhaps illustrating) what I think I learned there today. It was like a brain race, dizzying and exhilarating, magically shedding light into dark corners only to reveal more unexplored pathways.

After the javascript party, I set my fried brain upon some SICP exercises until a fellow student was ready to pair program in Ruby. We worked on simplifying her slick Roman Numeral converter, and I was impressed with some of my own knowledge and experience. When we hit a terrific road block and posted a help request on our in-house irc client, one of the facilitators sprung to our aid. As Z helped us rewrite our broken nested hash reference, I felt a chunk of understanding finally click into place after years of vague reliance on shadowy comprehension and unpredictable results. For the technically-inclined, a brief explanation: (please don’t judge that it’s taken so long for me to grasp this simple concept in a sturdy way)
When accessing nested elements of a hash/dictionary/associative array in a for each loop (or .each do block in Ruby), the “value” of your new (key, value) pair IS the first nested hash, whereas I thought it was the first element of that nested hash. An example will explain far better than words:

If you have the associative array (sorry, I come from a php background… also I’ve met many cats of varying sizes):

cat_weights = {
  'plump' => {'oreo' = 15, 'adina' = 11, 'monty' = 19},
  'healthy' => {'leela' = 8, 'rosie' = 9},
  'skinnypants' => {'bosch' = 7, 'ziggy' = 5, 'curie' = 6}
}

and you’re doing a do block on each of the elements of cat_weights, as such:

cat_weights.each do |size, names|
  ...
end

and you want to access adina’s weight at some point, I initially thought you’d access it within the do block with size[‘adina’]. This is wrong! Size refers to the three strings: ‘plump’, ‘healthy’, and ‘skinnypants’, NOT hashes. The VALUES of those keys are hashes, hence you would access adina’s weight with names[‘adina’], since names is the pointer for the hash full of cat names and weights (the subhash to fatness levels, aka size). BAM! Now I finally know how to predictably access nested elements in a hash. Yessss!

After an hour or more of Ruby, I attended a Git lesson and learned about merging branches. After that I worked furiously on a small SICP exercise and became ensnarled in simple logic that I failed to wield correctly. At long last, I attacked the problem differently and got a working solution. For the logically inclined: given three numbers, how do you get the sum of the squares of the largest two using only combinations of logical functions and the multiplier? I’m either dumb or it’s harder than it seems. Regardless, getting a working solution felt so great simply because it didn’t come on my first or second try.

At the tail end of the day, wobbly with hunger, I sought to push my solution to github but forgot the proper command to link up my new local master to my new remote empty repo. I sought the help of T who gladly advised me and showed me a bunch of cool tidbits for working in Scheme. In posting my code and using a new IDE (Dr Racket for Scheme), I found myself in exactly the situation that Z had taught us about earlier at the Git session: Dr Racket had created a temp file that I did not want in my commit but had already staged! Fear not: I scrunched my face and pulled from my brain the command I’d learned earlier in the day (to remove specific files from my staging area: git reset HEAD filename). Brilliant! I also created a .gitignore file–another task I had learned at the Git session. So fun to put knowledge to use IMMEDIATELY. That’s what I’m doing all day long now. This is my version of heaven.

Quick trip to Santa Fe

I’m in Santa Fe for the weekend, which is the closest thing to a “home” city that I like to have. While I live in Brooklyn, grew up in Chapel Hill, NC, and was born in California, only Santa Fe still holds a large enough familiar population to take me in its arms whenever I feel like returning. Fitting that I should think the word “familiar,” as I have no blood relations in the Fe, and all real blood relations are scattered nationwide, yet Santa Fe is indeed still my home, where I have non-blood family, and the general population suites my preferred way to live: befriend strangers and trust everyone.

In a “city” that is technically the state capital, the bus driver knows several of his passengers by name, advises a wayward soul and makes an extra stop for her, reminds a gaggle of teens that we’ve arrived at the high school and they ought to get off, and postpones the next connecting bus for two middle-aged women who are running late. While casually waiting hours at one of various bus stops today, I chat with an elderly Hispanic man who tells me in broken English that he’s from El Paso, TX, but has lived in Hawaii, California, and Mexico, Santa Fe longest of all. He’s “no concern with hours”, and is, like me, patiently waiting for a bus that may or may not ever come.

I’m now at another bus stop, downtown, awaiting the lone bus to SJC, an enigma of the locals. Somehow last night, my taxi driver pegged my SJC accent, a Johnnie himself, as it turns out. There’s what appears to be an impromptu car show along this street, with hot rods like this: Continue reading

phpfog trickiness

For anyone’s future reference:

I just moved a site to phpfog from a normal web host. It’s a site I had hand-coded in php, from scratch–my first real piece of php work, so, granted, probably not the most elegant, cooperative app ever. Anyway, in moving it to phpfog, first I figured out how to set up and use git (pretty easy, as it turns out), then today I moved the files into my new phpfog directory on my computer and “pushed” ’em up. Violá! Site looks awesome… until you click a link… And then you see my redirect page, minus all css and images. Blech.

You see, I had set up an .htaccess file to make my links slick, which I forgot to download from my original server. So, okay, downloaded it, moved it, pushed it, reloaded the page, and … rats! Still no luck.

I then spent an hour and a half making small changes and trying to figure out why when I clicked any link I lost everything… I could tell something was not working with my redirects or path variables. After a hassle and a half more, having double-checked my reg-exs and such, I searched the phpfog documentation (cool app/site, but their documentation was not obviously organized to me… I felt like I was being put through loops), and found a link to this post on David Walsh’s Blog. While this did not really address my question, I clicked the “.htaccess” tag in his topics sidebar and found this post. On a whim, I decided to try his quick fix, and HURRAH!!! He had suggested a fix for hosting with Go Daddy, and while Go Daddy is it’s own scary monster, I figured it was worth a try. Thanks bunches, David Walsh: I literally would never in a million years have figured that out on my own. I don’t know much about .htaccess, and since creating that stuff over a year ago, I’ve forgotten everything I once knew. For anyone wondering, here’s the clever fix. Add this lil’ bit o’ code at the top of your .htaccess file, before you begin your rewrites:

#Fix Rewrite
Options -Multiviews

That’s all. Feelin’ pretty good about the innernettes.

sabbatical

As my lovely boyfriend says, I am currently on “sabbatical”. It is the first time in my life that I have not had to be somewhere at a certain time nearly every day of the week, and I absolutely love it. Do I sit around and watch television? NO. Instead, I have learned stuff, worked on various art and freelance projects, read two books, and taken time to breathe.

Today I sit here, in my delightful bedroom, enjoying the color of the light. Outside feels like a spring day in January (terrifying, truly, but still pleasant to the senses). It poured a while ago, then the sun came out for a few minutes, and now a darkness has descended. Inside I have Christmas lights and a small desk lamp that illuminate the room with a soft white light. Somehow the lighting, like the day itself, is both ominous and hopeful, terrifying but comfortable. I love it.

php heredoc syntax GOTCHA

 

If you are using the heredoc syntax in php
[example:

echo <<< EOT formatted spaceEOT;

]

and you find yourself getting the following error:
parse error: syntax error, unexpected $end in (document.php) on line #
then chances are you have added a space or tab AFTER the last EOT;
I knew it was important to have no spaces and such BEFORE it on the same line, but turns out you can’t have spaces afterwards either! So watch out.