Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is actually a follow-up question of Yet another Pascal triangle in Python written in functional programming pattern Yet another Pascal triangle in Python written in functional programming pattern, which is put on hold for posting broken code.

Again, this is my implementation of a Pascal program, which is used to print an n-order Pascal triangle. I write it to familiarize myself with Python's Functional Programming Modules. Some changes have been made to get the code work and make it more "functional"

#!/usr/bin/env python3
import operator
from itertools import starmap, tee
def pairwise(iterable):
 """
 s -> (s0,s1), (s1,s2), (s2,s3), ...
 https://docs.python.org/3/library/itertools.html#itertools-recipes
 """
 a, b = tee(iterable)
 next(b, None)
 return zip(a, b)
def pascal_row(n):
 """
 Return a generator that yields the nth row of a Pascal triangle
 """
 if n < 2:
 return (x for x in [1])
 else:
 def dispatch():
 yield 1
 yield from starmap(operator.add, pairwise(pascal_row(n-1)))
 yield 1
 return dispatch()
def print_pascal(n):
 """
 Print an n-order Pascal triangle
 """
 if n > 0:
 print_pascal(n-1)
 print(list(pascal_row(n)))
print_pascal(10)

Output:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Please let me know if this piece of code isn't reflecting the essence of functional programming correctly.

This is actually a follow-up question of Yet another Pascal triangle in Python written in functional programming pattern, which is put on hold for posting broken code.

Again, this is my implementation of a Pascal program, which is used to print an n-order Pascal triangle. I write it to familiarize myself with Python's Functional Programming Modules. Some changes have been made to get the code work and make it more "functional"

#!/usr/bin/env python3
import operator
from itertools import starmap, tee
def pairwise(iterable):
 """
 s -> (s0,s1), (s1,s2), (s2,s3), ...
 https://docs.python.org/3/library/itertools.html#itertools-recipes
 """
 a, b = tee(iterable)
 next(b, None)
 return zip(a, b)
def pascal_row(n):
 """
 Return a generator that yields the nth row of a Pascal triangle
 """
 if n < 2:
 return (x for x in [1])
 else:
 def dispatch():
 yield 1
 yield from starmap(operator.add, pairwise(pascal_row(n-1)))
 yield 1
 return dispatch()
def print_pascal(n):
 """
 Print an n-order Pascal triangle
 """
 if n > 0:
 print_pascal(n-1)
 print(list(pascal_row(n)))
print_pascal(10)

Output:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Please let me know if this piece of code isn't reflecting the essence of functional programming correctly.

This is actually a follow-up question of Yet another Pascal triangle in Python written in functional programming pattern, which is put on hold for posting broken code.

Again, this is my implementation of a Pascal program, which is used to print an n-order Pascal triangle. I write it to familiarize myself with Python's Functional Programming Modules. Some changes have been made to get the code work and make it more "functional"

#!/usr/bin/env python3
import operator
from itertools import starmap, tee
def pairwise(iterable):
 """
 s -> (s0,s1), (s1,s2), (s2,s3), ...
 https://docs.python.org/3/library/itertools.html#itertools-recipes
 """
 a, b = tee(iterable)
 next(b, None)
 return zip(a, b)
def pascal_row(n):
 """
 Return a generator that yields the nth row of a Pascal triangle
 """
 if n < 2:
 return (x for x in [1])
 else:
 def dispatch():
 yield 1
 yield from starmap(operator.add, pairwise(pascal_row(n-1)))
 yield 1
 return dispatch()
def print_pascal(n):
 """
 Print an n-order Pascal triangle
 """
 if n > 0:
 print_pascal(n-1)
 print(list(pascal_row(n)))
print_pascal(10)

Output:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Please let me know if this piece of code isn't reflecting the essence of functional programming correctly.

Source Link
nalzok
  • 503
  • 1
  • 4
  • 16

Functional Pascal triangle in Python (working version)

This is actually a follow-up question of Yet another Pascal triangle in Python written in functional programming pattern, which is put on hold for posting broken code.

Again, this is my implementation of a Pascal program, which is used to print an n-order Pascal triangle. I write it to familiarize myself with Python's Functional Programming Modules. Some changes have been made to get the code work and make it more "functional"

#!/usr/bin/env python3
import operator
from itertools import starmap, tee
def pairwise(iterable):
 """
 s -> (s0,s1), (s1,s2), (s2,s3), ...
 https://docs.python.org/3/library/itertools.html#itertools-recipes
 """
 a, b = tee(iterable)
 next(b, None)
 return zip(a, b)
def pascal_row(n):
 """
 Return a generator that yields the nth row of a Pascal triangle
 """
 if n < 2:
 return (x for x in [1])
 else:
 def dispatch():
 yield 1
 yield from starmap(operator.add, pairwise(pascal_row(n-1)))
 yield 1
 return dispatch()
def print_pascal(n):
 """
 Print an n-order Pascal triangle
 """
 if n > 0:
 print_pascal(n-1)
 print(list(pascal_row(n)))
print_pascal(10)

Output:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Please let me know if this piece of code isn't reflecting the essence of functional programming correctly.

lang-py

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