Tag Archives: Idea

Weight your toys

I decide to write this post because this post reminds something about my weakness when dealing with algorithm problems.

Here’s the problem, I put it to gist.


 

How to abstract reality problems to sort problems, and how to take advantage of sort to find out things that interest us?

The code above, I want to use observer design pattern ( compare_event ). Yes that’s the part to emphasise that we always want to search things interest us, if not, why we search things? So that is the pattern of human beings, and I always decouple things when I find a point that fit normal brains.

Here is the steps that I always follow to create a solution:

  1. Understand them, feel them, and try to measure them. We always have a way to quantify things, though most people die here or find a really bad way to measure things.
  2. So we have convert states to numbers in the first step. We can use the numbers to compare two things.
  3. If I want to search something, I always search things while sort them. You always do the two things together, think about it when you are paring the socks from your washing machine. Even school child does it without thinking.
  4. I interpolate tasks while each comparison. Searching itself is also a task. Each task can access the current comparison result and process state.
  5. Choose a sort algorithm which fit the situation best
  6. Launch the whole process.

 

Git Daemon Two Common Mistakes

This is a powerful function introduced since git 1.7. The git will listen to a port and receive  incoming git protocol request. When we need to auto deploy project inside a private network, it is one of the easiest way to work with. It doesn’t depend on http or ssh, and much faster. Though it doesn’t support user authentication, it still a good solution for sharing code.

Two common mistake that the git documentation doesn’t mention:

  1. A white list path must be set.
    Something like this won’t work: 
  2. The white list path should be an absolute path or ‘./‘ prefixed.
    Something like below won’t work, the  test_repo should be prefixed:   ./test_repo , or use absolute path 

A simple correct example should be like below.

Then access your server by git push git://server_addr:8123/test_repo .

I wrote a simple shell function to help setup:

 

我眼中日语里的谓语

前略,虽然顺利的通过了N2考试,但接下来12月的N1才是重头。今天是教师节,我觉得有必要写点什么来表示我对老师的感激。不善言辞,这里就写点日语学习的心得吧。

语言的最主要的作用就是跨越时间和空间传递讯息。而实现这一目标的主要手段便是对事物进行描述。中文和日语在事物描述方面我觉得最值得注意的区别就是它们描述事物的角度。中文描述对象的时候主要以主语为中心推导事物的性质,关于这点英语也是如此。但日语则一般不以主语为出发点描述事物。

举个例子,中文里我们说“我喜欢这个”,此时是站在我的角度发出喜欢的感想。日语的说法是“これが好き”,从“が”这个自动词助词就能看出语言间的差异,日语这句话表达的是这个东西具有让人喜欢的特性,日语并没有直接表明说话人本人的意见,而是间接的通过说这个东西很好来表达我喜欢它这件事,我们从“好き”这个汉子便能猜出,古代日本一开始启用“好き”的时候就是想说这东西好,所以才值得喜欢。这也解释了为什么日语常省略掉主语,因为这东西具备好的品质,任何人都可以喜欢。也即是说“これが好き”这句话只要语境不同,也可以表示妈妈喜欢这个,不局限于说话人本身。这一点体现了日语相较于中文具备某种更加客观的感情,虽然过于客观不一定是好事。日语希望抛开主观偏见来描述事物,于是到处都充满了省略了主语的句式,甚至有时候你如果强加入主语,反而会显得别扭。

由这个论点可以进一步推导日语的语序为什么和中文有如此大的差距。我认为为了方便初学者学习日语可以将日语的很多谓语动词不理解成动词,而是以描述事物性质的形容词来理解。比如“これを食べる”,这句如果传统的翻译成“吃这个”就会丢失掉很多意思,我觉得应该理解为,这个东西具备能被吃的特性,于是吃这个。这个初想你会觉得多此一举,但是如果你仔细看看“食べる”这样的一类动词的结构,就能解释为什么要有“る”这样的う段结尾假名了,从语言上来猜测,日语源自汉语,而汉语常常用“的”这个字结尾来形容事物,比如“吃的”,在“吃”后面加上“的”之后就是表示为“能吃的东西”这个意思了,日语在这点上似乎是保留了汉语的这种特性。然后他们把这个形容词特性委婉的用成了动词,“可以吃的东西”于是去吃。

知道了这些,就可以更好地理解“日语是个宾语谓语颠倒的语言”这句话了,其实它并不是颠倒了,而是把其他的东西当谓语用了。然后你发现一个有趣的问题,这语言竟然同时省略了主语又没有谓语!?听上去我这是个很疯狂的想法,但是如果行得通的话,正是因为句子里全是名词形容词,所以一句日语可以说的超长,并且似乎能无限再往成句里插入更细节的形容词。因为他们不必担心缺少主语和谓语而语法不成立,反正都可以没有。这也印证了日语的另一个特点:“听日本人说话基本就是在猜”,恩,猜的其实就是主语和谓语是什么。比如说“これが好き”,听了这句话你就要通过语境猜是谁,然后默认值就是说话人本身,然后句子里提到这东西很好,于是猜既然很好那主语大概会喜欢这东西,然后组合起来就可能是“我喜欢这个”,当然也可能是“他喜欢这个”。

而这些结论也告诉我们日语的重点在于句末,所以我们时常从句未开始翻译。

了解了这些以后初学日语的人大概会减少对日语这种异常语序的反感度。

PS:以上仅供参考,我是在毫无依据的情况下发出的思考,仅希望能提出一个看待问题的新角度。

Thoughts on naming convention

I heard a lot of voices saying that they want to make their coding style the same with the corresponding frameworks, so they study the ancestors’ codes and mimic the so called golden standards.

Keeping coding style the same with the framework is no wrong. But don’t lost your own voice when you are trying to follow other’s. 

I prefer my coding style being different from the framework that I’m using. One big advantage is that it will be much easier to distinguish which part of the codes is mine and which belongs to the API. For example, a mix of camelCase and underscore conventions:

If a new member comes to read this java snippet, he will find it useful to find the road map of this project. Sometimes I think a proper way to mix will make life easier, isn’t it?

Collective Phenomena

Inspired by this post, I wrote a CoffeeScript implementation. This is a very interesting phenomena that revealed the beautiful philosophy behinds the nature, a slight push to the collection of simplicity can make miracles : D

As you can see, each particle is just moving around on a circle orbit with the same velocity, only the initial position is different. However, when they are made a whole, things begin to be interesting.

Continue reading

Some thoughts on the software’s auto-update design pattern

There are so many ways to get the program to have the ability of auto-update. But as I’ve known, there is no way can absolutely make sure the program will update itself as expected. For example, you have defined some data type, in your program and store the data locally. And you have happily distributed your product to thousands users. Then months pass, you add some details to the basic data type. You migrate all the user data properly, and everything gone fine. Not a single complain heard from you users. Things turned complicate when the third time you update your data type. Now you have 3 types of data. Some of your users may be still the oldest version. Fortunately, you have left a tag in the data type to mark the version of the data type. So you take your time to check the version, and implement 2 method to migrate the data.

How about if this pattern continues for around 10 times? How many migration methods you should implement? Nine, of course. But if for some reason some of your users have reversed the version to an old one? From the basic principles of combination, we need 10×9 which means 90 methods to meet the requirement. That sounds crazy.

The main problem is because you can’t be sure whether all the users have always updated to the newest version. In other words, you need to find a solution to sync all the users program version and don’t hurt their data. On one can predict the future, we never know how the data finally like.

To be continued…

MSV Application Architecture

I think the general MVC architecture is not perfect enough for a complex dynamic application. The most confusing part of the MVC is it’s Controller. It receives the user sent data, sends commands to change the state of the Model, and also takes the responsibility to change the presentation of the view.

I think it could be simpler:

  • Model
  • Service
  • View

To separate each component’s responsibilities, and make the architecture more open. I replaced the Controller with the Service. The Service only handles the data that the View requests, such as REST, or sends commands to change the Model’s state. All UI logic is moved to the View, and all View requests are async based. So that we can take the full control of the representing flow the application. The front-end developers will only need the client knowledge and skills to construct the dynamic content of the application.

The disadvantages of this architecture are obvious. The client should at least supply a programmable interface. Such as the HTML based web application, the browser should support javascript and the javascript is enabled. And looser coupling means lower performance.

The better way to combine hashcodes

For example, two int numbers x and y. A simple way is like below:

But it’ll be easily result in collision. To avoid this, Joshua Bloch performed an elegant way to combine two hashcodes:

The usage of the prime numbers are the most brilliant part of the algorithm.