Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions samples/function/lambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import math

# Function using def
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x

# Lambda function equivalent
lambda_abs = lambda x: x if x >= 0 else -x

# Function using def
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny

# Lambda function equivalent
lambda_move = lambda x, y, step, angle=0: (x + step * math.cos(angle), y - step * math.sin(angle))

# Examples
n_def = my_abs(-20)
n_lambda = lambda_abs(-20)
print(n_def, n_lambda)

x_def, y_def = move(100, 100, 60, math.pi / 6)
x_lambda, y_lambda = lambda_move(100, 100, 60, math.pi / 6)
print(x_def, y_def, x_lambda, y_lambda)


my_abs('123')