2008/12/10

Insertion Sort in Scala

Insertion sort is a so much simple and straightforward sorting algorithm. The pseudocode looks like following:



for j ← 2 to length[A]
do key ← A[j]
i ← j - 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i ← i - 1
A[i + 1] ← key




But do it in Scala using FP is a quite interesting experience.

Firstly, the traditional way in Scala:

def insertSort(list:Array[Int]) = {
var j = 1
while(j<list.length){
val key = list(j)
var i = j-1
while(i>=0 && list(i)>key){
list(i+1) = list(i)
i=i-1
}
list(i+1)=key
println("In the middle " + list.mkString(","))
j=j+1
}
list
}


I use while rather than for, because for means more than looping.

The pure functional way requires no side-effect, which also generally means immutable and no loop.

So the first function is insert(list:List[Int], t:Int]:List[Int], which inserts a value t into a sorted list list and returns the result.

def insert(list:List[Int], t:Int):List[Int]= list match {
case Nil => List[Int](t)
case x::r if x > t=> t::x::r
case x::r => x::insert(r,t)
}


Pattern match makes this like a breeze.

Then the sorting function:

def insertSortFp(list:List[Int])={
(list :\ List[Int]())((a,b)=>insert(b,a))
}


I use foldRight here just because of my laziness.

2008/12/03

Pandora Radio, the most downloaded free app on iPhone

又来自online:

the most downloaded free application on Apple's iTunes 2008 list (sorry, it's not on the web) is Pandora Radio, the internet streaming radio service.


同样,在Pandora的Blog中,也在庆祝这一成就:




Pandora一直是我最爱的服务之一,只是似乎除了来自美国IP之外,其余国家一概由于法律/版权的原因不提供服务。我只能在办公室里满足一下。

PS: iPhone今天被偷了,欲哭无泪。

2008/12/01

辛普森一家开始恶搞苹果(Apple)

来自Online,在恶搞过Bush之后,辛普生一家盯上了Apple,最近的一期节目中便是:

"it's so sterile," gasps Lisa on seeing the new Mapple store in Springfield. (Well, they've opened everywhere else in the US, haven't they?) Bart then disses a "Steve Mobs" product announcement in front of a crowd of gaping nerds, "You think you're cool because you buy a $500 phone with a picture of a fruit on it. Well guess what? They cost 8 bucks to make and I pee on every one!"

A Mapple store employee then angrily responds, "Who dares question the boss we fired 10 years ago and then brought back!"

最后一句爆笑!!

2008/11/30

IBM patent: Pay at the table system

有一个来自IBM的古怪专利,Pay at the table system获得了美国专利局的批准

Patrons at a restaurant or bar can pay at their table using credit cards, without involving the restaurant or bar cashier and/or wait staff. Patrons are assisted using this system in dividing the bill by displaying the amount due (including tax) and allowing each patron to enter the amount they wish to pay. When the initial bill is presented, a balance due will be displayed and the indication will be provided that the bill has yet to be paid in full. As each transaction is entered, a running total will be displayed indicating the remaining balance due. When the running total reaches zero, the bill is paid in full, and an indication will be provided, such as by illuminating a green indicator light or by displaying a balance due of $0.00.
可是,这样一来,the mighty Bistromathematical 威力岂不是大减。

Real World Haskell

很多人都很兴奋的提到说,受到了从Amazon寄来的Real World Haskell

...

其实我,好几天前就通过非正规手段 (实在是辜负了大家的期望,很是对不起勤勤恳恳的作者,和辛辛苦苦的pre-reader),得到了chm版本。但是似乎最近还没有时间细读,惭愧惭愧。

2008/11/24

GWT-ENT 看上去挺有趣

GWT-Ent一个旨在把GWT带向企业级应用的项目,最近在GWT maillist中宣布了一个很有的工具-GWT AOP

GWT AOP借鉴了AspectJ 5的annotation定义,实现了5个常用的Advice:

  1. @Around
  2. @Before
  3. @After
  4. @AfterReturning
  5. @AfterThrowing
至于Pointcut的描述,GWT AOP同时支持AspectJ的pointcut表达式和Google Guice的matchclass(我和Guice不熟)。

我还没有想清楚GWT AOP能带来如何显著,壮观的效果,但是这的确是一个GWT generator的非常不错的用例。

2008/11/20

Blog on AppEngine

I am thinking of writing a blog-like application using AppEngine, just for FUN!!!

Footer

  © Blogger template 'Grease' by Ourblogtemplates.com 2008

Back to TOP