Munna
Munna

Reputation: 336

Why is map function not effective?

I am not struck or anything, but I feel this weird. Below is the code snippet I have worked on:

from hashClass import HashTable

a = HashTable(11)
input_value = list((54,26,93,17,77,31,44,55,20))

map(lambda x: a.put(x,x),input_value))
print(a.data)

I have created my own hash table class. It has a method called put which accepts key-value pair and "HashTable.data" attribute displays all the values in the table.

It works completely fine when I use put method with normal key-value pairs. I know that the solution can be implemented using:

for i in input_value: a.putt(i,i)

But I would like to know why the map function is not effective? When I try to map every input_value with "put" method, it has to add the value to the instance if I am not wrong. My reason is I may not be using the mapped value but syntactically when I am mapping, it is supposed to update the instance variable.

Below is the hash Class I have created for reference.

class HashTable(object):

    def __init__(self,size):
        self.size = size
        self.slots = self.size*[None]
        self.data = self.size*[None]
        self.values = 0

    def put(self,key,value):
        hashValue = self.hashFunction(key)
        if self.slots[hashValue] == None:
            self.slots[hashValue] = key
            self.data[hashValue] = value
            self.values += 1
        elif self.slots[hashValue] == key:
            self.data[hashValue] = value
        else:
            hashValue = self.reHash(hashValue)
            while self.slots[hashValue] != None and self.slots[hashValue] != key:
                hashValue = self.reHash(hashValue)
            self.slots[hashValue] = key
            self.data[hashValue] = value
            self.values += 1


    def reHash(self,oldValue):
        return (oldValue+1)%self.size

    def __len__(self):
        return self.values


    def get(self,key):
        hashValue = self.hashFunction(key)
        if self.slots[hashValue] == None:
            return "No Value associated"
        elif self.slots[hashValue] == key:
            return self.data[hashValue]


    def hashFunction(self,key):
        return key%self.size

Upvotes: 1

Views: 990

Answers (2)

user24863889
user24863889

Reputation: 1

I can't find a map function that has From: And To: Anymore. They go straight to the sales pitch before giving you what you came for.

Upvotes: 0

cs95
cs95

Reputation: 403208

I'm going to go out on a limb here and assume you are using Python3.

With python3, a map facilitates lazy evaluation, meaning it will not carry out its functionality unless it really needs to. What you are trying to do is use a map to produce side effects. Sure, you can do this with:

list(map(lambda x: a.put(x,x), input_value)))

The list() forces evaluation to take place.

However, using map for side effects is somewhat of an anti-pattern. I would prefer something clearer and more idiomatic such as the for loop you mentioned.


As an example:

In [854]: s = set()

In [862]: m = map(lambda x=x: s.add(x), [1, 2, 3])

In [863]: s
Out[863]: set()

Nothing happened to s thus far. Now, apply list() to the map object.

In [864]: list(m)
Out[864]: [None, None, None]

The Nones are classic symptoms of side-effects. However...

In [865]: s
Out[865]: {1, 2, 3}

So, it works. But it certainly doesn't look good.

Upvotes: 6

Related Questions