Day 8 of 30 - Ruby Coding Challenge - Factorial Numbers - Functional Version 1

DZone 's Guide to

Day 8 of 30 - Ruby Coding Challenge - Factorial Numbers - Functional Version 1

Day 8 of 30 - Ruby Coding Challenges in 30 Days. We're going to explore how to calculate Factorial Numbers in Ruby using a more declarative approach, which is a concept borrowed from functional programming

· Web Dev Zone ·
Free Resource

Hey folks!

In today’s post, which is the writing version of the Youtube video, we’ll finally achieve our dreams of writing the Factorial Algorithm in a more Ruby way. This will be very short because, well, that’s the way Ruby solves things ��

#1 - Previous Algorithm Version


It’s easy to understand, however, it could be simpler

#2 - Ruby Way for Factorial Numbers

Ruby allows us to reduce a list of arrays into a single value by applying a method, which in our case will be the multiplier method


What’s happening is:

  • ruby iterates over the numbers, between 1 and the given number
  • then Ruby reduces this list of numbers into a single value by applying the given method, which multiplies the numbers

Pretty straightforward and cool, isn’t it?

In the next video, we’ll explore the final version of this algorithm, which can be frighteningly easier. See you there :)

Don’t forget to keep in touch and say hi Alex

Twitter Youtube Instagram Linkedin GitHub

algorithm, algorithm analysis, coding, coding bootcamp, programming, programming language, rails, ruby, tutorial for beginners

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}