26. Using the higher order function reduce(), write a function max_in_list() that takes a list of numbers and returns the largest one. Then ask yourself: why define and call a new function, when I can just as well call the reduce() function directly?

from functools import reduce

def max_in_list(num_list):
def max_of_two(a, b):
return a if a >= b else b
biggest = float("-inf")
return reduce(max_of_two, num_list, biggest) print(max_in_list([100,-2,3,4,5]))

【june】reduce和map是函数式编程最明显的特点,也是两个相反的过程。这个函数在真实的编程中用的并不多,完全可以用for来实现同样的功能,只是代码多几行而已。

27. Write a program that maps a list of words into a list of integers representing the lengths of the correponding words. Write it in three different ways: 1) using a for-loop, 2) using the higher order function map(), and 3) using list comprehensions.

# 1) use loop
def words_to_length(words):
lens = []
for word in words:
lens.append(len(word))
return lens # 2) use map
def words_to_length(words):
return list(map(len, words)) # 3) use list comprehension
def words_to_length(words):
return [len(word) for word in words] words_to_length(["i", "am", "newbie"]) 

28. Write a function find_longest_word() that takes a list of words and returns the length of the longest one. Use only higher order functions.

from functools import reduce

def find_longest_word(words):
return reduce(max, map(len, words)) print(find_longest_word(["a", "student", "good"]))

46 Simple Python Exercises-Higher order functions and list comprehensions的更多相关文章

  1. 46 Simple Python Exercises (前20道题)

    46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises construct ...

  2. 46 Simple Python Exercises-Very simple exercises

    46 Simple Python Exercises-Very simple exercises 4.Write a function that takes a character (i.e. a s ...

  3. [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions

    [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...

  4. [Redux] Understand Redux Higher Order Reducers

    Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...

  5. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  6. [React] Implement a Higher Order Component with Render Props

    When making a reusable component, you'll find that people often like to have the API they're most fa ...

  7. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  8. [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component

    In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...

  9. simple python code when @ simplnano

    code: import serial,time,itertools try: ser=serial.Serial(2,115200,timeout=0) except: print 'Open CO ...

随机推荐

  1. laravel打印原生语句

    laravel中快捷方便的打印语句的方法步骤一:DB::connection()->enableQueryLog();$data["banner"] = WebsiteBan ...

  2. skynet实践(8)-接入websocket

    我从开源项目(https://github.com/lipp/lua-websockets,这里我们简称LWS)中抽出了websocket的部分处理,步骤如下: 1)首先是解决LWS的几个依赖问题.L ...

  3. hadoop Namenode因硬盘写满无法启动

    当写元数据的分区写满,可能导致namenode挂掉从而导致及时清理出大块的空间也无法启动namenode,那此时系统namenode会报错 org.apache.hadoop.hdfs.server. ...

  4. I.MX6 AW-NB177NF p2p support

    /***************************************************************************** * I.MX6 AW-NB177NF p2 ...

  5. 单片机和Linux都想学_换个两全的方法学习单片机

    本节教你如何学习单片机,如何选择合适的开发板和开发工具. 现在我们知道单片机是要学习的,那么怎么去学习单片机?在上一课我们说不要使用老一套的方法学习,实际上是指的两个问题. 第一:选择什么开发板: 第 ...

  6. 用js实现的一个可拖动标签的例子

    先贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  7. Android菜单代码

    前言: 学习android断断续续也有一年半左右,但一直在学习,很少回顾以往的知识.所以我打算用业余时间来写一些这样总结性的文章,希望温故知新. 以下只是我个人的一些感悟和见解(当然会查证资料验证), ...

  8. vs2008打开类视图,看不到类的解决方法

    去工程文件中删除ncb文件,重新打开工程

  9. 分区时"磁盘上没有足够的空间完成此操作"的解决方法

    在新的预装windows 7的品牌机上,工作人员一般将磁盘分为C.D两个分区.但往往造成C盘有很大一部分的空间没办法分出来,而分出来的部分空间又不能和后面的磁盘合并,甚至出现无法新建简单卷的操作,即点 ...

  10. 【WIP】iOS Xcode基础

    创建: 2018/04/18 Xcode基本操作  创建项目处的填空  Product Name 应用名 英语字母  Organization Name 公司/组织/个人名 英语字母  Organiz ...