Functional Programming with Ramda.js

React.js library with a focused FP goal

/logos/2019.09.13-ramda.jpg

Ramda.js

Ramda is a library designed specifically for a functional programming style. It makes it easy to create functional programming pipelines, and never mutates user data. JavaScript’s Array.prototype class has some functionally-flavored methods such as map, reduce, and filter. However, these only operate on arrays. Ramda is described as more generic, being that it can work with strings, objects, and user-defined data types. There are other convenience libraries that allow functional operations on objects, such as lodash, but Ramda is much more functional by design; explicitly eliminating the possibility side-effects, and facilitating a divergence from the very imperative style of JavaScript to a more declarative model of programming.

Composition with Functions

So why would I want to use Ramda when lodash is easier to understand? Ramda allows functions to be used as first-order components.

[Read More]

Functional and Recursive Ruby

This program returns Squares. From a functional perspective, the use of a variable for the ‘range_object’ is technically a side-effect, but I include it here for sake of clarity.

Range objects are a good example of the kind syntactic sugar that I sometimes felt was lacking in JavaScript.

class Squares
  def initialize(n)
    @number = n
  end

  def square_of_sum
    def factorial(n)
      n == 1 ? 1 : n + factorial(n - 1)
    end

    factorial(@number) ** 2
  end

  def sum_of_squares
    range_object = 1..@number
    range_object.reduce { | a, b | a + b ** 2 }
  end

  def difference
    square_of_sum - sum_of_squares
  end
end