Pure functions
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的更多相关文章
- 5.24 Declaring Attributes of Functions【转】
转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes o ...
- 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 ...
- 纯计算监控(Pure computed observables)
纯计算监控,在knockout 3.2.0里才有,提供了对性能和内存更好的管理.这是因为纯计算监控不包含对他的依赖的订阅.特点有: 防止内存泄漏 降低计算开销:值不再是observed,是一个不会重新 ...
- "Becoming Functional" 阅读笔记+思维导图
<Becoming Functional>是O'Reilly公司今年(2014)7月发布的一本薄薄的小册子,151页,介绍了函数式编程的基本概念.全书使用代码范例都是基于JVM的编程语言, ...
- 优化一个奇葩表设计上的全表扫描SQL
之前在一个比较繁忙的系统抓到的耗时长.消耗CPU多的一条SQL,如下:SELECT * FROM Z_VISU_DATA_ALARM_LOG TWHERE TO_DATE(T.T_TIMESTR, ' ...
- JavaScript 与函数式编程
原文:https://bethallchurch.github.io/JavaScript-and-Functional-Programming/ 译文:http://www.zcfy.cc/arti ...
- Modifiers
Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the ...
- Think Python Glossary
一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...
- [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 ...
随机推荐
- UNIX基础【UNIX入门经典】
最早在学校很流行.学生毕业以后就会为公司购买操作系统.导致UNIX流行 UNIX内核: Shell:sh csh ksh 其他组件:
- HDU 4312 Contest 2
题目要求两点间的最大值作为距离即: 即是切比雪夫距离.而切比雪夫距离与曼哈顿距离的转换却很巧妙. 把平面坐标所有点绕原点逆向旋转45度后,所得点的曼哈顿距离之和除以√2,即是切雪比夫距离.旋转点的公式 ...
- BestCoder Round #52 (div.2) HDU 5418 Victor and World (DP+状态压缩)
[题目链接]:pid=5418">click here~~ [题目大意]: 问题描写叙述 经过多年的努力,Victor最终考到了飞行驾照. 为了庆祝这件事,他决定给自己买一架飞机然后环 ...
- Python中常见的文件对象内建函数
文件对象内建方法列表 文件对象的方法 操作 file.close() 关闭文件 file.fileno() 返回文件的描写叙述符(file descriptor.FD,整数值) file.flush( ...
- js调节图片的亮度
js调节图片的亮度:(使用CSS3的滤镜) 1.实现点亮图标.熄灭图标的效果 效果图: 页面代码: <!DOCTYPE html> <%@ page language="j ...
- Aspose office (Excel,Word,PPT),PDF 在线预览
前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...
- asp.net mvc5 文件下载上传
下载:是通过点击a标签直接下载的方式,没有其他任何要求,在服务器上存在实体文件,不需要请求后台控制层 前段js: <a id="NF-DownLoad" authorize= ...
- 搭建python3环境
将Python2.7通过控制面板卸载,然后从网站下载python的安装包,安装即可. 安装beatifulsoup4库.在命令行下, pip install beautifulsoup4 千万不能在p ...
- js闭包详解-转自好友trigkit4
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 闭包的特性 闭包有三个特性: 1.函数嵌套函数 2.函数内部可以引用外部的参数和变量 3.参数 ...
- yii2.0 发送邮件带word小附件
把 common/config/main-local.php 下的 mailer 注释掉: 'mailer'=>[ 'class ...