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
|
||||||
@ -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
|
||||||
@ -205,7 +203,6 @@ 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
|
||||||
@ -222,7 +219,6 @@ identity 1 # => 1
|
|||||||
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,7 +229,6 @@ 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
|
||||||
@ -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