Functional C#: Embracing Functional Programming in a C# World (C# Functional Book 1) by Bueno Carlos

Functional C#: Embracing Functional Programming in a C# World (C# Functional Book 1) by Bueno Carlos

Author:Bueno, Carlos
Language: eng
Format: epub
Published: 2023-05-06T00:00:00+00:00


In this example, we’ve used Query Syntax to find the best flight fares from two different airlines. We’ve also utilized custom extension methods to fetch flight data and keep our code functional and clean.

To make the previous code work in parallel, we can use the AsParallel() extension method from the System.Linq.ParallelEnumerable class. This method enables parallelization of query execution, which can help to improve performance for large data sets and complex operations.

First, we need to add the System.Linq and System.Threading.Tasks namespaces to our extension methods class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

Now, let’s modify the AirlineExtensions class to return Task<IEnumerable> instead of IEnumerable. We’ll use Task.Run() to simulate asynchronous API calls for fetching flight data:

public static class AirlineExtensions

{

public static Task<IEnumerable<Flight>> GetFlightsFromAirline1Async(this string origin)

{

return Task.Run(() =>

{

// Simulate asynchronous API call with Task.Delay

Task.Delay(500).Wait();



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.