咖喱功能图案贴片
masala的Python项目详细描述
生成状态
大师级
安装
$ pip install masala
示例
咖喱
>>>from__future__import(print_function,division,absolute_import,unicode_literals,)>>>frommasalaimportCurryContainerascc>>>cur=cc(lambdaa,b,c:[a,b,c])>>>cur=cur<<'aaa'<<'bbb'>>>cur('ccc')['aaa','bbb','ccc']>>>cur=cc(lambdaa,b='hogeeee',c='foooo':[a,b,c])>>>cur=cur<<'a'<<('c','c')>>>cur('b')['a','b','c']>>>frommasalaimportcurried>>>@curried...defsum5(a,b,c,d,e):...returna+b+c+d+e...>>>sum0=sum5<<1<<2<<3<<4<<5>>>assertsum0()==sum5(1,2,3,4,5)
短手
λ
>>>frommasalaimportlambdas_>>>frommasala.datatypeimportEither>>>Either.right('hachi')>>_.title()'Hachi'>>>replacer=_.replace(_,_)>>>replacer('hachi','chi','chiboee')'hachiboee'>>>list(map(_+2,range(3)))[2,3,4]>>>fromsix.movesimportreduce>>>reduce(_+_,range(5))10
方法链接
>>>frommasalaimportBuilderAllowsMethodChainingas__>>>Either.right('hachi')>>__.title().replace('i','U').replace('c','z')'HazhU'>>>foolambda=__.title().replace('i','U').replace('c','z')>>>foolambda('hachi')'HazhU'>>>Either.right(4)>>((__+4)*'py'.title())'PyPyPyPyPyPyPyPy'>>>(__+1+2+3+4+5)(0)15>>>(__+1+2+3+4+5).apply__(0)15
列表处理
>>>frommasalaimport(applyasa,lambdas_,)>>>frommasala.datatypeimportStream>>># extends linq like methods to Stream.>>># but I reccomend to use itertools extention is also prepared as `from masala.datatype.stream import itertools_ext`>>>frommasala.datatype.streamimportlinq_ext>>>Stream([1,2,3]).select(_*2).to_list()[2,4,6]>>># support lazy evaluation>>>Stream([1,2,3]).select(_*2)# doctest:+ELLIPSISStream:<<function...>>>Stream(range(0,15)).select(_+1).where(__%2==0).to_list()[2,4,6,8,10,12,14]>>>Stream(range(0,100)).select(_*2).where(_>1000).first()# doctest:+ELLIPSISEmpty:<None>reason=><class'masala.datatype.stream.error.NoContentStreamError'>:>>>Stream(range(0,100)).select(_*2).any(_>1000)False>>>Stream(111111).select(_*2).to_list()Empty:<None>reason=><class'masala.datatype.stream.error.NotIterableError'>: 'int' object is not iterable>>># you can extend the method by yourself>>>frommasala.datatype.streamimportdispatch_stream>>>@dispatch_stream...defmy_select(xs,x_to_y):...forxinxs:...yieldx_to_y(x)>>>Stream([1,2,3]).my_select(_*2).to_list()[2,4,6]>>>frommasala.datatype.streamimportdelete_dispatchedmethods>>># you can clean extentions.>>>delete_dispatchedmethods(['my_select'])>>># other cases>>>twicer=Stream().select(_*2)>>>twiced=twicer<<[1,2,3]>>>list(twiced)[2,4,6]>>>twiced2=twicer<<[2,3,4]>>>list(twiced2)[4,6,8]>>>delete_dispatchedmethods(linq_ext.__all__)
模式匹配
>>>frommasalaimportMatch>>>match=Match(10)>>>ifmatch.when(1):...print('boo')...elifmatch.when(10):...print('yieeeee')yieeeee>>>frommasalaimportWildcardas_>>>match=Match([1,2,3])>>>@match.when([2,2,2],let_=('one','two','thr'))...defcase1(one,two,thr):...return'case1'>>>@match.when([_,2,3],let_=('one','_','thr'))...defcase2(one,thr):...return'case2'>>>assertmatch.end=='case2'>>>match=Match('python')>>>@match.when(_.isdigit(),let_='moo')...defcase1(moo):...returnone>>>@match.when(_=='python',let_=('a'))...defcase2(a):...returna>>>assertmatch.end=='python'>>># with datatype>>>frommasala.datatypeimportRight,Left>>>match=Match(Either.right('python'))>>>@match.when(Right)...defcase_right(v):...returnv+' is right!'>>>@match.when(Left)...defcase_left(v):...assertFalse>>>assertmatch.end=='python is right!'
具有可选值的调用方法
>>>frommasalaimportPerhaps>>>p=Perhaps('hoge huga foo')>>>p.try_('replace','huga','muoo').try_('upper').get()'HOGE MUOO FOO'>>>p.apply(len).get()13>>>nonecase=Perhaps(None).try_('replace','huga','muoo').try_('upper')>>>nonecase.get()>>>nonecase.get_or('nnnnn')'nnnnn'
支架
python的测试版本是
- 2.7
- 3.4