Skip to content

Files

Latest commit

 

History

History
47 lines (34 loc) · 976 Bytes

0430.md

File metadata and controls

47 lines (34 loc) · 976 Bytes

How Functiony is your Python?

map/filter

  • Programming structures designed to manipulate lists
  • Preferred method is to use list comprehensions

map

Create a list by applying a function to each element in an existing list.

map(<function>, list)

filter

Create a list by filtering out elements of an existing list.

lambda expressions

  • Anonymous function creation in python
  • Similar to lambda in racket and function in javascript

Syntax

lambda arguments : expression
  • Expression must return a value; return is not used
lambda x : 2*x
lambda a, b : a if a > b else b

reduce

Compute a single value based on a list.

reduce( function, list )
  • The function argument must take 2 parameters
  • They represent the previously computed value and the next element in the list
reduce( lambda a, b: a + b, [49, 37, 163, 2])
reduce( lambda a, b: a if a > b else b, [49, 37, 163, 2])