-
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add python examples #9
base: master
Are you sure you want to change the base?
Conversation
def create_log(base): | ||
def log(n): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More direct translation would use lambdas here.
def
translates as function
in JavaScript
@@ -0,0 +1,14 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that a PEP requirement to have an empty line in the beginning of the file if there are no comments and imports?
def partial(fn, x): | ||
return lambda *args: fn(x, *args) | ||
|
||
|
||
def summary(a, b, c, d): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto, lambdas would more direct translation
return lambda *rest: fn(*(args + rest)) | ||
|
||
|
||
def summary(a, b, c, d): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the same identifacators names so a person learning different languages would have easier time understanding the analogy between JavaScript and Python
def summary(a, b, c, d): | |
def sum4(a, b, c, d): |
f1 = partial(summary, 1) | ||
f2 = partial(f1, 2) | ||
f3 = partial(f2, 3) | ||
print(f3(4)) | ||
|
||
f11 = partial(summary, 1, 2) | ||
f12 = partial(f11, 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto, let's use the same variable names
def curry(fn, *args, **kwargs): | ||
def wrapper(*iargs, **ikwargs): | ||
all_args = args + iargs | ||
all_kwargs = {**kwargs, **ikwargs} | ||
try: | ||
return fn(*all_args, **all_kwargs) | ||
except TypeError: | ||
return curry(fn, *all_args, **all_kwargs) | ||
return wrapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's cool: quite nice solution for kwargs 👍
No description provided.