Skip to content

Instantly share code, notes, and snippets.

@kaosf
Created July 7, 2012 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaosf/3067930 to your computer and use it in GitHub Desktop.
Save kaosf/3067930 to your computer and use it in GitHub Desktop.
LiveScript my first practice
# ref. http://d.hatena.ne.jp/mizchi/20120706/1341568588
# http://twilog.org/ka_/date-120708
global <<< require \prelude-ls
take(n, [x, ...xs]:list) =
| n <= 0 => []
| empty list => []
| otherwise => [x] +++ take n - 1, xs
# | otherwise => x & take n - 1, xs # obsolete from version 0.9.11
console.log take 2, [1 2 3 4 5] #=> [1 2]
even(x) = x % 2 == 0
invert(x) = not x
console.log even 1 #=> false
console.log even 2 #=> true
console.log invert true #=> false
console.log invert false #=> true
odd = invert << even
console.log odd 1 #=> true
console.log odd 2 #=> false
f(x) = x * 2
g(x) = x + 3
console.log (f << g) 1 #=> 8
console.log (f >> g) 1 #=> 5
f(x) = x + 1
console.log f 1 #=> 2
f = (x) -> x + 1
console.log f 1 #=> 2
console.log ((x) -> x + 1) 1 #=> 2
a = [1 2 3] |> map (* 2)
console.log a #=> [2 4 6]
# console.log [1 2 3] |> map (* 2) #=> ERROR
console.log ([1 2 3] |> map (* 2)) #=> [2 4 6]
a = [1 2 3] |> map (x) -> x * 2
console.log a #=> [2 4 6]
a = [1 2 3] |> map (x) -> ((* 2) x)
console.log a #=> [2 4 6]
mul2 = (* 2)
add2 = (+ 2)
console.log mul2 1 #=> 2
console.log mul2 2 #=> 4
console.log add2 1 #=> 3
console.log add2 2 #=> 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment