<@U06RKAMK0UC> How would I write a workflow that t...
# ask-ai
b
@ancient-wolf-19325 How would I write a workflow that takes a list of integers and creates a task to double each integer and them have the results summed up by another task?
a
To create a Flyte workflow that takes a list of integers, doubles each integer using a task, and then sums up the results using another task, you can follow these steps: 1. Define the task to double each integer. 2. Define the task to sum up the results. 3. Create the workflow to orchestrate these tasks. Here's an example:
Copy code
from flytekit import task, workflow
import typing

# Task to double an integer
@task
def double(x: int) -> int:
    return x * 2

# Task to sum a list of integers
@task
def sum_list(numbers: typing.List[int]) -> int:
    return sum(numbers)

# Workflow to orchestrate the tasks
@workflow
def double_and_sum_workflow(numbers: typing.List[int]) -> int:
    doubled_numbers = [double(x=n) for n in numbers]
    return sum_list(numbers=doubled_numbers)

# Example execution
if __name__ == "__main__":
    result = double_and_sum_workflow(numbers=[1, 2, 3, 4])
    print(f"Result: {result}")
In this example: - The
double
task takes an integer and returns its double. - The
sum_list
task takes a list of integers and returns their sum. - The
double_and_sum_workflow
workflow takes a list of integers, applies the
double
task to each integer, and then sums the results using the
sum_list
task. Would you like more details on any specific part of this workflow? Sources: - Flyte Task and Workflow Definition - Flyte Customizing Task Resources 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
That does not work.

https://files.slack.com/files-pri/TN89P6GGK-F07FUQHLWN4/image.png