Home > coffeescript, Programming > Learning CoffeeScript

Learning CoffeeScript

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

Advertisements
Categories: coffeescript, Programming
  1. June 26, 2012 at 10:20 pm

    Hi Rico,

    This is Chris Smith, author of the Backbone + CoffeeScript interactive book (http://www.scriptbooks.com). Awesome to come across your blog here!

  2. ricoch3n
    June 27, 2012 at 2:30 am

    Thanks Chris. I love every moment learning backbone.js through your (by far the best) one of a kind book covering the technologies that I enjoy learning. On one the week nights I literally went through the whole chapter 1 with php as the backend (just a convenient choice). Everything works great except with eco v1.1.0-pre I can’t use eco.compile, the workaround I found is to use the following fix (for instance):

    ShowView=Backbone.View.extend
        template: '''
            <h3>
                <%= @model.get('subject') %>
            </h3>
            <p>
                <%= @model.get('message') %>
            </p>
            <a href="#" class="btn">
                Back
            <a>
        '''
    
        render: ->
            @$el.html eco.render(@template, {model:@model})
            @
    
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: