About


In [74]:
from IPython.core.display import HTML, Javascript
from IPython.core import display

Installed coffee script using npm

npm install -g coffee-script

Following is the directory structure that we are going to follow for this tutorial.


In [6]:
!ls *b


coffeescript.ipynb

tutorial:
lib
src

In [2]:
!coffee -v


CoffeeScript version 1.12.3

Help


In [1]:
!coffee -h


Usage: coffee [options] path/to/script.coffee -- [args]

If called without options, `coffee` will run your script.

  -b, --bare         compile without a top-level function wrapper
  -c, --compile      compile to JavaScript and save as .js files
  -e, --eval         pass a string from the command line as input
  -h, --help         display this help message
  -i, --interactive  run an interactive CoffeeScript REPL
  -j, --join         concatenate the source CoffeeScript before compiling
  -m, --map          generate source map and save as .js.map files
  -M, --inline-map   generate source map and include it directly in output
  -n, --nodes        print out the parse tree that the parser produces
      --nodejs       pass options directly to the "node" binary
      --no-header    suppress the "Generated by" header
  -o, --output       set the output directory for compiled JavaScript
  -p, --print        print out the compiled JavaScript
  -r, --require      require the given module before eval or REPL
  -s, --stdio        listen for and compile scripts over stdio
  -l, --literate     treat stdio as literate style coffee-script
  -t, --tokens       print out the tokens that the lexer/rewriter produce
  -v, --version      display the version number
  -w, --watch        watch scripts for changes and rerun commands

Hello World (Expression)


In [3]:
expr = "square = (x) -> x*x"

In [4]:
!coffee -c -e "$expr"


// Generated by CoffeeScript 1.12.3
(function() {
  var square;

  square = function(x) {
    return x * x;
  };

}).call(this);

In [5]:
!coffee -b -c -e "$expr"


// Generated by CoffeeScript 1.12.3
var square;

square = function(x) {
  return x * x;
};

Hello World (Script)


In [2]:
src = "tutorial/src"
lib = "tutorial/jslib"

In [51]:
%%writefile "$src/cs0.coffee"
square = (x) -> x * x


Writing tutorial/src/cs0.coffee

In [52]:
!coffee --no-header -b -p -c "$src/cs0.coffee"


var square;

square = function(x) {
  return x * x;
};

In [53]:
!coffee -o "$lib" -c "$src/cs0.coffee"

In [3]:
!cat "$lib/cs0.js"


// Generated by CoffeeScript 1.12.3
(function() {
  var square;

  square = function(x) {
    return x * x;
  };

}).call(this);

In [57]:
!ls * -R


coffeescript.ipynb

tutorial:
lib
src

tutorial/lib:
cs0.js

tutorial/src:
cs0.coffee

Function Default Arguments


In [81]:
%%writefile "$src/cs1.coffee"

fill = (container, liquid="water") -> "Filling the #{container} with #{liquid}"


Overwriting tutorial/src/cs1.coffee

In [87]:
!coffee -b -o "$lib" -c "$src/cs1.coffee"
#!cat "$lib/cs1.js"

Failed experiment

_js = !cat "$lib/cs1.js" js = Javascript("\n".join(_js)) display.display(js)

First load the java script file and then add the javascript cell magic.


In [134]:
%%javascript
//# %load "tutorial/lib/cs1.js"

// Generated by CoffeeScript 1.12.3
var fill;

fill = function(container, liquid) {
  if (liquid == null) {
    liquid = "water";
  }
  return "Filling the " + container + " with " + liquid;
};

alert(fill("Cups"));


What I have observed is that the load magic command substitutes the variable expansion!!

Is there a way to not let this happen?

%load "$lib/cs1.js" #%%javascript # %load "tutorial/lib/cs1.js"

Data Types


In [128]:
%%writefile "$src/cs2.coffee"

song = ['do', 're', 'mi', 'fa', 'so']

singers = { Jagger: "Rock", Elvis: "Roll"}

bitlist = [
    1, 0, 1
    0, 0, 1
    1, 1, 0
]

kids = 
    brother: name: "max", age: 11
    sister: 
        name: "Ida"
        age: 9


Overwriting tutorial/src/cs2.coffee

In [129]:
!coffee -b --no-header -o "$lib" -c "$src/cs2.coffee"

!cat "$lib/cs2.js"


var bitlist, kids, singers, song;

song = ['do', 're', 'mi', 'fa', 'so'];

singers = {
  Jagger: "Rock",
  Elvis: "Roll"
};

bitlist = [1, 0, 1, 0, 0, 1, 1, 1, 0];

kids = {
  brother: {
    name: "max",
    age: 11
  },
  sister: {
    name: "Ida",
    age: 9
  }
};
#%%javascript %load "$lib/cs1.js"

On Reserved Words


In [120]:
%%writefile "$src/cs3.coffee"

$('.account').attr class: 'active'


Writing tutorial/src/cs3.coffee

In [122]:
!coffee -b --no-header -o "$lib" -c "$src/cs3.coffee"
!cat "$lib/cs3.js"


$('.account').attr({
  "class": 'active'
});

Creating Objects


In [132]:
%%writefile "$src/cs4.coffee"

name = "Jim Carrey"
mask = "The Mask"
weapon = "Laughter"

superhero = {name, mask, weapon}

output = "#{turtle.name} wears an #{turtle.mask} as a mask. Watch out for his #{turtle.weapon}!"


Overwriting tutorial/src/cs4.coffee

In [133]:
!coffee -b -o "$lib" -c "$src/cs4.coffee"
!cat "$lib/cs4.js"


// Generated by CoffeeScript 1.12.3
var mask, name, output, superhero, weapon;

name = "Jim Carrey";

mask = "The Mask";

weapon = "Laughter";

superhero = {
  name: name,
  mask: mask,
  weapon: weapon
};

output = turtle.name + " wears an " + turtle.mask + " as a mask. Watch out for his " + turtle.weapon + "!";

Lexical Scoping and Variable Safety


In [136]:
%%writefile "$src/cs5.coffee"

outer = 1

changeNumbers = ->
    inner = -1
    outer = 10

inner = changeNumbers()


Writing tutorial/src/cs5.coffee

In [138]:
!coffee -b -o "$lib" -c "$src/cs5.coffee"
!cat "$lib/cs5.js"


// Generated by CoffeeScript 1.12.3
var changeNumbers, inner, outer;

outer = 1;

changeNumbers = function() {
  var inner;
  inner = -1;
  return outer = 10;
};

inner = changeNumbers();

In [ ]:
#%%javascript
%load "$lib/cs1.js"
%%writefile "$src/cs0.coffee" !coffee -b -o "$lib" -c "$src/cs1.coffee" #!cat "$lib/cs1.js" #%%javascript %load "$lib/cs1.js"

Sandbox

Why is the following command hanging the kernel?

!coffee -wbcp "$srcLoc"

!cat "$lib/cs1.js" | pygmentize

html = !pygmentize -f html -l js "$lib/cs1.js" HTML("\n".join(html))