Defining point of functional programming

前端 未结 8 1635
无人及你
无人及你 2020-12-31 01:17

I can enumerate many features of functional programming, but when my friend asked me Could you define functional programming for me? I couldn\'t.

相关标签:
8条回答
  • 2020-12-31 01:26

    I have to add that functional programming tends to also abstract control structures of your program as well as the domain - e.g., you no longer do a 'for loop' on some list of things, but you 'map' it with some function to produce the output.

    i think functional programming is a state of mind as well as the definition given above.

    0 讨论(0)
  • 2020-12-31 01:29

    A lot of the definitions so far have emphasized purity, but there are many languages that are considered functional that are not at all pure (e.g., ML, Scheme). I think the key properties that make a language "functional" are:

    1. Higher-order functions. Functions are a built-in datatype no different from integers and booleans. Anonymous functions are easy to create and idiomatic (e.g., lambdas).
    2. Everything is an expression. In imperative languages, a distinction is made between statements, which mutate state and affect control flow, and expressions, which yield values. In functional languages (even impure functional languages), expression evaluation is the fundamental unit of execution.

    Given these two properties, you naturally get the behavior we think of as functional (e.g., expressing computations in terms of folds and maps). Eliminating mutable state is a way to make things even more functional.

    0 讨论(0)
  • 2020-12-31 01:32

    There are two separate definitions:

    • The older definition (first-class functions) has been given by Chris Conway.

    • The newer definition (avoiding side effects like mutation) has been given by John Stauffer. This is more generally known as purely functional programming.

    This is a source of much confusion...

    0 讨论(0)
  • 2020-12-31 01:33

    From wikipedia:

    In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state.

    Using a functional approach gives the following benefits:

    • Concurrent programming is much easier in functional languages.
    • Functions in FP can never cause side effects - this makes unit testing much easier.
    • Hot Code Deployment in production environments is much easier.
    • Functional languages can be reasoned about mathematically.
    • Lazy evaluation provides potential for performance optimizations.
    • More expressive - closures, pattern matching, advanced type systems etc. allow programmers to 'say what they mean' more readily.
    • Brevity - for some classes of program a functional solution is significantly more concise.

    There is a great article with more detail here.

    0 讨论(0)
  • 2020-12-31 01:33

    It's like drawing a picture by using vectors instead of bitmaps - tell the painter how to change the picture instead of what the picture looks like at each step.

    It's application of functions as opposed to changing the state.

    0 讨论(0)
  • 2020-12-31 01:37

    I think John Stauffer mostly has the definition. I would also add that you need to be able to pass functions around. Essentially you need high order functions, meaning you can pass functions around easily (although passing blocks is good enough).

    For example a very popular functional call is map. It is basically equivalent to

    list is some list of items
    OutList is some empty list
    foreach item in list
         OutList.append(function(item))
    return OutList
    

    so that code is expressed as map(function, list). The revolutionary concept is that function is a function. Javascript is a great example of a language with high order functions. Basically functions can be treated like a variable and passed into functions or returned from functions. C++ and C have function pointers which can be used similarly. .NET delegates can also be used similarly.

    then you can think of all sorts of cool abstractions...

    Do you have a function AddItemsInList, MultiplyItemsInList, etc..?
    Each function takes (List) and returns a single result

    You could create (note, many languages do not allow you to pass + around as a function but it seems the clearest way to express the concept)....

    AggregateItemsInList(List, combinefunction, StepFunction)
    

    Increment functions work on indexes...better would be to make them work on list using list operations like next and for incTwo next next if it exists....

    function incNormal(x) {
      return x + 1
    }
    
    function incTwo(x) {
      return x + 2
    }
    
    AggregateItemsInList(List, +, incNormal)
    

    Want to do every other item?

    AggegateItemsInList(List, +, incTwo)
    

    Want to multiply?

    AggregateItemsInList(List, *, incNormal)
    

    Want to add exam scores together?

    function AddScores (studenta, studentb) {
       return studenta.score + studentb.score
    }
    
    AggregateItemsInList(ListOfStudents, AddScores, incOne)
    

    High order functions are a very powerful abstraction. Instead of having to write custom methods for numbers, strings, students, etc.. you have one aggregate method that loops through a list of anything and you just have to create the addition operation for each data type.

    0 讨论(0)
提交回复
热议问题