D
D
Dima Sokolov2017-02-01 11:26:44
Programming
Dima Sokolov, 2017-02-01 11:26:44

Is functional programming possible in C# without LINQ?

Something more like functional programming in php, js.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arseniy Efremov, 2017-02-01
@dimka11

Quite possible. See:

static IEnumerable<TResult> Map<TIn, TResult>(this IEnumerable<TIn> seq, Func<TIn, TResult> mapper){
    foreach(TIn item in seq) yield return mapper(item);
}
enum Parity { Even, Odd }
static void Test(){
    List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
    Func<int, Parity> parity = (num) => num % 2 == 0 ? Parity.Even : Parity.Odd ;
    foreach(var parity in numbers.Map(parity)) 
        Console.Write(parity.ToString() + ", ");
}

This will display "Odd, Even, Odd, Even, Odd, Even, ".
What is not a functional approach for you? And LINQ is not here (unless homemade).

S
Sumor, 2017-02-01
@Sumor

If you want functional programming in C#, try F#.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question