WATCH AND LEARN

I learn by doing and seeing

Parameter List or Curry Style?

[ ] [ Tags geek, scala ]

I’ve been playing with Scala at work here and there, but never gotten a chance to explore some of its interesting aspects until recently.

I’ve seen some Scala code that is using implicit variables here and there, noticing there are two ways a function can be declared with parameters.

1
2
def somefunction(a: Int, b: String)(implicit c: String) = { ... }
def somefunction(a: Int, b: String, c: String) = { ... }

So when do you use one over the other? What’s the advantages of using one or the other? This StackOverflow post What’s the difference between multiple parameters lists and multiple parameters per list in Scala? gave me a satisfying answer. I’d like to rephrase it myself here just so I can remember it better. Or else the link would do the job at explaining.

The answer went over couple of programming concept worth mentioning, I will briefly reiterate over the main points in my own words:

Partial function

A kind of function that is only applicable to certain input values. Partial function can be useful for error detection, and knows how to deal with invalid inputs. The emphasis is that partial function will do something else if an input is not accepted.

Partially applied function

A function that has been partially applied. Say if a function requires two parameters, we can only apply the first one but leave the second one. Later on we can apply this partially applied function with the second argument.

1
2
3
def dividedBy(a: Int, b: Int) = { a/b }
val tenDividedBy = dividedBy(10, _: Int)
tenDividedBy(10) // should be 1

Currying

It could easily be confused with partially applied function. I’d like to think of currying as a subset of partially applied function. We decompose a function with multiple parameters into a chain of functions taking one argument at a time.

So, what’s the difference?

  • Passing a block in a function can be syntactically concise
1
2
3
4
5
6
7
8
9
10
11
def eachOfMap(m: Map[String, Int])(fn: ((String, Int)) => Unit) = {
    m.foreach(fn)
}
val aMap = Map("a" -> 1, "b" -> 2, "c" -> 3)
eachOfMap(aMap) {
    case (str: String, in: Int) =>
        println(s"$str, $in")
}
// a, 1
// b, 2
// c, 3
  • Referring to an argument from previous parameters
1
2
3
def getKeysFirst(m: Map[String, Int])(k: Iterable[String] = m.keys) = {
    k.foreach(println(_))
}
  • Implicit variable cannot co-exist with regular parameters unless with multiple parameter lists

Rails Journey

[ ] [ Tags geek, rails ]

How to debug Rails

Instrumentation

1
2
 = debug(@movie)
 logger.debug(@movie.inspect)

JavaScript Gotchas

[ ] [ Tags JavaScript, geek ]

How to use JavaScript declare class

  1. Using a function
1
2
3
4
5
6
7
function Apple(type) {
    this.type = type;
    this.color = "red";
    this.getInfo = function () {
        return this.color;
    };
}
  • Instantiate an object
1
2
var apple = new Apple('macintosh);
apple.color = 'reddish';
  • Declare instance method
1
2
3
Apple.prototype.getInfo = function () {
    return this.color;
};
  • Singleton - using object literal
1
2
3
4
5
6
7
var apple = {
    type: 'hey',
    color: 'blue',
    getInfo: function () {
        return this.color;
    }
};

EC2 Instance Configuration Note

[ ] [ Tags geek ]

This post is intended to document the basic configuration I do for a typical EC2 Ubuntu instance, just want to make my life easier without googling the same thing all over again.

Setting up zsh

source

  • sudo apt-get install zsh
  • curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sudo sh
  • sudo su
  • chsh -s /bin/zsh ubuntu

EC2 Adventurous

[ ] [ Tags geek, git, tools ]

It’s already July! Omg time flies when it comes to summer time.

Update: recently I’ve been very involved in my internship project. I can’t expose the detail but basically I am working with AngularJS extensively. Though I am still very far from an experienced AngularJS developer, I must say IT IS A BEAUTIFUL WEB FRONT-END FRAMEWORK.

Currently I am working on a project called “yolyo” (temporarily name), which is based on an open source forum Discourse. Since Discourse requires quite a bit of computing resources, I decided to use Amazon EC2 as our starting point. As I realized how important it is to configure a virtual instance, I would like to dedicate this post to document all the works I’ve put into to make things work smoothly.

First of all, I would like to thank BitNami providing one-click Discourse installation to deploy on EC2. Otherwise, I would’ve still struggled with all the nitty gritty deployment detail.

How to deploy/update by Git

This has confused me for a long while. I had been used to deploy virtually everything by drag and drop for so long. Though I’ve learnt how to use Git to do versioning, I do not know how to setup the cloud server so that I can simply do git push origin master to deploy the changes.

Although I finally figured out how to use Git to deploy remote instance, I realized this workflow doesn’t work really well with Discourse. Discourse itself already contains a git repo, so I would need to push to a remote repo and deploy the instance by pulling from this remote repo. (If anyone knows a better deployment strategy, please let me know)

On remote machine

create a git repo as a transition to deploy to a designated directory

  • Copy local machine public key to the remote server (ec2 instance)
1
$ cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/amazonKey.pem ubuntu@example.com "cat >> ~/.ssh/authorized_keys"
  • Create and initialize a new git repo
1
2
$ mkdir website.git && cd website.git
$ git init --bare
  • Configure the post-receive hook in order to put the files into desired directory
1
2
3
4
5
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/website
export GIT_WORK_TREE
git checkout -f
  • Make the post-receive file executable
1
$ chmod +x hooks/post-receive
  • Configure hooks/post-update file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".

git update-server-info

is_bare=$(git config --get --bool core.bare)

if [ -z "$is_bare" ]
then
        # for compatibility's sake, guess
        git_dir_full=$(cd $GIT_DIR; pwd)
        case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi

update_wc() {
        ref=$1
        echo "Push to checked out branch $ref" >&2
        if [ ! -f $GIT_DIR/logs/HEAD ]
        then
                echo "E:push to non-bare repository requires a HEAD reflog" >&2
                exit 1
        fi
        if (cd $GIT_WORK_TREE; git diff-files -q --exit-code >/dev/null)
        then
                wc_dirty=0
        else
                echo "W:unstaged changes found in working copy" >&2
                wc_dirty=1
                desc="working copy"
        fi
        if git diff-index --cached HEAD@{1} >/dev/null
        then
                index_dirty=0
        else
                echo "W:uncommitted, staged changes found" >&2
                index_dirty=1
                if [ -n "$desc" ]
                then
                        desc="$desc and index"
                else
                        desc="index"
                fi
        fi
        if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
        then
  • Make hooks/post-update executible
1
$ chmod +x post-update

On local machine

I’m lazy to explicitly write out the steps here. I will just refer to the StackOverflow page that helped me solve this problem.

Side note

After all the above hassle I suddenly realized that I do not need any of these setup. Because I shall rely on a remote (e.q. GitHub) to host the repo, so the remote machine can easily pull from this remote repo. Though it is true that I am not currently working collaboratively with anybody else, this is a good deployment/development practice.

Reference

Readings for the Summer

[ ] [ Tags AngularJS, JavaScript, Ruby, Sinatra, geek, web ]

Hello! Summer 2013 officially started :-)

Today is the second day of the summer, and I feel pretty good about having a fresh start despite the fact that I did waste yesterday just to have beers with my friends. All in all, life’s good after the final exams, YAY!

This summer I will be working on Ruby on Sinatra, along with some light-weight front-end framework. Therefore, in order to get ready for my internship, I am setting up a reading list so that I don’t just blindly work on something without understanding the fundamentals.

Although I don’t think the project I will be working on requires any kind of sophisticated front-end work, I still want to learn the trendy hipster-ish front-end JavaScript frameworks like AngularJS, KnockoutJS, or EmberJS.

Here is the reading list of the books and the useful tutorials:

Online resources

Books reference

So far I found Ruby very magical and pseudo-intuitive to learn, but as coming from a less-dynamic programming language, I sometimes found I am doing too much work for what should’ve been done in two lines of code. I need to practice write less code in Ruby to achieve maximum efficiency. I will continue to update this list as I found useful resources.

It’s already a great start, cheers Rae!

SelfControl Saves My Final Grades

[ ] [ Tags geek, tools ]

Alright, I know the title is a bit off. I could’ve still suffered if I don’t study well enough to handle the finals. I’ve discovered a OSX application called SelfControl to prevent me from wondering on social websites occasionally for a period of time.

This app is really awesome if you are the kind of person like me who lacks self-discipline whenever there is a need to focus. Well, mainly the reason is I don’t really like the material and it is hard in general, so that pushed me off from being proactive about the finals.

Fortunately, this app is powerful enough to keep me from cracking the rules. It claims that after the rules are set, there is no way to break the rules even uninstalling the app. So there you have it! In case someone is looking for some help to get focused studying for exams.

How Long Does It Take a Noob to Master UNIX Commands?

[ ] [ Tags geek ]

This semester I am taking a self-paced UNIX class, which is a lot more fun compared to the other theoretical-ish computer courses. Sometimes I really lack motivation to learn effective UNIX commands to make my life easier. And I don’t think I’m alone, but in order to be a legit software enginner I must at least not suck at the basics.

Last week I ran to an UNIX tutor asking some dumb questions from the text, and I finally realized that piping is not restricted to a single input command! I wonder why it took me so long to notice this trivial fact, why!!!!????

Here it goes, I need to remind myself how dumb I was/am and hopefully I stay motivated to be an effective engineer.

1
$ cat someFile | comm -12 - anotherFile

It’s the hyphen that makes this works beautifully! Who would have thought of using hyphen? Who?

Internship for Summer 2013

[ ] [ Tags internship ]

I have been very busy interviewing for my internship opportunities. And it went quite well despite the fact that I got a rejection from a startup. At first, I was a little bit disappointed about how I blew the second phone interview. However, I quickly resumed my confidence as I kept getting email requesting interviews. Overall, the internship recruiting experience has been positive and I’ve learned tons of tips on things like “how to make your first impression” or “bugging the recruiters you know until you get an interview”. If it wasn’t my sister who is experienced with all of this, I could’ve been still blindly submitting my not-so-impressive resume electronically.

So I ended up taking the offer from Autodesk. Weird right? They are well-known for AutoCAD, and lots of 3D design software suite that are widely used in different industries. But why Autodesk? The most important determinant for me is the degree of impact I would make for a job, as well as the level of experience I would learn from the developers. Also, I originally set up a list of the language or frameworks that I want to learn/master for this summer. It turns out that I am going to use Ruby on Rails as the back-end to develop the project, which is great. Because I really need to learn Ruby on Rails by practical implementation.

I just need to take good care of the rest of the semester and occasionally learn Ruby on Rails from time to time before the first day of my internship! Woot woot!