Saturday, March 28, 2015

3/28/15 - Permutations in Ruby and Python

Right at the end of class on Friday I finished my ruby permutations implementation, but didn't get a chance to blog it.
Sam also wrote a python version and explained the yield statement to me. I even wrote about it in Thursday's post, but I still don't really understand what is happening here. I'll try to get around to looking a bit more closely at the code and figure out how it works.

Friday, March 27, 2015

3/27/15 - Strange Javascript

Finn had posted this code snippet on his blog a couple of days ago, and Kevin wanted to talk to me about it If you actually run this in a terminal, javascript throws an error: Uncaught TypeError: Cannot read property 'otherVariable' of undefined. This is attempting to treat a javascript function directly as an object, which isn't allowed. If you do console.log(variables) it prints:
function variables(){
  this.number     = 1;
  this.stringVariable = "test";
  this.otherVariable = 2.0;
}
and console.log(variables()); [notice the () after variables] prints:
undefined
In order for this to work as intended, you need to get an object instance from this class:
var variablesObj = new variables
Then console.log(variablesObj) ->
variables {number: 1, stringVariable: "test", otherVariable: 2}

So now we can do variablesObj.number, variablesObj.stringVariable, etc.
It's also good to note that if you set variablesObj.number = 2, the field will update. The initial code of setting number to 1 is only run once, when the object is first instantiated with the new keyword.

Thursday, March 26, 2015

3/26/15 - Tree Isomorphism

Today, Sam and I worked on UMD contest problem 4 from 2013, Tree Isomorphism. Basically, it gives you two string representations of trees, which could have any number of child nodes, and asks you to develop an algorithm to determine whether the trees have the same "structure", disregarding the actual data of the nodes.

First, we developed a TreeNode class, and started writing an algorithm to check if the trees were the same. Originally I figured we could just do a traversal of each tree and see if the connections were the same, but Sam pointed out that this approach would only work if the trees had the exact same structure in the exact order, which isn't necessarily the case. Instead, we wrote a recursive method that would generate all the possible orderings of child ListNodes for each parent ListNode, in a tree. Then each of these permutations was checked against the other tree, because if they were isomorphic, one of the permutations of tree A would eventually be an exact match for tree B.

The method generating all the permutations was the most difficult part, and we ended up having to look up how to do this as a hint. What our finished product did was traverse the nodes from root to leaves, and removing one element at a time from the list of nodes, then generating all permutations of the remaining nodes by making a recursive function call until that had been done for all elements.  The element that had been removed initially was then stuck back onto the beginning of the list.
It was tough, but Sam kindly helped me through it and did a good job of letting me think for myself. Recursion is something I have a lot of trouble picturing in my head, so hopefully I will have some more time to really understand how our permutations method works (In theory I understand it, but I'm not sure I could explain it to someone–I think my explanation in this post is probably missing some things)

Sam also re-wrote the permutations method in python, which was much easier to read. He also showed me the yield keyword in python, which is something I had come across in ruby but never really understood. Basically it is used with what is called a generator, a class that is iterable and generates values on the fly. Each time the code calling the generator increments, the next yield statement is evaluated and the value is sent to the function calling the generator.

It looks like yield in ruby is a bit different. Instead of being used to generate data to be iterated over, yield statements in ruby act as a way to pass data into a block that has been passed to the function. I actually don't like this, because it's all very implicit and hard to understand (at least as someone unfamiliar with this behavior). If you're unfamiliar, in ruby curly braces can be used to pass blocks of code into functions, so the statement foo {...} passes the code block {...} into the method foo, even though foo isn't defined to take any arguments.

Wednesday, March 25, 2015

3/25/15 Karma Setup

Apparently, I didn't actually follow all the steps I wrote about in my blog post yesterday (or it got deleted? I'm not sure) but I had to reinstall the karma-firefox adapter and the karma-mocha adapter.  I also updated our unit tests slightly.

I also signed up for the codementor.io Firefox OS workshop.

Tuesday, March 24, 2015

3/24/15 - Karma Setup

We worked more on setting up Karma and Mocha.

First I ran karma init, which asks a series of questions about what you want Karma to do, and generates a karma.conf.js file based on your responses.  I also had to add some additional stuff myself.  Here's what Finn and I ended up with at the end of the period today.

 Next, I added mocha and chai to the frameworks array in the config. Several dependencies also need to be downloaded through npm. We got karma-firefox-launcher (npm install karma-firefox-launcher) so that karma could start a firefox instance, and karma-mocha so that karma can talk to mocha (npm install karma-mocha).

We also got chai, which is a library that allows you to do #expect(), #should(), and other test-related methods in mocha tests with npm install chai --save-dev. We then got the karma-chai adapter (npm install karma-chai --save-dev)

Note: I'm not sure what the --save-dev flag does, but it apparently logs it as a dependency in some sort of json file (I assume this is more geared towards node projects though). There's also the -g flag, which installs globally.

In order to make our lives easier, we also got karma-cli, which allows you to run karma directly from the command line.

With that done, you should be able to run karma start to start a web server with karma, and then in a new terminal, karma run. Make sure you run from the librifox directory so that karma will be able to find the karma.conf.js file.

Finn also expanded the testing of #stripHTMLTags (since that's the most easily testable method right now!)

One problem I see with this is if anyone else wants to run tests in our repo, they have to install all these dependencies, which is annoying for them.

Monday, March 23, 2015

3/23/15 - Mocha and Karma

Today Finn and I worked on setting up our test suite.  We're using Mocha, which I like because it is syntactically very similar to rspec, the ruby test suite I used for NSFPatents2.  We're also using Karma--since Mocha runs on node.js, things like window and jQuery are undefined, and our javascript would of course throw errors because of this and cause our tests to fail.

Here's Finn's breakdown of how we got Mocha and Karma to work.

Karma is a wrapper for Mocha that allows the tests to run in an actual browser environment, so assumptions that are made in our code like 'there is a window we can add elements to' will be valid (node.js is a server, so it doesn't have any of that). Right now we are trying to figure out how to have our app.js and eventually other files accessible so that we can test them.

Also, I'm not sure exactly how Karma handles things, but I would imagine we need to set it to test in Firefox.  Fxpay, which I'm using as reference to try to set this stuff us, does this here in their Karma config file.  I think we'll also need to install this plugin for Karma (This page was also helpful).

I'm excited to start testing because I think it will give us a reason to really spend a lot of time improving the structure of our code. For example, methods like this are pretty much untestable in their current state. I'm thinking that we should have separate objects that handle page generation for each of our pages. I'm not sure how object inheritance works in javascript (or if it even exists) though.

Friday, March 20, 2015

3/20/15 - Mocha, parseHTML

First of all, I messed up a commit to the repo.  I forgot to save my changes in WebIDE before committing, so this was added, which is definitely not what we want!  I committed the intended code, which makes a couple of changes (I love the branch compare feature, by the way!).  First of all, I pulled out the html stripping code into a dedicated method.  My two options seemed to be using either regular expressions or jQuery for the stripping.  I opted to use a regex, because jQuery has some weird behavior that I would not want hidden away and waiting to cause problems for code using #stripHTMLTags.  For example, passing a string like 'abcdef' does something unexpected: $('abcdef').text() returns "", I guess because 'abcdef' isn't valid html and so it just...ignores everything.  At first, I fixed this by ensuring the string was always enclosed in html tags: $('<html>' + string + '</html>').text() to ensure that in the case of no tags being present, the string is still returned.  I realized that this approach ugly and probably brings up other issues.  The regex, on the other hand, is very transparent in what it is doing, and easy to test as well (I'm using rubular for this because I can directly link to saved regex tests), so I chose to use it over jQuery.

Also, somehow we fixed the search function bug where it would only allow one search, even though we don't know what fixed it.  While it's good that it's fixed, it's worrying that seemingly unrelated functions are intertwined.

Also, I started looking at Mocha, but we ran out of time.  I had a little blast of nostalgia though, because I was looking over my tests from NSFPatents2, which I wrote over the summer.