Is Ruby a functional language?

前端 未结 12 1477
谎友^
谎友^ 2021-01-30 04:09

Wikipedia says Ruby is a functional language, but I\'m not convinced. Why or why not?

相关标签:
12条回答
  • 2021-01-30 04:32

    Ruby does support higher-level functions (see Array#map, inject, & select), but it is still an imperative, Object-Oriented language.

    One of the key characteristics of a functional language it that it avoids mutable state. Functional languages do not have the concept of a variable as you would have in Ruby, C, Java, or any other imperative language.

    Another key characteristic of a functional language is that it focuses on defining a program in terms of "what", rather than "how". When programming in an OO language, we write classes & methods to hide the implementation (the "how") from the "what" (the class/method name), but in the end these methods are still written using a sequence of statements. In a functional language, you do not specify a sequence of execution, even at the lowest level.

    0 讨论(0)
  • 2021-01-30 04:33

    Strictly speaking, it doesn't make sense to describe a language as "functional"; most languages are capable of functional programming. Even C++ is.

    Functional style is more or less a subset of imperative language features, supported with syntactic sugar and some compiler optimizations like immutability and tail-recursion flattening,

    The latter arguably is a minor implementation-specific technicality and has nothing to do with the actual language. The x64 C# 4.0 compiler does tail-recursion optimization, whereas the x86 one doesn't for whatever stupid reason.

    Syntactic sugar can usually be worked around to some extent or another, especially if the language has a programmable precompiler (i.e. C's #define).

    It might be slightly more meaningful to ask, "does language __ support imperative programming?", and the answer, for instance with Lisp, is "no".

    0 讨论(0)
  • 2021-01-30 04:35

    I submit that supporting, or having the ability to program in a language in a functional style does not a functional language make.

    I can even write Java code in a functional style if I want to hurt my collegues, and myself a few months weeks on.

    Having a functional language is not only about what you can do, such as higher-order functions, first-class functions and currying. It is also about what you cannot do, like side-effects in pure functions.

    This is important because it is a big part of the reason why functional programs are, or functional code in generel is, easier to reason about. And when code is easier to reason about, bugs become shallower and float to the conceptual surface where they can be fixed, which in turn gives less buggy code.

    Ruby is object-oriented at its core, so even though it has reasonably good support for a functional style, it is not itself a functional language.

    That's my non-scientific opinion anyway.

    Edit: In retrospect and with consideration for the fine comments I have recieved to this answer thus far, I think the object-oriented versus functional comparison is one of apples and oranges.

    The real differentiator is that of being imparative in execution, or not. Functional languages have the expression as their primary linguistic construct and the order of execution is often undefined or defined as being lazy. Strict execution is possible but only used when needed. In an imparative language, strict execution is the default and while lazy execution is possible, it is often kludgy to do and can have unpredictable results in many edge cases.

    Now, that's my non-scientific opinion.

    0 讨论(0)
  • 2021-01-30 04:35

    Ruby will have to meet the following requirements in order to be "TRUELY" functional.

    Immutable values: once a “variable” is set, it cannot be changed. In Ruby, this means you effectively have to treat variables like constants. The is not fully supported in the language, you will have to freeze each variable manually.

    No side-effects: when passed a given value, a function must always return the same result. This goes hand in hand with having immutable values; a function can never take a value and change it, as this would be causing a side-effect that is tangential to returning a result.

    Higher-order functions: these are functions that allow functions as arguments, or use functions as the return value. This is, arguably, one of the most critical features of any functional language.

    Currying: enabled by higher-order functions, currying is transforming a function that takes multiple arguments into a function that takes one argument. This goes hand in hand with partial function application, which is transforming a multi-argument function into a function that takes less arguments then it did originally.

    Recursion: looping by calling a function from within itself. When you don’t have access to mutable data, recursion is used to build up and chain data construction. This is because looping is not a functional concept, as it requires variables to be passed around to store the state of the loop at a given time.

    Lazy-evaluation, or delayed-evaluation: delaying processing of values until the moment when it is actually needed. If, as an example, you have some code that generated list of Fibonacci numbers with lazy-evaluation enabled, this would not actually be processed and calculated until one of the values in the result was required by another function, such as puts.

    Proposal (Just a thought) I would be of great to have some kind of definition to have a mode directive to declare files with functional paradigm, example

    mode 'functional'

    0 讨论(0)
  • 2021-01-30 04:35

    Ruby isn't really much of a multi-paradigm language either, I think. Multi-paradigm tends to be used by people wanting to label their favorite language as something which is useful in many different areas.

    I'd describe Ruby is an object-oriented scripting language. Yes, functions are first-class objects (sort of), but that doesn't really make it a functional language. IMO, I might add.

    0 讨论(0)
  • 2021-01-30 04:36

    Ruby is an object-oriented language, that can support other paradigms (functional, imperative, etc). However, since everything in Ruby is an object, it's primarily an OO language.

    example:

    "hello".reverse() = "olleh", every string is a string object instance and so on and so forth.

    Read up here or here

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