Skip to content
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

Wiki FAQ: Lambda Functions Update and Formatting #37

Open
MarkMoretto opened this issue Apr 19, 2020 · 0 comments
Open

Wiki FAQ: Lambda Functions Update and Formatting #37

MarkMoretto opened this issue Apr 19, 2020 · 0 comments

Comments

@MarkMoretto
Copy link

MarkMoretto commented Apr 19, 2020

I wanted to push an update for the FAQ in the Wiki section. I just cleaned up and added a little bit to the section about Lambda notation. Below is the updated replacement.

# syntax error, lambda notation python 2.7 vs 3.x

<style type="text/css">
.container {
  display: inline-grid;
  align-items: center;
  justify-content: center;
}

.cap-map {
    text-align: center;
    font-size: 24px;
    text-decoration: none;
    vertical-align: middle;
}

.header-cell {
  text-align: center;
  font-weight: bold;
  vertical-align: middle;
}

.center-cell {
    text-align: center;
    vertical-align: middle;
}

.function-cell {
    text-align: left;
    font-family: monospace;
    white-space: pre;
    vertical-align: middle;
}
</style>

<div class="container">
    <table>
        <caption class="cap-map">Mapping RDDs with Single Values</caption>
        <thead>
            <tr>
                <th class="header-cell">Python Version</th>
                <th class="header-cell">Solution #</th>
                <th class="header-cell">Lambda Function</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="center-cell">2.7</td>
                <td class="center-cell">1</td>
                <td class="function-cell">result_array_ts = result_rdd.map(lambda (ts, voltage): ts).collect()</td>
            </tr>
            <tr>
                <td class="center-cell">3.x</td>
                <td class="center-cell">1</td>
                <td class="function-cell">result_array_ts = result_rdd.map(lambda ts_voltage: ts_voltage[0]).collect()</td>
            </tr>
            <tr>
                <td class="center-cell">3.x</td>
                <td class="center-cell">2</td>
                <td class="function-cell">result_array_voltage = result_rdd.map(lambda ts_voltage: ts_voltage[1]).collect()</td>
            </tr>        
        </tbody>
    </table>
    <br />
    <table>
        <caption class="cap-map">Mapping RDDs with Multiple Values</caption>
        <thead>
            <tr>
                <th class="header-cell">Python Version</th>
                <th class="header-cell">Lambda Function</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="center-cell">2.7</td>
                <td class="function-cell">rdd.map(lambda (x, y): pow(x * y, 2)).sum()</td>
            </tr>
            <tr>
                <td class="center-cell">3.x</td>
                <td class="function-cell">rdd.map(lambda xy: pow(xy[0] * xy[1], 2)).sum()</td>
            </tr>
        </tbody>
    </table>
</div>

The less-fancy versions (HTML and markdown) are:

Mapping RDDs with Multiple Values
Python Version Solution # Lambda Function
2.7 1 result_array_ts = result_rdd.map(lambda (ts, voltage): ts).collect()
3.x 1 result_array_ts = result_rdd.map(lambda ts_voltage: ts_voltage[0]).collect()
3.x 2 result_array_voltage = result_rdd.map(lambda ts_voltage: ts_voltage[1]).collect()

Mapping RDDs with Multiple Values
Python Version Lambda Function
2.7 rdd.map(lambda (x, y): pow(x * y, 2)).sum()
3.x rdd.map(lambda xy: pow(xy[0] * xy[1], 2)).sum()

Mapping RDDs with Single Values

Python Ver. Solution # Lambda Func.
2.7 1 result_array_ts = result_rdd.map(lambda (ts, voltage): ts).collect()
3.x 1 result_array_ts = result_rdd.map(lambda ts_voltage: ts_voltage[0]).collect()
3.x 2 result_array_voltage = result_rdd.map(lambda ts_voltage: ts_voltage[1]).collect()

Mapping RDDs with Multiple Values

Python Ver. Lambda Func.
2.7 rdd.map(lambda (x, y): pow(x * y, 2)).sum()
3.x rdd.map(lambda xy: pow(xy[0] * xy[1], 2)).sum()
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

1 participant