mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-05 14:28:31 +00:00
Livescript is Updated
This commit is contained in:
parent
b86ea4d51d
commit
9f609e23e6
@ -22,7 +22,8 @@ Feedback is always welcome, so feel free to reach me over at
|
|||||||
[@kurisuwhyte](https://twitter.com/kurisuwhyte) :)
|
[@kurisuwhyte](https://twitter.com/kurisuwhyte) :)
|
||||||
|
|
||||||
|
|
||||||
```coffeescript
|
coffeescript
|
||||||
|
------------
|
||||||
# Just like its CoffeeScript cousin, LiveScript uses number symbols for
|
# Just like its CoffeeScript cousin, LiveScript uses number symbols for
|
||||||
# single-line comments.
|
# single-line comments.
|
||||||
|
|
||||||
@ -30,8 +31,9 @@ Feedback is always welcome, so feel free to reach me over at
|
|||||||
Multi-line comments are written C-style. Use them if you want comments
|
Multi-line comments are written C-style. Use them if you want comments
|
||||||
to be preserved in the JavaScript output.
|
to be preserved in the JavaScript output.
|
||||||
*/
|
*/
|
||||||
```
|
|
||||||
```coffeescript
|
coffeescript
|
||||||
|
------------
|
||||||
# As far as syntax goes, LiveScript uses indentation to delimit blocks,
|
# As far as syntax goes, LiveScript uses indentation to delimit blocks,
|
||||||
# rather than curly braces, and whitespace to apply functions, rather
|
# rather than curly braces, and whitespace to apply functions, rather
|
||||||
# than parenthesis.
|
# than parenthesis.
|
||||||
@ -42,12 +44,11 @@ Feedback is always welcome, so feel free to reach me over at
|
|||||||
########################################################################
|
########################################################################
|
||||||
|
|
||||||
# Lack of value is defined by the keyword `void` instead of `undefined`
|
# Lack of value is defined by the keyword `void` instead of `undefined`
|
||||||
void # same as `undefined` but safer (can't be overridden)
|
void ,same as `undefined` but safer (can't be overridden)
|
||||||
|
|
||||||
# No valid value is represented by Null.
|
# No valid value is represented by Null.
|
||||||
null
|
null
|
||||||
|
|
||||||
|
|
||||||
# The most basic actual value is the logical type:
|
# The most basic actual value is the logical type:
|
||||||
true
|
true
|
||||||
false
|
false
|
||||||
@ -56,10 +57,9 @@ false
|
|||||||
on; off
|
on; off
|
||||||
yes; no
|
yes; no
|
||||||
|
|
||||||
|
|
||||||
# Then you get numbers. These are double-precision floats like in JS.
|
# Then you get numbers. These are double-precision floats like in JS.
|
||||||
10
|
10
|
||||||
0.4 # Note that the leading `0` is required
|
0.4,Note that the leading `0` is required
|
||||||
|
|
||||||
# For readability, you may use underscores and letter suffixes in a
|
# For readability, you may use underscores and letter suffixes in a
|
||||||
# number, and these will be ignored by the compiler.
|
# number, and these will be ignored by the compiler.
|
||||||
@ -67,7 +67,7 @@ yes; no
|
|||||||
|
|
||||||
|
|
||||||
# Strings are immutable sequences of characters, like in JS:
|
# Strings are immutable sequences of characters, like in JS:
|
||||||
"Christina" # apostrophes are okay too!
|
"Christina", apostrophes are okay too!
|
||||||
"""Multi-line
|
"""Multi-line
|
||||||
strings
|
strings
|
||||||
are
|
are
|
||||||
@ -76,7 +76,7 @@ yes; no
|
|||||||
|
|
||||||
# Sometimes you want to encode a keyword, the backslash notation makes
|
# Sometimes you want to encode a keyword, the backslash notation makes
|
||||||
# this easy:
|
# this easy:
|
||||||
\keyword # => 'keyword'
|
\keyword # => 'keyword'
|
||||||
|
|
||||||
|
|
||||||
# Arrays are ordered collections of values.
|
# Arrays are ordered collections of values.
|
||||||
@ -93,7 +93,7 @@ fruits = [ \apple, \orange, \pear ]
|
|||||||
fruits = <[ apple orange pear ]>
|
fruits = <[ apple orange pear ]>
|
||||||
|
|
||||||
# You can retrieve an item by their 0-based index:
|
# You can retrieve an item by their 0-based index:
|
||||||
fruits[0] # => "apple"
|
fruits[0] # => "apple"
|
||||||
|
|
||||||
# Objects are a collection of unordered key/value pairs, and a few other
|
# Objects are a collection of unordered key/value pairs, and a few other
|
||||||
# things (more on that later).
|
# things (more on that later).
|
||||||
@ -107,19 +107,19 @@ person =
|
|||||||
person = {name: "Christina", likes: ["kittens", "and other cute stuff"]}
|
person = {name: "Christina", likes: ["kittens", "and other cute stuff"]}
|
||||||
|
|
||||||
# You can retrieve an item by their key:
|
# You can retrieve an item by their key:
|
||||||
person.name # => "Christina"
|
person.name # => "Christina"
|
||||||
person["name"] # => "Christina"
|
person["name"] # => "Christina"
|
||||||
|
|
||||||
|
|
||||||
# Regular expressions use the same syntax as JavaScript:
|
# Regular expressions use the same syntax as JavaScript:
|
||||||
trailing-space = /\s$/ # dashed-words become dashedWords
|
trailing-space = /\s$/ # dashed-words become dashedWords
|
||||||
|
|
||||||
# Except you can do multi-line expressions too!
|
# Except you can do multi-line expressions too!
|
||||||
# (comments and whitespace just gets ignored)
|
# (comments and whitespace just gets ignored)
|
||||||
funRE = //
|
funRE = //
|
||||||
function\s+(.+) # name
|
function\s+(.+) # name
|
||||||
\s* \((.*)\) \s* # arguments
|
\s* \((.*)\) \s* # arguments
|
||||||
{ (.*) } # body
|
{ (.*) } # body
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
@ -134,7 +134,6 @@ funRE = //
|
|||||||
4 / 2 # => 2
|
4 / 2 # => 2
|
||||||
3 % 2 # => 1
|
3 % 2 # => 1
|
||||||
|
|
||||||
|
|
||||||
# Comparisons are mostly the same too, except that `==` is the same as
|
# Comparisons are mostly the same too, except that `==` is the same as
|
||||||
# JS's `===`, where JS's `==` in LiveScript is `~=`, and `===` enables
|
# JS's `===`, where JS's `==` in LiveScript is `~=`, and `===` enables
|
||||||
# object and array comparisons, and also stricter comparisons:
|
# object and array comparisons, and also stricter comparisons:
|
||||||
@ -185,7 +184,6 @@ two!
|
|||||||
# The `:=` operator is available to *reuse* a name from the parent
|
# The `:=` operator is available to *reuse* a name from the parent
|
||||||
# scope.
|
# scope.
|
||||||
|
|
||||||
|
|
||||||
# You can destructure arguments of a function to quickly get to
|
# You can destructure arguments of a function to quickly get to
|
||||||
# interesting values inside a complex data structure:
|
# interesting values inside a complex data structure:
|
||||||
tail = ([head, ...rest]) -> rest
|
tail = ([head, ...rest]) -> rest
|
||||||
@ -194,7 +192,7 @@ tail [1, 2, 3] # => [2, 3]
|
|||||||
# You can also transform the arguments using binary or unary
|
# You can also transform the arguments using binary or unary
|
||||||
# operators. Default arguments are also possible.
|
# operators. Default arguments are also possible.
|
||||||
foo = (a = 1, b = 2) -> a + b
|
foo = (a = 1, b = 2) -> a + b
|
||||||
foo! # => 3
|
foo! # => 3
|
||||||
|
|
||||||
# You could use it to clone a particular argument to avoid side-effects,
|
# You could use it to clone a particular argument to avoid side-effects,
|
||||||
# for example:
|
# for example:
|
||||||
@ -205,24 +203,22 @@ a = { a: 1 }
|
|||||||
copy a, { b: 2 } # => { a: 1, b: 2 }
|
copy a, { b: 2 } # => { a: 1, b: 2 }
|
||||||
a # => { a: 1 }
|
a # => { a: 1 }
|
||||||
|
|
||||||
|
|
||||||
# A function may be curried by using a long arrow rather than a short
|
# A function may be curried by using a long arrow rather than a short
|
||||||
# one:
|
# one:
|
||||||
add = (left, right) --> left + right
|
add = (left, right) --> left + right
|
||||||
add1 = add 1
|
add1 = add 1
|
||||||
add1 2 # => 3
|
add1 2 # => 3
|
||||||
|
|
||||||
# Functions get an implicit `it` argument, even if you don't declare
|
# Functions get an implicit `it` argument, even if you don't declare
|
||||||
# any.
|
# any.
|
||||||
identity = -> it
|
identity = -> it
|
||||||
identity 1 # => 1
|
identity 1 # => 1
|
||||||
|
|
||||||
# Operators are not functions in LiveScript, but you can easily turn
|
# Operators are not functions in LiveScript, but you can easily turn
|
||||||
# them into one! Enter the operator sectioning:
|
# them into one! Enter the operator sectioning:
|
||||||
divide-by-two = (/ 2)
|
divide-by-two = (/ 2)
|
||||||
[2, 4, 8, 16].map(divide-by-two) .reduce (+)
|
[2, 4, 8, 16].map(divide-by-two) .reduce (+)
|
||||||
|
|
||||||
|
|
||||||
# Not only of function application lives LiveScript, as in any good
|
# Not only of function application lives LiveScript, as in any good
|
||||||
# functional language you get facilities for composing them:
|
# functional language you get facilities for composing them:
|
||||||
double-minus-one = (- 1) . (* 2)
|
double-minus-one = (- 1) . (* 2)
|
||||||
@ -233,11 +229,10 @@ double-minus-one = (- 1) . (* 2)
|
|||||||
double-minus-one = (* 2) >> (- 1)
|
double-minus-one = (* 2) >> (- 1)
|
||||||
double-minus-one = (- 1) << (* 2)
|
double-minus-one = (- 1) << (* 2)
|
||||||
|
|
||||||
|
|
||||||
# And talking about flow of value, LiveScript gets the `|>` and `<|`
|
# And talking about flow of value, LiveScript gets the `|>` and `<|`
|
||||||
# operators that apply a value to a function:
|
# operators that apply a value to a function:
|
||||||
map = (f, xs) --> xs.map f
|
map = (f, xs) --> xs.map f
|
||||||
[1 2 3] |> map (* 2) # => [2 4 6]
|
[1 2 3] |> map (* 2) # => [2 4 6]
|
||||||
|
|
||||||
# You can also choose where you want the value to be placed, just mark
|
# You can also choose where you want the value to be placed, just mark
|
||||||
# the place with an underscore (_):
|
# the place with an underscore (_):
|
||||||
@ -251,7 +246,6 @@ div = (left, right) -> left / right
|
|||||||
div-by-two = div _, 2
|
div-by-two = div _, 2
|
||||||
div-by-two 4 # => 2
|
div-by-two 4 # => 2
|
||||||
|
|
||||||
|
|
||||||
# Last, but not least, LiveScript has back-calls, which might help
|
# Last, but not least, LiveScript has back-calls, which might help
|
||||||
# with some callback-based code (though you should try more functional
|
# with some callback-based code (though you should try more functional
|
||||||
# approaches, like Promises):
|
# approaches, like Promises):
|
||||||
@ -263,7 +257,6 @@ console.log a + b
|
|||||||
# Same as:
|
# Same as:
|
||||||
readFile 'foo', (a) -> readFile 'bar', (b) -> console.log a + b
|
readFile 'foo', (a) -> readFile 'bar', (b) -> console.log a + b
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
## 4. Patterns, guards and control-flow
|
## 4. Patterns, guards and control-flow
|
||||||
########################################################################
|
########################################################################
|
||||||
@ -290,7 +283,6 @@ take = (n, [x, ...xs]) -->
|
|||||||
| n == 0 => []
|
| n == 0 => []
|
||||||
| _ => [x] ++ take (n - 1), xs
|
| _ => [x] ++ take (n - 1), xs
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
## 5. Comprehensions
|
## 5. Comprehensions
|
||||||
########################################################################
|
########################################################################
|
||||||
@ -309,7 +301,6 @@ evens = [x for x in oneToTwenty when x % 2 == 0]
|
|||||||
# back an object rather than an Array:
|
# back an object rather than an Array:
|
||||||
copy = { [k, v] for k, v of source }
|
copy = { [k, v] for k, v of source }
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
## 4. OOP
|
## 4. OOP
|
||||||
########################################################################
|
########################################################################
|
||||||
|
Loading…
Reference in New Issue
Block a user