Posts

Spring DI with different styles

Spring as a dependency injection (DI) framework allows us to define our application components' dependency tree in three different ways: XML Annotation Java Config I've written a simple app, bookstore, with the three styles and they're available in the following repository. You can take a look and see how each style would look like. It also has a version that uses no Spring beans for comparison. https://github.com/ryu1kn/spring--working-with-beans Different styles have different pros/cons. Spring lets you mix the styles; so you can change it as you go. Here is my observations on each style. XML-based configuration Pros Weak/loose coupling with Spring framework in your app code. Good for keeping the option of removing Spring later Class dependency information is centralised. Fine-grained control on the dependency definition. Changing only the dependency information doesn't require the recompilation of your app code. Cons Unless you have a good IDE ...

Leverage shell commands to quickly edit text on Visual Studio Code

Image
I really like vi. When I started using Visual Studio Code as my main editor shortly after it hit version 1, one thing I felt missing in Code was the ability to quickly edit text using various commands already available on your shell. For example, let’s say you’re editing a section of text that looks like following. Contributors: - Mike, 3 PRs - Elizabeth, 5 PRs - Robert, 2 PRs - Mary, 10 PRs If you want to order the contributors by the amount of contributions, in vi, you can utilise shell’s sort command: Select the list of contributors Execute a shell command sort -t, -k 2 -n -r on the visual mode Then you’ll get Contributors: - Mary, 10 PRs - Elizabeth, 5 PRs - Mike, 3 PRs - Robert, 2 PRs If you want to drop the number of PRs from the list, you can just pipe the sort output to cut command (e.g. sort -t, -k 2 -n -r | cut -d, -f1 ), because it’s just a shell command! Contributors: - Mary - Elizabeth - Mike - Ro...

Terraform

Organise your infrastructure with terraform How to create reusable infrastructure with Terraform modules How to manage Terraform state But HIL is somehow limited Support for nested maps in variables #2114 Type system to support list and map element types Intermediate variables (OR: add interpolation support to input variables) Use null_data_source Can't create an output map

Golang way

I want to convert a list of type T1 to a list of type T2. Do I convert its element one-by-one? Yes, simply iterate over the whole list. cf. Type converting slices of interfaces in go Does a Go project needs to be under GOPATH ? I wrote a tool in Go and want to check it into an exising repository, but don't want to put the repo under GOPATH as it's mostly written in non-Go languages Add an appropriate place to GOPATH every time you run go commands. GOPATH for a project written in several languages (for example Go and Python)? AWS SDK for Go exposes struct s but not many interface s hence hard to mock in unit tests. Define interfaces as you need and use AWS SDK through them. cf. Discussion: How should you unit test code which uses the aws-sdk-go Where is list manipulation functions like filter , map , reduce , ...? Use for loop. cf. Map, reduce and filter in Go? , Writing type parametric functions in Go Dereferencing a nil pointer doesn't cause an error...

Use SWI-Prolog's package manager

SWI-Prolog has its own package manager ( prolog_pack ). Available packages are listed here: Available packages I was looking for a way to write lambda expressions in Prolog and found lambda pack on that list. To install/use the package, I rebuilt my swi-prolog to have archive.pl availbale. Install libarchive-devel (I use openSUSE with zypper, change the command according to your environment) $ sudo zypper install libarchive-devel Fetch the git repository (Or get the source from the official page ) $ git clone git://www.swi-prolog.org/home/pl/git/pl.git Follow the instructions in the repository To download configuration files, submodules and documentations. Do prepare first. $ cd pl $ ./prepare configure , make and make_install in the src directory $ cd src $ ./configure $ make $ make install configure , make and make_install in the packages directory $ cd ../packages $ ./configure $ make $ make install Make sure that after you do configure , you have H...

Vim + Syntastic + JSHint

Vim で JSHint を使えるようにする話。 Vim でコードのチェックをするのにどのプラグインを使うかで、評判のよい Syntastic を使うことにした。GitHub で公開されてて、 Vim への追加も簡単。 1. JSHint をインストール まずコマンドラインから JSHint を使えるようにする。 $ npm install jshint -g Stackoverflow の以下の投稿で、 JSHint を vim で使うための方法が書かれてる。 javascript - VIM + JSLint? - Stack Overflow, Community-driven jshint.com (better than JSLint) ここでは .jshintrc というファイルを $HOME に置くようにと言ってる。そしてそのサンプルが紹介されてる。ただしこのサンプル、プロパティ "predef" 内に余分なカンマ (,) があり(22行目)、このままだと Syntastic を使うときに正しく文法チェックをしてくれないので、カンマは消した上で保存した。 早速 JSHint を使ってみる。試しに何か文法エラーを含む JavaScript ファイルを作ってそれをチェックする。 $ jshint js-with-errors.js ソースコードの問題点がターミナルに列挙されていればok。 2. Syntastic を Vim に追加 自分は Vim でのパッケージ管理に NeoBundle を使ってる。なので、 .vimrc に次のように書き込み Syntastic を追加する。 NeoBundle 'scrooloose/syntastic' .vimrc を保存し、 vim を再起動。 :NeoBundleInstall で Syntastic のインストールが完了。 3. Syntastic の設定 Syntastic は JavaScirpt 以外にも色々な言語のソースをチェックすることができる。以下では JavaScript のチェックだけ自動で行われるようにしてる。また、 JavaScript のソースチェックに JSHint を使いたいので、 ...