Skip to main content
Code Review

Return to Answer

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussed previously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

added 56 characters in body
Source Link
jonrsharpe
  • 14k
  • 2
  • 36
  • 62

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussedpreviously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

Source Link
jonrsharpe
  • 14k
  • 2
  • 36
  • 62

In terms of the functional programming paradigm, I would suggest the following for repeated:

def repeated(f, n):
 """Return the function that computes the nth application of f.
 f -- a function that takes one argument
 n -- a positive integer
 >>> repeated(square, 2)(5)
 625
 >>> repeated(cube, 2)(5)
 1953125
 """
 if n == 1:
 return f
 return lambda x: f(repeated(f, n-1)(x))

Note that, per the rule of thumb you have previously discussed, each statement can be replaced with its result and the function still works correctly.

You could also simplify the imports:

from operator import mul, pow

In terms of naming conventions, pylint doesn't like single-character names, so perhaps func and num would be better than f and n/x, but in general you don't have a problem here.

In terms of error handling, you don't currently have any. I don't think that's a problem either, though - the docstrings explain what the inputs are supposed to be, and the user should expect errors/weird behaviour if they supply anything else!

lang-py

AltStyle によって変換されたページ (->オリジナル) /