Dependency Injection and Guice

Dependency Injection and Guice

Dependency injection, in a word, is to separate the creation of objects from its implementation.

It’s not something advanced. In fact, you are very likely to have already implemented some sort of dependency injection in your code.

For example, when implementing a server, you want to create a controller class that handles business logic. It uses a data manger class to manage the data. And the data manager in turn uses a MySQL client to manipulate the database. In a common design without dependency injection, the business logic controller will create an instance of the data manager in its constructor, and the data manager do the same in its constructor to create a MySQL client as well.

read more

Configure coc.nvim for C/C++ Development

Configure coc.nvim for C/C++ Development

coc.nvim is yet another LSP plugin, which was first released in mid-2018. Because of its powerful functionality comparable to VSCode, coc.nvim soon becomes one of the most popular Vim plugins for 2018.

I have been using YouCompleteMe for many years. And I also once used Neocomplete and Deoplete. They were good for Python and JavaScript, but when it comes to C++, YouCompleteMe was just way better than any of them. However, the first time I tried coc.nvim, I fell in love with it and now it becomes one of the must-have plugins for my Vim configuration.

For those who are not familiar with LSP, I think it’s worth a brief intro.

read more

Java 8 Optional Cheatsheet

Java 8 Optional Cheatsheet

1. I have an A a, how can I get an Optional<A> containing a?

Optional.of(a)    // a cannot be null

2. I have an A aOrNull which can be null, how can I get an Optional<A>?

Optional.ofNullable(aOrNull)

This is extremely useful to convert the traditional nullable reference to an Optional object.

read more

Adopt GitHub Style to Your Jekyll Blog

Adopt GitHub Style to Your Jekyll Blog

I have spent a lot of time trying to find a Jekyll theme that is both elegant and readable. But unfortunately, most Jekyll themes is just so unreadable that the attention is easily distracted away from the content.

I noticed that GitHub renders Markdown files in a very readable style. It is so natural that it takes no effort for the reader to pay all the attention to the content rather than the style. So I decided to migrate the GitHub style to my blog, which you have already been reading.

What we need is a GitHub-flavored markdown renderer and a GitHub CSS. I find the following posts and code very helpful and I would recommend reading them for further detail.

read more

Network Flow

Network Flow

Given a directed graph G that has a single source s and target t, where each edge has a non-negative integer capacity (the maximum of flow that can be passed through this edge), what is the maximum flow that can be passed from the source to the target?

This problem is known as max flow.

read more

Three OCaml Mistakes That A Haskeller Would Make

Three OCaml Mistakes That A Haskeller Would Make

OCaml is not an indented language

(* The wrong program. *)
let f l =
  match l with
    | [] -> Printf.printf "a"
    | [x] -> 
      match x with
        | [] -> Printf.printf "b"
        | _ -> Printf.printf "c"
    | _ -> Printf.printf "d"
in
f [[3]; [4]]

A Haskeller may think that the program above prints “d” to the screen, but actually it crashes because of the missing of patterns. Although most OCaml programs are written in a well-indented style, but, in essense, just like C or Java, the OCaml syntax is not indented. In the example above, the clause printing "d" belongs to the inner match expression, not the outer. When writing nested match expressions, always keep in mind to use parentheses.

read more

Union Find

Union Find

Assume that you have some nodes, each of which belongs to a group. How can you effectively check if two nodes are in the same group or merge two groups? UnionFind is a very fast algorithm to solve this problem. In this post, we first address how the UnionFind problem raises in a graph problem, then present the UnionFind algorithm, and analyze its time complexity in the end (which is optional).

read more

Use Emacs as Your Terminal

Use Emacs as Your Terminal

If there is anything that Emacs can do but Vim can’t, eshell must be one of them. What eshell distinguishes itself from an external terminal is that it is so deeply integrated into Emacs that you can do many things otherwise difficult. As a gentle start, all that you need to do is to run this command:

M-x eshell

If you are new to eshell, be sure to check the following features that make eshell worth a trial:

read more