You may have read 10 CoffeeScript One Liners to Impress your Friends, inspired by a post about Scala one liners on HN. Well I wanted to impress my friends with some one liners in LiveScript, and its recommended base library, prelude.ls. You can try out all of these yourself on the LiveScript website.
Parentheses free chaining is nice.
$ \.content .find \.date-author .children \a .text!split ' ' .0 #=> 'George'
func!
is a bang call, equivalent to func()
. You don't need to use a dot to access things in non-ambiguous situations. You can make string literals of strings without spaces using a leading backlash. You can also use .0
instead of [0]
.
Partially applied operators are also nice.
map (* 2), [1 to 5] #=> [2, 4, 6, 8, 10]
You can use operators as functions in LiveScript by surrounding them in parentheses. You can also partially apply them, as shown above.
It's almost SQL!
# some data table1 = * id: 1 name: \george * id: 2 name: \mike * id: 3 name: \donald table2 = * id: 2 age: 21 * id: 1 age: 20 * id: 3 age: 26 result = [{id, name, age} for {id, name} in table1 for {id:id2, age} in table2 when id is id2] #=> [{id: 1, name: "george", age:20}, {id: 2, name: "mike", age: 21},{id:3, name: "donald", age: 26}]
You can list with asterisks. This is useful when listing implicit objects for instance. Also on display here is object destructuring.
Using pipes is a nice way to make your code more readable if you have several nested function calls.
[1 2 3] |> map (* 2) |> filter (> 3) |> fold1 (+) #=> 10
In LiveScript if the preceding item is not callable you can omit the comma when listing things, as we've done with the input list.
First, with the built in:
sum [1 to 5] #=> 15
Well that was easy. What if we didn't have sum
?
fold1 (+), [1 to 5] #=> 15
<[ ... ]>
delineates a list of one word strings.
wordList = <[ LiveScript JavaScript prelude.ls ]> tweet = 'This is an example tweet talking about LiveScript and stuff.' any (in words tweet), wordList #=> true
Here we used the operator in
, with a partially applied second argument, as a function.
If you multiply something by a string literal, it acts as a join. Eg. list * ','
acts as list.join ','
. Also in this example are list comprehensions and string interpolation.
["Happy Birthday #{if x is 2 then 'dear George' else 'to you'}" for x to 3] * ', ' #=> 'Happy Birthday to you, Happy Birthday to you, Happy Birthday dear George, Happy Birthday to you'
partition
is like a combination of filter
and reject
.
scores = [49 58 76 43 88 77 90] [passed, failed] = partition (> 60), scores passed #=> [76, 88, 77, 90] failed #=> [49, 58, 43]
Destructuring of returned values here.
There's a built in:
minimum [14 35 -7 46 98] #=> -7
What if we didn't have minimum
? We can fold with the minimum operator.
fold1 (<?), [14 35 -7 46 98] #=> -7
Determines if a number is prime. |
is an alias for when
f = -> [p.push x for x from 2 to it | not any (-> x % it is 0), (p ?= [])] and it in p f 12 #=> false f 13 #=> true
For more on LiveScript and prelude.ls, follow @gkzahariev.
comments powered by Disqus