I'll say it again here in my answer: you're asking about functional programming but everything you describe comes from OOP.
In general, you don't ask what objects can do in functional programming. Instead, you have known functions that are capable of operating on known input types and return known output types.
Some first order functions
const add = (x, y) =>
x + y
const mult = (x, y) =>
x * y
First order data abstraction, a rational
number module
const rational = (numer, denom) =>
[ numer, denom ]
const numerator = r =>
r [0]
const denominator = r =>
r [1]
We can add functions to the module
const string_of_rational = r =>
`(${numerator (r)} / ${denominator (r)})`
const add_rational = (r1, r2) =>
rational
( add ( mult ( numerator (r1)
, denominator (r2)
)
, mult ( numerator (r2)
, denominator (r1)
)
)
, mult ( denominator (r1)
, denominator (r2)
)
)
An example program using our module
const a = rational (1, 2)
const b = rational (1, 4)
const c = add_rational (a, b)
console.log (string_of_rational (c))
// (1 / 2) + (1 / 4) =
//=> (6 / 8)
Data abstraction is important because it preserves our program's functionality even if the underlying representation of data changes.
Above, we used a simple array []
to store the two values for our rational numbers. We can use a different implementation if we wanted
const rational = (numer, denom) =>
({ numer, denom })
const numerator = r =>
r.numer
const denominator = r =>
r.denom
And our program still works
const a = rational (1, 2)
const b = rational (1, 4)
const c = add_rational (a, b)
console.log (string_of_rational (c))
// (1 / 2) + (1 / 4) =
//=> (6 / 8)
It's up to you how to package and implement your modules. In this example, we have all the functions related to our rational numbers in a file called rational.js
// import whatever you need from your module,
// and make sure the module exports it!
const { make, add, string_of } = require ('./rational')
// our program works the same
const a = make (1, 2)
const b = make (1, 4)
const c = add (a, b)
console.log (string_of (c))
// (1 / 2) + (1 / 4) =
//=> (6 / 8)
I recommend Structure and Interpretation of Computer Programs, 2nd edition by Gerald Jay Sussman and Harold Abelson. It will teach you what is most important about functional programming, the function. This book will give you a strong foundation to work your way through the more advanced concepts in any functional language.