| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Define a local function. |
| 18 |
// Default arguments are like Python: |
| 19 |
local my_function(x, y=10) = x + y; |
| 20 |
|
| 21 |
// Define a local multiline function. |
| 22 |
local multiline_function(x) = |
| 23 |
// One can nest locals. |
| 24 |
local temp = x * 2; |
| 25 |
// Every local ends with a semi-colon. |
| 26 |
[temp, temp + 1]; |
| 27 |
|
| 28 |
local object = { |
| 29 |
// A method |
| 30 |
my_method(x): x * x, |
| 31 |
}; |
| 32 |
|
| 33 |
{ |
| 34 |
// Functions are first class citizens. |
| 35 |
call_inline_function: |
| 36 |
(function(x) x * x)(5), |
| 37 |
|
| 38 |
call_multiline_function: multiline_function(4), |
| 39 |
|
| 40 |
// Using the variable fetches the function, |
| 41 |
// the parens call the function. |
| 42 |
call: my_function(2), |
| 43 |
|
| 44 |
// Like python, parameters can be named at |
| 45 |
// call time. |
| 46 |
named_params: my_function(x=2), |
| 47 |
// This allows changing their order |
| 48 |
named_params2: my_function(y=3, x=2), |
| 49 |
|
| 50 |
// object.my_method returns the function, |
| 51 |
// which is then called like any other. |
| 52 |
call_method1: object.my_method(3), |
| 53 |
|
| 54 |
standard_lib: |
| 55 |
std.join(' ', std.split('foo/bar', '/')), |
| 56 |
len: [ |
| 57 |
std.length('hello'), |
| 58 |
std.length([1, 2, 3]), |
| 59 |
], |
| 60 |
} |