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 ...
随机推荐
- 【转载】黑客内核:编写属于你的第一个Linux内核模块
黑客内核:编写属于你的第一个Linux内核模块
- linux下使用DBCA(database configuration assistant)创建oracle数据库
前提:切换到图形界面 到Oracle的bin文件夹下,使用oracle用户.运行dbca就可以.和windows的效果一样. 假设出现乱码 export LANG="en_US:UTF-8& ...
- Android学习JNI,使用JNI实现字符串加密
本节学习使用C语言加密字符串,大家都知道使用JAVA实现的加密都能够反编译的,而使用C写的加密是非常难被反编译的.所以我们使用JNI学习怎样使用C实现对字符串的加密. 首先:我们实现一个界面 布局文件 ...
- 微软柯塔娜(Cortana)的一句名言
近日.媒体频传Win 10装机量已经超过多少千万台.我操心的问题是,集成在Win 10操作系统中的柯塔娜小姐将怎样面对各色各样的人群.由于,在这个世界上.人是最复杂的生物,什么人都有. 依据国外媒体报 ...
- iOS知识点汇总
1.怎样追踪app崩溃率.怎样解决线上闪退 当iOS设备上的App应用闪退时.操作系统会生成一个crash日志.保存在设备上.crash日志上有非常多实用的信息,比方每个正在运行线程的完整堆栈跟踪信息 ...
- 【Linux探索之旅】第二部分第三课:文件和文件夹,组织不会亏待你
wx_fmt=jpeg" alt="" style="max-width:100%; height:auto!important"> 内容简单介 ...
- Testbench代码设计技巧
Testbench代码设计技巧 " There are many ways " to code a test case, it all depens on the creativi ...
- bzoj2768: [JLOI2010]冠军调查(双倍经验最小割)
2768: [JLOI2010]冠军调查 题目:传送门 题解: 双倍经验(1934) 代码: #include<cstdio> #include<cstring> #inclu ...
- HTML5学习笔记(一):HTML5基本概念
1.HTML的发展历程 HTML(1994年,W3C成立) HTML2(1995年) HTML3(1996年) HTML4.0(1997年) HTML4.01(1999年)——HTML5(2008年: ...
- H5中 input消除默认,取消在手机上的点击高亮效果
input消除默认,代码如下 input{ -webkit-tap-highlight-color: rgba(255, 255, 255, 0); ...