一个很酷的函数式编程工具。函数可以直接用可用于类型测试的定义域来操作。

fcool的Python项目详细描述


简介

摘要

一个很酷的函数式编程工具。操作数学中的函数。类型测试是以一种很糟糕的方式实现的。

关键词

功能编程,类型测试

功能:

  1. function with domain, the domain can be used to type testing
  2. operator on functions, for example, f + g: x -> f(x) + g(x) where f, g, f+g are functions
  3. to glue functions with glue function or method that can be used to define piecewise functions
  4. no 3rd part requirement
  5. implement memoization in oo way.
  6. use MathFunction for math functions where keyword arguments are deprecated. (new in this version)

内容

课程:

BaseFunction -> Type (or Domain), Function
BaseFunction: func: function (or number)
Function: func,
          domain: Type, the definition domain of func

功能:

Interval(a:num, b:num) -> Type
restrict(t:Type) -> decorator(f:function -> Function(f, t))

常数:

TURE, FALSE(Type) represent universal set, empty set

语法

基本语法

导入:

import fcool (or from fcool import *)

用域定义函数:

F = Function(lambda x:x, Type(lambda x:x>2))
F(3)

函数上的运算符:

(F + F)(3)
(F * F)(4)
(2 * F)(3)

二维函数和类型:

f = 3     # or lambda x,y: 3
g = lambda x,y: 2/x
t = Type(lambda x:x<5 and isinstance(x, int)) * TRUE  # define type(domain) and functions on it
    <=> Type(lambda x, y:x<5) & Type(lambda x, y:isinstance(x, int))
G = Function(g, t)
F = Function(f, t)

备忘录:

f.memoize()    # f is the object of BaseFunction
f.unmemoize()  # prohibit to use memo (memo is not deleted)
f.del_memo()   # just clear the memo, will update the memo in next time
f.forget()     # f.del_memo() and f.unmemoize()

高级语法

粘合功能:

print(G.glue(F)(3,4), glue(G, F)(3,4))    # glue functions

ID = Function(lambda x:x)
print(ID.compose(F)(3,4))      # composition

使用restrict decorator进行类型测试:

@restrict(Interval(1,2))       # restriction decorator
def f(x):
    return x

print(f(1))
try:
    print(f(3))
except Exception as ex:
    print(ex)

G=Function(lambda x:x)
print(G(3))
G = G | Interval(1,2)           # restriction method
print(G(3))

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将字符串转换为自定义对象   java如何从socket方法获取数据?   Java中的soap读取回车和新行   java在单击时替换图像   java推荐的使用RXJava执行异步任务的方法   java MySql连接器JDBC驱动程序不支持连接池吗?   java将活动堆栈清理到顶部   java计数用户输入的数量   java从webservice下载大文件导致应用程序性能问题   JavaLocalDate。EPOCH不可用   java如何在使用Selenium等待一定时间后,在页面无法加载(get(url))时自动刷新页面   java Calendar setLenient方法不允许检查年份字段的健全性   java Eclipse和intelliJ 安卓 SDK问题   java为什么我可以在没有super关键字的情况下调用父方法?   java iText的PDF格式不好