A generalized map function for lift function over structures
Project description
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, given lst=[1, 2, 3]
and function f = lambda x: x+1
, this functin only focuses on solving the problem for one element in the list, but map(f, lst)
will apply f
to 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 iteratable
- it can cross only one level of the structure
- it is not easy to apply to user defined objects
and these are what generalmap package can do.
Basic Usage
Some examples below like applying function to nonhomogeneous data structure are not good coding styles, they are simply used to demonstrate what this pacakge can do.
Lift function automatically
This is the default behavior, the gMap function will cross all predefined structures then apply f
to the basic types like int, float, string
(defaultly) or int, float
(if set intoStr=True
). One caveat is that f
will always apply to the same level of structure.
An one level example:
# define different structures
sList = [1, 2, 3]
sTuple = (1, 2, 3)
sDict = {1: 2, 2: 3, 3: 4}
# create function
foo = lambda x: x+1
# create default gMap object
mp = GMap()
# apply f and print the results
print(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)]
def foo x:
return x+1
mp = 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 = lambda x: str(int(x)+1)
mp = GMap(intoStr=True) # change default behavior
print(mp.gMap(foo, sStr1))
with output:
234
Lift function by specifying depth
With depth specified, gMap will apply f
to 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 = lambda x: x[0]
mp = GMap(recDepth=2) # specify depth
print(mp.gMap(foo, sMixed))
with output:
[[1, 4], [6, '9']]
The sMixed
has 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 the intoStr
here, for we don't lift function into the string structure.
Current supported strucutres
The basic (bottom) types including:
int, float, string
the structure types including:
list, tuple, map, range, slice
if with intoStr
set as True
, then string
is a structure type instead of basic.
Advanced Usage: Apply to User Defined Objects
This 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 types
Register new basic types
This can be accomplished by the method mp.regBasicType(cls)
where cls
is the class. For instance, suppose there is a new data type double
defined, and we want it to be a bottom type, so that when we apply gMap automatically, it'll stop and apply f
on the data of this type.
Notice, if register a new basic type that is already in the structure types, it will be removed from the structure type.
Register new structure types
The method mp.regStructType(cls, clsRule)
can register a new structure type, where cls
is a class and clsRule
is 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)
class MSet:
def __init__(self, lst):
self.elems = lst
def __repr__(self):
return('MSet' + str(self.elems) + '')
def __getitem__(self, idx):
if type(idx) is int:
return self.elems[idx]
else:
return MSet(self.elems[idx])
def toList(self):
return self.elems
then 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 section
def msetMapRule(mset):
isBottom = False
const = MSet
paramList = mset.toList()
paramMapIdx = range(len(paramList))
ifExpand = False
projFunc = lambda x: x
liftFunc = lambda x, res: res
return (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 object
mp.regStructType(MSet, msetMapRule) # register new structure type with rule
# define functions and data
# a two level MSet
mset0 = MSet([1,2])
mset1 = MSet([3,4])
mset = MSet([mset0, mset1])
# a tuple of MSets
mtuple = (MSet([1,2]), MSet([3,4]))
# function to apply
foo = lambda x: x+1
# apply and show results
print(mp.gMap(foo, mset))
print(mp.gMap(foo, mtuple))
the output is
MSet[MSet[2, 3], MSet[4, 5]]
(MSet[2, 3], MSet[4, 5])
The map rule function
Now suppose we are defining a map rule function mcMapRule
for the class MyCls
, then mcMapRule(myObj)
is a function accepting objects of MyCls
and ouput a tuple of rules. Each term in the rules are defined as:
-
isBottom
: a function from the input objectmyObj
to 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 functionconst
to produce the input objectmyObj
. -
paramMapIdx
: the list of indexes of elements inparamList
that needs to be apply byf
. In most cases, all parameters needs to be applied byf
, thenparamMapIdx = range(len(paramList))
. -
ifExpand
: means when we applyconst
onto 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 inputmyObj
to the next level. In most cases, there is nothing to transform, soprojFunc = lambda x: x
. In the dictionary case, we only mapf
on 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 applyingf
on the transformed value we getres
, then how to mergeres
with 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 apply f
to all the parameters of the construct function, like list, tuple
or the example MSet
above, then we always set
paraMapIdx = range(len(paramList))
projFunc = lambda x: x
liftFunc = lambda x, res: res
And in most cases isBottom = 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 Install
Use pip
pip install generalmap
Future Plan
Add more predefined basic and strcutre types.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for generalmap-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9dd976b065a7439995e08aa690691fe09da92acc9f28f7207168eba8ac6b0efd |
|
MD5 | 2a90f61cf6458064d103c503a1c074d9 |
|
BLAKE2b-256 | d9970a2836944556142d26fb1143face8245704350e0736cb818ca4431ac3cd3 |