Package: generalmap
What is Generalized Map Function (generalmap)This is a generalized map function in python, which can lift basic function over various data structures and user defined objects. It implements the idea borrowed from functional programming language, that is to define a simple function first to tackle a small piece of problem, then reuse this function to apply to complicated data structures (sometimes called function lifting).An simple example, givenlst=[1, 2, 3]and functionf = lambda x: x+1, this functin only focuses on solving the problem for one element in the list, butmap(f, lst)will applyfto a list, which is a more complicated structure. In a nutshell, the map function let you focus on the atom part or essential part of the problem, then reuse the function in a very smart and cheap way.However, the default map function provided by python is very restrictive:it can only apply to iteratableit can cross onlyone levelof the structureit is not easy to apply to user defined objectsand these are what generalmap package can do.Basic UsageSome examples below like applying function to nonhomogeneous data structure arenot good coding styles, they are simply used to demonstrate what this pacakge can do.Lift function automaticallyThis is the default behavior, the gMap function will cross all predefined structures then applyfto thebasic typeslikeint, float, string(defaultly) orint, float(if setintoStr=True). One caveat is thatfwill always apply to the same level of structure.An one level example:# define different structuressList=[1,2,3]sTuple=(1,2,3)sDict={1:2,2:3,3:4}# create functionfoo=lambdax:x+1# create default gMap objectmp=GMap()# apply f and print the resultsprint(mp.gMap(foo,sList))print(mp.gMap(foo,sTuple))print(mp.gMap(foo,sDict))with output as:[2,3,4](2,3,4){1:3,2:4,3:5}Example for two level mixed nonhomogeneous structure:sMixed=[(1,2),[3,4,5],{6:6,7:7},range(3),slice(None,10,2)]deffoox:returnx+1mp=GMap()print(mp.gMap(foo,sMixed))with output as:[(2,3),[4,5,6],{6:7,7:8},range(1,4),slice(1,11,2)]notice different structures have their own unique lifting behaviors predefined.In defualt, the gMap will not crossing string to apply function on each character, but this can be changed:sStr1='123'foo=lambdax:str(int(x)+1)mp=GMap(intoStr=True)# change default behaviorprint(mp.gMap(foo,sStr1))with output:234Lift function by specifying depthWith depth specified, gMap will applyfto the desinged level without caring whether the type is basic or not. Depth 1 as one level structure (behavior of map function in python).A example:sMixed=[[(1,2,4),(4,5)],[(6,7,8),'91011']]foo=lambdax:x[0]mp=GMap(recDepth=2)#  specify depthprint(mp.gMap(foo,sMixed))with output:[[1,4],[6,'9']]ThesMixedhas a three-level strucure, but we force gMap only apply to the second level, so the function will take tuples and string as input. Notice we don't need to specify theintoStrhere, for we don't lift function into the string structure.Current supported strucutresThe basic (bottom) types including:int, float, stringthe structure types including:list, tuple, map, range, sliceif withintoStrset asTrue, thenstringis a structure type instead of basic.Advanced Usage: Apply to User Defined ObjectsThis section is not necessary if the basic usage is enough for you!!There are two more things user can achieve with this package:Register new basic types.Register new structure typesRegister new basic typesThis can be accomplished by the methodmp.regBasicType(cls)whereclsis the class. For instance, suppose there is a new data typedoubledefined, and we want it to be a bottom type, so that when we apply gMap automatically, it'll stop and applyfon the data of this type.Notice, if register a new basic type that is already in the structure types, it will beremoved fromthe structure type.Register new structure typesThe methodmp.regStructType(cls, clsRule)can register a new structure type, whereclsis a class andclsRuleis a rule description function. Same as before, registering an existing basic type to structure type will remove it from the basic type list.With new structure type defined, it can be mixed with other structure types to build complicated data structure. And gMap can be used on these objects the way same as before.Below is a new data type called MSet (multiset).# a self defined container (multiset)classMSet:def__init__(self,lst):self.elems=lstdef__repr__(self):return('MSet'+str(self.elems)+'')def__getitem__(self,idx):iftype(idx)isint:returnself.elems[idx]else:returnMSet(self.elems[idx])deftoList(self):returnself.elemsthen we need to define a map rule function for this class, all the rules will be explained in the next section.# define the function that describe the map rule# each term will be explained in the next sectiondefmsetMapRule(mset):isBottom=Falseconst=MSetparamList=mset.toList()paramMapIdx=range(len(paramList))ifExpand=FalseprojFunc=lambdax:xliftFunc=lambdax,res:resreturn(isBottom,const,paramList,paramMapIdx,ifExpand,projFunc,liftFunc)with both class and rules are defined, we can apply gMap as same as before:mp=gMap()# create gMap objectmp.regStructType(MSet,msetMapRule)# register new structure type with rule# define functions and data# a two level MSetmset0=MSet([1,2])mset1=MSet([3,4])mset=MSet([mset0,mset1])# a tuple of MSetsmtuple=(MSet([1,2]),MSet([3,4]))# function to applyfoo=lambdax:x+1# apply and show resultsprint(mp.gMap(foo,mset))print(mp.gMap(foo,mtuple))the output isMSet[MSet[2,3],MSet[4,5]](MSet[2,3],MSet[4,5])The map rule functionNow suppose we are defining a map rule functionmcMapRulefor the classMyCls, thenmcMapRule(myObj)is a function accepting objects ofMyClsand ouput a tuple of rules. Each term in the rules are defined as:isBottom: a function from the input objectmyObjto bool, to sift extra bottom cases that is input dependent, such as length 1 string is a bottom case for string structure. In most cases,isBottom = False.const: a construct function that given a list of parameters can construct an object ofMyCls, not necessary to be the constructor of the class.paramList: the list of parameters that used as input for the construct functionconstto produce the input objectmyObj.paramMapIdx: the list of indexes of elements inparamListthat needs to be apply byf. In most cases, all parameters needs to be applied byf, thenparamMapIdx = range(len(paramList)).ifExpand: means when we applyconstonto theparamList, do we need to expand the list or not. Ifconst(a,b,c), thenifExpand=True, ifconst([a, b, c]), thenifExpand=False.projFunc: define a function that transform the inputmyObjto the next level.In most cases, there is nothing to transform, soprojFunc = lambda x: x. In the dictionary case, we only mapfon the value instead of the key, so we need to extract the second value to pass tof, henceprojFunc = lambda x: x[1](in my implementation, the parameter list for dictionary is a list of length 2 tuples, sox[1]is extracting the value).liftFunc: the reverse process ofprojFunc. After applyingfon the transformed value we getres, then how to mergereswith current input objectmyObj.In most cases, there is nothing to merge, soliftFunc = lambda x, res: res. In the dictionary case, we need to combine with key into a tuple, soliftFunc = lambda x, res: (x[0], res).An rule of thumb is, if we applyfto all the parameters of the construct function, likelist, tupleor the exampleMSetabove, then we always setparaMapIdx=range(len(paramList))projFunc=lambdax:xliftFunc=lambdax,res:resAnd in most casesisBottom = False, so the only things to specify are the construct function, the construct list of parameters and whether or not expand the list when we apply construct function on the parameter list.All the rules for the predefined structure types can be found in the source file, can be used as examples for designing rules.How to InstallUse pippipinstallgeneralmapFuture PlanAdd more predefined basic and strcutre types.