In the next few sections, we’ll write two versions of a function called add_time, which calculates the sum of two Time objects. They demonstrate two kinds of functions: pure functions and modifiers. They also demonstrate a development plan I’ll call prototype and patch, which is a way of tackling a complex problem by starting with a simple prototype and incrementally dealing with the complications.

class Time:
""" represents the time of day
attributes: hour, minute, second"""
def print_time(self):
print('%d:%d:%d' % (self.hour,self.minute,self.second))
def after(self,t):
if(self.hour < t.hour):
return False
elif(self.hour == t.hour):
if(self.minute < t.minute):
return False
elif(self.minute == t.minute):
if(self.second <= t.second):
return False
else: return True
return True def add_time(t1,t2):
total = Time()
total.hour = t1.hour + t2.hour
total.minute = t1.minute + t2.minute
total.second = t1.second + t2.second
if(total.second >=60):
total.second -= 60
total.minute +=1
if(total.minute >=60):
total.minute -=60
total.hour +=1
return total time = Time()
time.hour = 11
time.minute = 59
time.second = 30
time1 = Time()
time1.hour = 11
time1.minute = 59
time1.second = 36
time2 = Time()
time2.hour = 11
time2.minute = 58
time2.second = 55

Although this function is correct, it is starting to get big. We will see a shorter alternative later.

from Thinking in Python

Pure functions的更多相关文章

  1. 5.24 Declaring Attributes of Functions【转】

    转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes o ...

  2. Think Python - Chapter 16 - Classes and functions

    16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...

  3. 纯计算监控(Pure computed observables)

    纯计算监控,在knockout 3.2.0里才有,提供了对性能和内存更好的管理.这是因为纯计算监控不包含对他的依赖的订阅.特点有: 防止内存泄漏 降低计算开销:值不再是observed,是一个不会重新 ...

  4. "Becoming Functional" 阅读笔记+思维导图

    <Becoming Functional>是O'Reilly公司今年(2014)7月发布的一本薄薄的小册子,151页,介绍了函数式编程的基本概念.全书使用代码范例都是基于JVM的编程语言, ...

  5. 优化一个奇葩表设计上的全表扫描SQL

    之前在一个比较繁忙的系统抓到的耗时长.消耗CPU多的一条SQL,如下:SELECT * FROM Z_VISU_DATA_ALARM_LOG TWHERE TO_DATE(T.T_TIMESTR, ' ...

  6. JavaScript 与函数式编程

    原文:https://bethallchurch.github.io/JavaScript-and-Functional-Programming/ 译文:http://www.zcfy.cc/arti ...

  7. Modifiers

    Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the ...

  8. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...

  9. [Ramada] Build a Functional Pipeline with Ramda.js

    We'll learn how to take advantage of Ramda's automatic function currying and data-last argument orde ...

随机推荐

  1. IOS版本号被拒的经历

    IOS版本号被拒的经历: 1,登陆方式依赖外部平台 由于我们的APP是仅仅用微博登陆.想做成类似meerkat类型的.也能各种消息都同步微博. 结果当然行不通,这个确实是不听好人言.网上多个人都说过这 ...

  2. OPENWRT中SSH免密钥登陆(具体步骤)

    通过使用ssh-keygen生成公钥,在两台机器之间互相建立新人通道极客. 如果本地机器是client,远程机器为server. 1.使用ssh-keygen生成rsa keygen(在这里会覆盖曾经 ...

  3. ubuntu 交叉编译qt 5.7 程序到 arm 开发板

    ubuntu 交叉编译qt 5.7 程序到 arm 开发板平台1 ubuntu 12.042 arm-linux-gcc 4.5.13 QT 5.74 开发板210 armcortex-A8 一 概述 ...

  4. 当我们谈论Erlang Maps时,我们谈论什么 Part 2

    声明:本文讨论的Erlang Maps是基于17.0-rc2,时间2014-3-4.兴许Maps可能会出现语法或函数API上的有所调整,特此说明. 前情提要: [Erlang 0116] 当我们谈论E ...

  5. xBIM 基础10 WeXplorer 浏览器检查

    系列目录    [已更新最新开发文章,点击查看详细]  在上一篇 <xBIM基础 09 WeXplorer 基本应用> 已经提到,查看器不会在所有浏览器的所有设备上运行.为了操作效率和简单 ...

  6. vue中router-link的click事件失效的解决办法

    title: vue中router-link的click事件失效的解决办法 toc: false date: 2018-12-04 16:28:49 categories: Web tags: vue ...

  7. 电信流氓注入JS

    (function () { var cs_url = _pushshowjs_.url, cs_delay = window.cs_delay; var cs_styles = window.sty ...

  8. 「JavaSE 重新出发」02.01 基本数据类型

    基本数据类型 1. 整型 注意:Java 没有任何无符号(unsigned)形式的 int.long.short 或 byte 类型. 不同进制的表示形式: 十六进制数值有一个前缀0x或0X(如0xC ...

  9. hiho 1564 - 简单dfs + 宏的危害!!!

    题目链接 H公司有 N 台服务器,编号1~N,组成了一个树形结构.其中中央服务器处于根节点,终端服务器处于叶子节点. 中央服务器会向终端服务器发送消息.一条消息会通过中间节点,到达所有的终端服务器.消 ...

  10. [HNOI2008]越狱 快速幂 逆推

    考虑越狱的情况有些复杂,不如考虑总情况减去不越狱的情况. 显然,总情况为 $m^n$ 种,不越狱的情况为 $m*(m-1)*(m-1)*(m-1)....$ 即为 $m*(m-1)^(n-1)$. 做 ...