Skip to content

why map instead of returning tuple? #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ladyada opened this issue Jan 9, 2018 · 2 comments
Closed

why map instead of returning tuple? #3

ladyada opened this issue Jan 9, 2018 · 2 comments
Assignees

Comments

@ladyada
Copy link
Member

ladyada commented Jan 9, 2018

hiya writing some code that uses this and its unclear why

        if self._accel_range == ACCEL_RANGE_2G:
            return map(lambda x: x * _ACCEL_MG_LSB_2G * _SENSORS_GRAVITY_STANDARD,
                       accel_raw)
        elif self._accel_range == ACCEL_RANGE_4G:
            return map(lambda x: x * _ACCEL_MG_LSB_4G * _SENSORS_GRAVITY_STANDARD,
                       accel_raw)
        elif self._accel_range == ACCEL_RANGE_8G:
            return map(lambda x: x * _ACCEL_MG_LSB_8G * _SENSORS_GRAVITY_STANDARD,
accel_raw)

is preferrable over something like

        factor = 0
        if self._accel_range == ACCEL_RANGE_2G:
            factor = _ACCEL_MG_LSB_2G
        elif self._accel_range == ACCEL_RANGE_4G:
            factor = _ACCEL_MG_LSB_4G
        elif self._accel_range == ACCEL_RANGE_8G:
            factor = _ACCEL_MG_LSB_8G
        return [x * factor * _SENSORS_GRAVITY_STANDARD for x in accel_raw]

that way you can print the return'd array directly (the map return isnt printable but is iterable)

thx!

@tannewt
Copy link
Member

tannewt commented Jan 9, 2018

map is an older style of that functional approach. It looks like in python3 it returns an iterable object rather than a list as it does in python2. I agree it should be switched to your suggestion using list comprehension.

@dhalbert
Copy link
Contributor

dhalbert commented Jan 9, 2018

Fixed by #4.

@dhalbert dhalbert closed this as completed Jan 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants