扩展python内置类型

hawkweed的Python项目详细描述


又一个缺少函数的实现。

安装

pip install hawkweed

用法

hawkweed大致分为三个不同的部分:数据类型、monads和 功能。所有的函数都用pydoc详尽地记录,所有的 参数、函数的时间复杂度(如果适用)和返回值 得到了。

数据类型

hawkweed中实现的大多数数据类型只是python的包装器。 标准数据类型。如果函数在python中没有返回任何内容 数据类型,此实现将返回self以允许链接。

一个显著的例外是基本上不稳定且未记录的Future类。

fromhawkweedimportList,Dict,SetList([1]).append(2).extend([3,None,4]).remove_empty()# => List([1, 2, 3, 4])List(range(10)).take(5)# => generator from 0 to 4List(range(10)).drop(5)# => generator from 5 to 9List(range(10)).take_while(lambdax:x<5)# => generator from 0 to 4List(range(10)).drop_while(lambdax:x<5)# => generator from 4 to 9List(range(10)).nth(3)# => generator yielding 0, 3, 6 and 9 (lazily); works with any subclass of IterableList(range(10)).reset(range(5))# => List([0, 1, 2, 3, 4])Dict({1:2,3:4}).reverse()# => Dict({2: 1, 4: 3})Dict({1:2,3:4,2:None}).remove_empty()# => Dict({1: 2, 3: 4})Dict({1:2,3:4,None:"go away"}).remove_empty(filter_keys=True)# => Dict({1: 2, 3: 4})Dict({1:2,3:4,2:3}).remove_empty(fun=lambdax:x!=2)# => Dict({1: 2, 3: 4})Dict({1:2,3:4}).reduce(fun=lambdaacc,k,v:acc+k+v,acc=0)# => 10Dict({1:2,3:4}).reduce(fun=lambdaacc,k,v:acc+(k,v))# => (1, 2, 3, 4)Dict({1:2,3:4,5:6}).pick(1,5)# => Dict({1: 2, 5: 6})Set({1,2,3,4}).remove_empty(fun=lambdax:x!=3)# => Set({1, 2, 4})# And now for something completely differentDict({"foo":List([1,2,3,Dict({"bar":"baz"})])}).get_in("foo",3,"bar")# => "baz"Dict({"foo":List([1,2,3,Dict({"bar":"baz"})])}).get_in("foo",100,"bar")# => NoneDict({"foo":List([1,2,3,Dict({"bar":"baz"})])}).get_in("foo",100,"bar",dflt="i am a default value")# => "i am a default value"Dict({"foo":List([1,2,3,Dict({"bar":"baz"})])}).update_in("foo",1,"bar",to="update")# => Dict({"foo": List([1, 2, 3, Dict({"bar": "update"})])})# if you want to insert your own datatype, just inherit from hawkweed.Collection# and implement get(key, dflt=None) and __setitem__

功能

所有的函数都是独立的,只要可能就可以使用。他们不依赖 以任何方式访问HawwkWeeds数据类型。

fromhawkweedimportmap,reduce,List,all,any,constantly,delaymap(inc,range(100))# => range(1, 101)incrementor=map(inc)incrementor(List(range(100)))# => range(1, 101)summator=reduce(add)summator(range(5))# => 10all(lambdax:x>100,[101,102,103])# => Trueany(lambdax:x>10,[3,5,8])# => Falseconstantly(10)# => an infinite generator of 10delayed=delay(print,'Hello, World!')# => this will return a variable that, when called, will compute the result of print with the argument 'Hello, World!'# it will cache the result instead of recomputing it upon reevaluation, i.e. `delayed() or delayed()` will only print 'Hello, World!' once

从函数式编程库(compose)中可以得到的一些其他函数, pipeidentityapplyflipcurry等)也被实现。他们 应该是直观的,并按预期工作。如果他们不认为这是一个错误。

单子

实现的monad是:identity,也许(just/nothing),continuation,或者,io,cachedio, 和list(称为listm)。也支持do符号。

fromhawkweedimportdoM,wrapM,JustdefdoMe():res1=yieldJust(1)res2=yieldJust(10)yieldJust(res1+res2)doM(doMe())# => Just(11)wrapM(Just(10)).real# => 10; the wrapper will try to call the wrapped values' function whenever it does not exist in the monad

有一个callcc函数,haskell的Data.MaybeData.Either中的所有函数都实现了。

玩得开心!

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

推荐PyPI第三方库


热门话题
来自控制器的java集合引用应用程序?   java无法插入到swagger 2.0文档中常见错误代码的html链接   循环中的java输入不匹配异常   java Spring批处理集成运行并行作业的远程分区   在Android中使用Gson在自定义类的ArrayList中读取java   C++规范化图像描述符OpenCV-java   java Andmore的Android软件包生成器失败,错误与sun/misc/BASE64Encoder相关   如何用java从多部分数据格式创建接收代码   java用文件填充数组   分页如何在Java代码中实现下一步按钮单击?   我们能用泛型参数动态调用Java接口方法吗?   java从另一个项目中定义的类调用静态方法需要为这两个项目添加库   反射:运行时类型信息是否存储在java中?   编写一个Java程序,允许用户输入自己的公式并进行计算   java Tomcat多个webapps文件夹   java比较两个xml文件并向第一个xml文件添加新标记   反射我能用正则表达式在java中找到类的方法吗?