扩展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)中可以得到的一些其他函数, pipe,identity,apply,flip,curry等)也被实现。他们 应该是直观的,并按预期工作。如果他们不认为这是一个错误。
单子
实现的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.Maybe和Data.Either中的所有函数都实现了。
玩得开心!