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 ...
随机推荐
- HDU 4323 Contest 3
编辑距离,经典的了.动态规划枚举即过. #include <iostream> #include <cstdio> #include <string.h> #inc ...
- POJ 3695
可以用容斥原理来求.求两个矩形的并的时候可以使用条件 x1=max(p.x1,q.x1);y1=max(p.y1,q.y1);x2=min(p.x2,q.x2);y2=min(p.y2,q.y2); ...
- 关于amd64和ia64的理解
关于amd64和ia64的理解 学习了:http://blog.csdn.net/zubin006/article/details/5060383 IA64指的是Intel安腾系列CPU,不是X86架 ...
- 在 RedHat/CentOS 7.x 中使用 nmcli 命令管理网络
在 RedHat/CentOS 7.x 中使用 nmcli 命令管理网络 学习了:https://linux.cn/article-5410-1.html#3_3613 http://www.linu ...
- 基于MySQL元数据的Hive的安装和简单測试
引言: Hive是一种强大的数据仓库查询语言,类似SQL,本文将介绍怎样搭建Hive的开发測试环境. 1. 什么是Hive? hive是基于Hadoop的一个数据仓库工具,能够将结构化的数据文件映射为 ...
- APACHE KYLIN™ 概览(分布式分析引擎)
Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop/Spark之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由eBay Inc. 开发并贡献至开源社区.它能 ...
- Android TextView加下划线的几种方式
如果是在资源文件里: <resources> <</u></string> <string name="app_name">M ...
- (转载)Android学习之Intent使用
ndroid学习之Intent使用 1.使用显示Intent Intent intent = new Intent(FirstActivity.this,SecondActivity.class) ...
- ZBrush实用插件ZAppLink简介
ZAppLink是ZBrush版本推出时被评为最值得期待的插件.事实证明,ZAppLink的出现让工具与工具之间有了交流,搭起软件与软件的沟通桥梁. ZAppLink插件专用于扩展ZBrush®的绘制 ...
- 基本数据类型(int,bool,str)
1.int bit_lenth() 计算整数在内存中占用的二进制码的长度 十进制 二进制 长度(bit_lenth()) 1 1 1 2 10 2 4 100 3 8 1000 4 16 10000 ...