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

时间: 2024-08-05 01:32:35

Pure functions的相关文章

初学knockoutjs记录7——Computed observables依赖监控(4 Pure computed observables 纯计算监控属性)

Pure computed observables 纯计算监控属性 纯计算监控属性在knockout3.2.0中引入,给在大多数场合下常规的计算监控属性提供了一个速度和内存性能更好选择.这是因为纯计算监控属性在它本身没有被订阅的情况下不需要维护它的依赖. 它的特性: Prevents memore leaks 防止内存泄露.纯计算监控属性不再是一个程序引用,但是它的整个依赖依然存在. Reduces computation oberhead 减少计算开销.当值不再被监控时不再进行计算监控属性的计

5.24 Declaring Attributes of Functions【转】

转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes of Functions In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your

纯计算监控(Pure computed observables)

纯计算监控,在knockout 3.2.0里才有,提供了对性能和内存更好的管理.这是因为纯计算监控不包含对他的依赖的订阅.特点有: 防止内存泄漏 降低计算开销:值不再是observed,是一个不会重新计算的computed observables. 根据是否有订阅,pure computed observable会自动在两状态间进行切换: 1. 当没有订阅者(subscribers)时,是sleeping状态.当进入sleeping状态时,会释放所有依赖的订阅.在这个状态期间,它不会订阅任何在函

Functional Programming.

I have been seeing Redux for sometime, but recently I come to realize that , it's part of so called functional programming. The basic idea for functional programming is so simple, Declare all dependency as function input, no hidden input ( we should

JavaScript 与函数式编程

原文:https://bethallchurch.github.io/JavaScript-and-Functional-Programming/ 译文:http://www.zcfy.cc/article/1013 译者注:推荐一篇译文,<函数式编程术语解析>. 本文是我在 2016 年 7 月 29 号听 Kyle Simpson 精彩的课程<Functional-Light JavaScript>时所做的笔记(外加个人的深入研究)(幻灯片在这). 长久以来,面向对象在 Jav

深入浅出 Python 函数式编程

1.函数式编程的定义与由来 如果程序中的函数仅接受输入并产生输出,即输出只依赖于输入,数据不可变,避免保存程序状态,那么就称为函数式编程(Functional Programming,简称FP,又称泛函编程). 这种风格也称声明式编程(Declarative Programming),与之相对的是指令式编程(Imperative Programming),后者中的对象会不断修改自身状态.函数式编程强调程序的执行结果比执行过程更重要,倡导利用若干简单的执行单元让计算结果不断渐进,逐层推导复杂的运算

[技术] 谈谈编程思想

https://zhuanlan.zhihu.com/p/19736530?columnSlug=prattle 作者:陈天链接:https://zhuanlan.zhihu.com/p/19736530来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 这段时间又攒了很多答应了,但还未动手的文章.大概一两周前,有个读者留言:「程序君,能发篇文章有关编程思想的吗?我是编程初学者,对编程思想没啥概念,求传授点经验!」 今天就讲讲编程思想.编程思想是个宏大的主题,我不敢保

Functional Programming Principles in ScalaScala函式编程原理 第一章笔记

所有non-trival编程语言都提供了 基本表达式(expression)去表示最简单的表达式 组合表达式的方法 抽象表达式的方法,也就是为表达式引入一个名字去引用它 substitional model 替代模型 sumOfSquares(3,2+2) sumOfSquares(3,4) square(3)+square(4) 9+square(4) 9+16 25 这种模型的核心概念是所有的表达式都规约成值,替代模型在lamada表达式中被形式化,构成了函数式编程的基石 substitio

The introduction of redux

Why has it been created This complexity is difficult to handle as we're mixing two concepts that are very hard for the human mind to reason about:mutation and asynchronicity.I call them Mentos and Coke. Both can be great in separation, but together t