Archive

Archive for June, 2012

PHP: removed trailing zeros from numeric strings (code illustration)

June 14, 2012 1 comment
<?php
	$nums=array('1.0', '2.300', '4', '5.9000');

	echo "Original format:\n";
	foreach($nums as $num) {
		echo "$num\n";
	}

	echo "\nTrailing zeros removed:\n";
	foreach($nums as $num) {
		echo ($num+0)."\n";
	}

Output:
Original format:
1.0
2.300
4
5.9000

Trailing zeros removed:
1
2.3
4
5.9

Advertisements
Categories: php, Programming

Learning CoffeeScript

June 10, 2012 2 comments

Practice 1: Define a Student class, make some new instances and classify them into two arrays based on the student scores

class Student
    constructor:(@gender, @score)->

s1=new Student('M', 45)
s2=new Student('F', 78)
s3=new Student('F', 59)
s4=new Student('M', 99)

passed=[]
failed=[]
(if s.score>59 then passed else failed).push s for s in [s1, s2, s3, s4]
console.log passed
console.log failed

Practice 2: Array filtering

pr=(items, lbl)->
    console.log "==#{lbl}=="
    for item, i in items
        console.log "Item #{i}: #{item}"
    false

a=[1..9]

odd=(item for item in a when item%2)
even=(item for item in a when item%2==0)
double=(item*2 for item in a)

pr odd, "odd"
pr even, "even"
pr double, "double"

Practice 3: more about class

class Candidate
    constructor:(@approval_rate)->

class Election
    elect:(candidate)->
        candidate.approval_rate>0.5


e=new Election
console.log e.elect( new Candidate(0.34) )
console.log e.elect( new Candidate(0.59) )

Practice 4: serial_maker (based on
JavaScript: The Good Parts by Douglas Crockford
Chap 4.12)

class SerialMaker
    constructor:(@prefix='Q', @start=0)->
    gen_seq:()->
        @start+=1
        @prefix+@start

sm=new SerialMaker('SQ')
console.log sm.gen_seq()
console.log sm.gen_seq()
console.log sm.gen_seq()

Practice 5: (added 7/12/2012) Self-invoking constructor

class Person
    constructor:(name='no name')->
        return new Person(name) if !(@ instanceof Person)
        @name=name

    id:->
        return @name

    isrich:true


p1=Person('Freedom')
# or the above line can be written as 
# p1=new Person('Freedom')
# the result is exactly the same and the
# benefit is user doesn't need to worry about forgetting using the "new" keyword
console.log p1.id() + ','+ p1.isrich

Reference: http://arcturo.github.com/library/coffeescript/index.html

Categories: coffeescript, Programming

Make your tmux look and work better

June 2, 2012 1 comment

I love using tmux. Besides the excellent scripting support (see my other post ), tmux is quite easy to customize to fit your need.

Here’s my tmux config: https://github.com/midnightcodr/my_tmux_conf/blob/master/.tmux.conf

The screenshot: https://github.com/midnightcodr/my_tmux_conf/blob/master/my_tmux_status_bar.png

To use, follow https://github.com/midnightcodr/my_tmux_conf/blob/master/README.md

Notes:
1. Credit goes to http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2/
2. I found using different color settings the most effect way to avoid accidental mistakes when working with test and production server, for example for production environment I use white on red (convenient color for caution?) for the current window tab color, for test environment I use black on yellow for the same element.

Feed-backs and comments are welcome!

[ UPDATE 6/6/2012 ] Finally I came up with the following status line that works on both Mac OSX and Linux regardless what the output of command uptime is:
set -g status-right '#[fg=yellow]#(uptime | egrep -o "[0-9]+ users?, +load.*"|perl -pe "s| averages?||"), %H:%M'

The challenge I found setting tmux status with command lines is that group capturing regex won’t work with tmux. For example, the following config set -g status-right '#[fg=yellow]#(uptime|perl -pe "s#.*,(\s+\d+ users.*)#\1#;s# averages?##"), %H:%M'
would end up 1saverages?#"), 23:44 showing in the status bar (right corner).

Categories: Uncategorized