DEV Community

Cover image for Represent pair data structure using functions.
a c sreedhar reddy
a c sreedhar reddy

Posted on

2 2

Represent pair data structure using functions.

Pair

Pair is a data structure which is used to combine two values.

(1, 4)
("sreedhar", "pavan")
Enter fullscreen mode Exit fullscreen mode

Javascript does not provide this data structure. So let us implement this.

Represent pairs in JS

Pairs can be represented using an object or an array

//Represent pair using object
function pair(a,b){
    return {first: a, second: b};
}
function first(p){
    return p.first;
}
function second(p){
  return p.second;
}

//use the pair
const p = pair(12, 13)

const f = first(p)
console.log(f) // logs 12

const s = second(p)
console.log(s) // logs 13

Enter fullscreen mode Exit fullscreen mode
//Represent pair using array.
function pair(a, b) {
  return [a, b];
}
function first(p) {
  return p[0];
}
function second(p) {
  return p[1];
}

//use the pair
const p = pair(12, 13);

const f = first(p);
console.log(f); // logs 12

const s = second(p);
console.log(s); // logs 13
Enter fullscreen mode Exit fullscreen mode

But is there a way to represent pair in terms of functions without using object or array?

Think for some time before you proceed.
.
.
.
Yes there is a way

Represent pair using functions

Consider our pair function returns a function.

function pair(a, b) {
  return (value) => (value === 0 ? a : b);
}
Enter fullscreen mode Exit fullscreen mode

Now this returned function when called with 0 returns the first element and when called with 1 returns the second element.

so we can implement first and second functions as

function first(p) {
  return p(0);
}

function second(p) {
  return p(1);
}
Enter fullscreen mode Exit fullscreen mode

Finally with these functions we can represent pair using functions.

function pair(a, b) {
  return (value) => (value === 0 ? a : b);
}
function first(p) {
  return p(0);
}

function second(p) {
  return p(1);
}

//use the pair
const p = pair(12, 13);

const f = first(p);
console.log(f); // logs 12

const s = second(p);
console.log(s); // logs 13
Enter fullscreen mode Exit fullscreen mode

Conclusion

I would never implement pair using functions. But it was fun to realise that I can.
I found this in a section in SICP book.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay