Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 628377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:37:51+00:00 2026-05-13T19:37:51+00:00

What are some good examples that I can use to explain functional programming? The

  • 0

What are some good examples that I can use to explain functional programming?

The audience would be people with little programming experience, or people who only have object-oriented experience.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:37:51+00:00Added an answer on May 13, 2026 at 7:37 pm

    The audience would be people with
    little programming experience,

    Is it really possible to explain functional programming, let along OO or procedural or any paradigm of programming to people without much programming experience?

    or people who only have
    object-oriented experience.

    Well probably the best examples would be converting known design patterns into their functional equivalent. Let’s take the canonical example of converting a list of ints to a list of strings:

    using System;
    
    namespace Juliet
    {
        interface IConvertor<T, U>
        {
            U Convert(T value);
        }
    
        class Program
        {
            static U[] Convert<T, U>(T[] input, IConvertor<T, U> convertor)
            {
                U[] res = new U[input.Length];
                for (int i = 0; i < input.Length; i++)
                {
                    res[i] = convertor.Convert(input[i]);
                }
                return res;
            }
    
            class SquareInt : IConvertor<int, string>
            {
                public string Convert(int i)
                {
                    return (i * i).ToString();
                }
            }
    
            class ScaleInt : IConvertor<int, string>
            {
                readonly int Scale;
    
                public ScaleInt(int scale)
                {
                    this.Scale = scale;
                }
    
                public string Convert(int i)
                {
                    return (i * Scale).ToString();
                }
            }
    
            static void Main(string[] args)
            {
                int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
                string[] squared = Convert<int, string>(nums, new SquareInt());
                string[] tripled = Convert<int, string>(nums, new ScaleInt(3));
            }
        }
    }
    

    Its simple, readable, object-oriented, its even generic so it works for any arbitrary types, so what’s the problem? For a start, its bloated: I have an interface definition and two interface implementations. What if I need another conversion? Well, I need another interface implementation — it can get out of hand quickly.

    When you think about it, the IConvertor<T, U> class is just a wrapper around a single function called Convert — the class literally exists to help us pass Convert to other functions. If you can understand this much, then you already understand the basic principles behind functions as first-class values — functional programming is all about passing functions to other functions in pretty much the same way you pass a person or an int or a string.

    People usually prefer functional programming because it helps them avoid single-method interfaces and implementations. Instead of passing a class, we just pass a function by name or anonymously:

    using System;
    
    namespace Juliet
    {
        class Program
        {
            static U[] Convert<T, U>(T[] input, Func<T, U> convertor)
            {
                U[] res = new U[input.Length];
                for (int i = 0; i < input.Length; i++)
                {
                    res[i] = convertor(input[i]);
                }
                return res;
            }
    
            static string SquareInt(int i)
            {
                return (i * i).ToString();
            }
    
            static void Main(string[] args)
            {
                int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
                string[] squared = Convert<int, string>(nums, SquareInt); // pass function by name
                string[] tripled = Convert<int, string>(nums, i => (i * 3).ToString()); // or pass anonymously
            }
        }
    }
    

    Alright, so now we have the exact same program in much fewer lines of code, its readable, and its very evident what its doing.

    Now someone might say “that’s a neat trick, but when would I use it” — there are plenty of cases when you’d want to pass functions around like this. It gives you a lot more power to abstract your programs control flow in novel ways. Adapted from an example shown here, consider a class which handles files:

    class FileFunctions
    {
        internal void SaveFile()
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                File.AppendAllText(fileDialog.FileName, MyDocument.Data);
            }
        }
    
        internal void WriteFile()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                MyDocument.Data = File.ReadAllText(fileDialog.FileName);
            }
        }
    }
    

    Yuck. The code is repetitious, but its performing operations on different classes and invoking different methods. How would you abstract away the duplicated code in the OO universe? In functional programing, it’s trivial:

    class FileFunctions
    {
        internal void ShowDialogThen(FileDialog dialog, Action<FileDialog> f)
        {
            dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                f(dialog);
            }
        }
    
        internal void SaveFile()
        {
            ShowDialogThen(
                new SaveFileDialog(),
                dialog => File.AppendAllText(dialog.FileName, MyDocument.Data));
        }
    
        internal void WriteFile()
        {
            ShowDialogThen(
                new OpenFileDialog(),
                dialog => { MyDocument.Data = File.ReadAllText(dialog.FileName); });
        }
    }
    

    Awesome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 337k
  • Answers 337k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just writing the ZipEntry to the stream is not enough,… May 14, 2026 at 4:04 am
  • Editorial Team
    Editorial Team added an answer Sure: Python. >>> a = 3 >>> b = "2"… May 14, 2026 at 4:04 am
  • Editorial Team
    Editorial Team added an answer to solve Left Join in Subsonic3 you just need to… May 14, 2026 at 4:04 am

Related Questions

I'm running through my web app and I'm trying to test various parts of
There are some good examples on how to calculate word frequencies in C#, but
I'm trying to generate a vector graphic from an area in a bitmap image,
I'm a pretty young developer, and still in the emulation phase of my career.
I would like to be able to spawn a process in python and have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.