DEV Community

Cole Rau
Cole Rau

Posted on

5

Map, Reduce, Filter/Select: Ruby vs. JavaScript

Map

# Ruby
def addTwoZeros(array)
  array.map { |element| element * 100 }
end 



// JavaScript
const addTwoZeros = array => {
  return array.map((element) => {
    return element * 100
  })
}
Enter fullscreen mode Exit fullscreen mode

The above functions/methods take in a list of numbers, add two zeros to the end of each number, and return a new list of transformed numbers. The main idea behind map is transforming each member in an array and putting it into a new array. This new array will eventually hold a collection of transformed elements in the same order as the original array.

In the Ruby implementation, each element of the array goes through |element| and undergoes the code to the right of |element|. That is, each element goes through the pipes || and gets 100 multiplied to it.

In the JavaScript implementation, the map function gets another function passed to it. Each element of the array gets passed through the (element) of this secondary function. Then the element gets 100 multiplied to it.

Reduce

# Ruby
def getProduct(array)
  array.reduce do |accumulator, element|
    accumulator * element
  end
end 



// JavaScript
const getProduct = array => {
  return array.reduce((accumulator, element) => {
    return accumulator * element
  })
}
Enter fullscreen mode Exit fullscreen mode

The above functions/methods take in an array, multiply all the elements together, then return the result. The main idea behind reduce is taking a bunch of things and reducing them down to a single value.

In the Ruby implementation, each element of the array goes through the element part of |accumulator, element| and gets multiplied to accumulator.

In the JavaScript implementation, the reduce function gets another function passed to it. Each element of the array gets passed through the (element) of this secondary function, then multiplied to accumulator.

Filter/Select

# Ruby
def getOddNums(array)
  array.select { |element| element % 2 != 0 }
end 



// JavaScript
const getOddNums = array => {
  return array.filter((element) => {
    return element % 2 !== 0
  })
}
Enter fullscreen mode Exit fullscreen mode

The above functions/methods take in an array and return only the elements that are odd. The main idea behind filter / select is passing each element to a block; if the element makes the block truthy, the element is added to a new array.

In the Ruby implementation, each element of the array goes through |element| and undergoes the code to the right of |element|. If the element makes the code to the right of |element| truthy, then that element is added to a new array. If the element does not make the code to the right of |element| truthy, the element is not added to the new array.

In the JavaScript implementation, the filter function gets another function passed to it. Each element of the array gets passed through the (element) of this secondary function. If the element makes element % 2 !== 0 truthy, then that element is added to a new array. If the element does not make element % 2 !== 0 truthy, the element is not added to the new array.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more