扩展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 Android:在ListView上使用setOnItemClickListener   使用Netbeans 7.0连接到SQL Server的java正在挂起   java Spring3依赖项注入不适用于mule   java Flink SQL结果字段与LocalDateTime上请求的类型错误不匹配   java找不到文件的结尾   考虑到NamingStrategy,java有没有办法将字符串转换为JsonNode?   使用Netbeans/ant部署java(命令行)应用程序   java如何修复Spring引导多部分上载中的“所需请求部分不存在”   java在应用程序启动时通过引用获取映射未知目标实体属性异常   java形状旋转问题Java2d   Weblogic服务器上的java ExecuteAndWaitInterceptor问题   JavaSpringBoot:project将图像保存在错误的路径中,并且在使用IDEIntellji打开时不显示图像   类向java接口添加方法   Swing组件上的Java 7泛型   sql server如何从java获取用户名。sql。联系   java如何检查该行是否与正则表达式(regex)冲突?   java如何在spring引导安全中为计数失败登录设置验证登录为false   图像如何在Java中使PNG的白色透明?