export function calculateTotal(items) {
return items.reduce(
(sum, item) => sum + item.price,
0
);
}
export function applyDiscount(total, percent) {
return total * (1 - percent / 100);
}
import {
calculateTotal,
applyDiscount
} from '../utils/math';
export function processOrder(cart, discount) {
const subtotal = calculateTotal(cart.items);
const final = applyDiscount(subtotal, discount);
return { subtotal, final };
}
import { processOrder } from '../services/order';
export async function handleCheckout(req, res) {
const result = processOrder(
req.body.cart,
req.user.discountCode
);
await saveTransaction(result);
res.json(result);
}
struct Greeter;
impl Greeter {
fn say_hello(&self) {
println!("Hello!");
}
}
fn main() {
let g = Greeter;
g.say_hello();
}
source_file [0, 0] - [13, 0]
struct_item [0, 0] - [0, 15]
name: type_identifier [0, 7] - [0, 14]
impl_item [2, 0] - [6, 1]
type: type_identifier [2, 5] - [2, 12]
body: declaration_list [2, 13] - [6, 1]
function_item [3, 4] - [5, 5]
name: identifier [3, 7] - [3, 16]
parameters: parameters [3, 16] - [3, 23]
self_parameter [3, 17] - [3, 22]
body: block [3, 24] - [5, 5]
macro_invocation [4, 8] - [4, 28]
function_item [8, 0] - [11, 1]
name: identifier [8, 3] - [8, 7]
parameters: parameters [8, 7] - [8, 9]
body: block [8, 10] - [11, 1]
let_declaration [9, 4] - [9, 20]
pattern: identifier [9, 8] - [9, 9]
expression_statement [10, 4] - [10, 18]
call_expression [10, 4] - [10, 17]
{
"name": "python",
"rules": {
"module": {
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_statement"
}
},
"_statement": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_simple_statements"
},
{
"type": "SYMBOL",
"name": "_compound_statement"
}
]
},
...
}