Friday, May 10, 2019

5 Functional Programming Languages You Should Know

functional-programming-languages

If you spend any amount of time reading up on programming trends on the internet, you’ll have heard about functional programming. The term is thrown about fairly often, but what does it mean?

Even if you’re aware of what functional programming is, you may be unclear about which languages are best suited for it. After all, not all programming languages are created equal. While you can apply functional programming paradigms in many languages, there are still some where you’ll feel much more comfortable.

What Is Functional Programming?

If you have a background in mathematics, you have a head start on functional programming. This is because the functional programming paradigm treats computing like mathematical functions. If you don’t have a mathematical background, this could leave you feeling confused.

Basically, functional programming treats functions and data as immutable. You pass data into a function, and it generally returns that data transformed or some other type of data. In functional programming, the function should never change the original data or program state.

There is a similarity to the Unix philosophy that each program should do one thing well. A function shouldn’t touch various parts of your program. Instead, it should take its input and give you an output.

Ideally, functions should be pure whenever possible in functional programming. This means that given the same input, the function’s output will always remain the same.

Functional vs. Object-Oriented Programming

This is a dramatic departure from something like object-oriented programming. In object-oriented programming, you often have a base object with various methods dedicated to changing either data or state that are part of that object. A method may even alter data or state if not explicitly stated.

In practical programs, sometimes this makes sense. That said, it can make programs harder to maintain, as it’s not always clear what is altering state or data. Functional programming was originally used in academic environments, but can also help to prevent these sorts of problems.

1. JavaScript

JavaScript logo

Some programming languages allow functional programming while others either encourage or even enforce it. JavaScript falls in the first category. While you can use functional programming paradigms in the language, you can just as easily use an object-oriented approach.

That said, there are plenty of functional programming paradigms built into JavaScript. Take, for example, higher-order functions. These are functions that can take other functions as arguments.

JavaScript has several functions that work with arrays like map(), reduce(), filter(), and others, all of which are higher-order functions. This lets you chain them together to quickly do all manner of things with an array.

While early JavaScript had some issues with mutability, newer versions of the ECMAScript standard provide fixes. Instead of the catch-all var keyword for defining variables, you now have const and let. The first lets you define constants, as the name implies. The second, let, limits the scope of a variable to the function in which it is declared.

2. Python

Python logo

Like JavaScript, Python is a generalized language that you can use any number of programming paradigms with. Python may have its downsides, but functional programming isn’t one of them. There is even an introduction to functional programming in the official Python documentation.

To start, you’ll find many of the same map(), filter(), reduce(), and similar functions mentioned above built-in. As with JavaScript, these are higher-order functions as they take other functions as arguments. In Python, functional programming has an advantage in the form of the lambda keyword.

You can use lambda expressions in a few ways. One way to use it is as shorthand for simple functions. When assigned to a variable, you can call lambda expressions exactly as you would a standard Python function. The real advantage of lambda expressions comes when you use them as anonymous functions.

Anonymous functions work in JavaScript and other languages on this list as well. They come in especially handy when used with higher-order functions since you can define them on the spot. Without anonymous functions, you’d have to pre-define even simple additions as bespoke functions.

3. Clojure

Clojure programming language logo

Unlike JavaScript and Python, Clojure may not exactly be a household name, even among programmers. In case you’re not familiar, Clojure is a dialect of the Lisp programming language, which dates back to the late 1950s. This brings along with it a very specific way of doing things that happens to be perfect for functional programming.

Like other Lisp dialects, Clojure treats code as data. This means that the code can effectively alter itself. Unlike other Lisp dialects, Clojure runs on the Java platform and is compiled to JVM bytecode. This means it can work with Java libraries, whether they were written in Clojure or not.

Unlike previous languages on this list, Clojure is a functional programming language from the ground up. That means that it advocates immutability wherever possible, especially within data structures.

Clojure doesn’t expect all programs to be mathematically “provable,” but encourages using pure functions wherever possible.

4. Elm

Elm programming language logo

One of the newer languages on this list, Elm is a purely functional language initially designed by Evan Czaplicki in 2012. The language has gained popularity among web developers, specifically for creating user interfaces.

Unlike every prior entry on this list, Elm uses static type checking. This helps to ensure no runtime exceptions, with errors instead caught at compile time. This means less visible errors for users, which is a major plus.

The Elm compiler targets HTML, CSS, and JavaScript. In the same way that you can use Clojure to write programs that run on Java, you can write apps that use JavaScript libraries in Elm.

One major difference between Elm and other languages here is that you won’t find generic filter(), map(), and similar functions. Instead, these are defined by data type like List.map or Dict.map.

5. Haskell

Haskell programming language logo

Haskell is another statically typed, purely functional language. Unlike Elm, Haskell has been around for a while. The first version of the language was designed in 1990. The latest standard is Haskell 2010, while the next version is planned for 2020.

As we’ve already explored, Haskell’s purely functional nature means that by design, functions shouldn’t have side effects. This makes it well-suited to solving real-world problems despite functional programming’s roots in academia.

Despite its lack of mainstream popularity, Haskell has been employed in some widely used projects. The Xmonad window manager is written entirely in Haskell. Pandoc, which converts different types of markup to and from other formats also uses the language.

The standard map(), filter(), reduce(), and other higher-order functions are present, which should let you take concepts from JavaScript or Python to Haskell. If you want to learn more about the language, Learn You a Haskell for Great Good is a popular starting point.

Are You New to Programming?

Some of the above terms and languages may seem somewhat esoteric if you’re not yet a seasoned coder. That’s good, as knowing what you don’t know is one of the first steps in becoming a better learner.

Some of the languages listed above are better for beginners than others. Take a look at our list of the best programming languages for beginners. You’ll notice there is some crossover between the two lists.

Read the full article: 5 Functional Programming Languages You Should Know



from MakeUseOf http://bit.ly/2WAY42Y

No comments:

Post a Comment