Created
June 17, 2021 20:39
-
-
Save IvanVergiliev/4b648a476aec4387b076f308fa294bcb to your computer and use it in GitHub Desktop.
Balanced fold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.spark.sql.Column | |
def treeFold( | |
columns: List[Column], | |
initialValue: Column, | |
op: (Column, Column) => Column | |
): Column = { | |
if (columns.isEmpty) { | |
initialValue | |
} else if (columns.length == 1) { | |
columns.head | |
} else { | |
val (left, right) = columns.splitAt(columns.length / 2) | |
op( | |
treeFold(left, initialValue, op), | |
treeFold(right, initialValue, op) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment