Skip to content

Function argument pattern matching...in JavaScript 😮

Notifications You must be signed in to change notification settings

moosch/argument-pattern-matching

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Function pattern matching

Function pattern matching is a very powerful tool found in other languages. It lets you defined functions with the same name but with different arguments and contents.

For example (in Elixir)

def my_fn(first_name, last_name) do
  "Hello #{first_name} #{last_name}"
end

def my_fn(first_name) do
  "Hello #{first_name}"
end

Now calling my_fn("Wil") returns "Hello Wil" and calling my_fn("Wil", "Wheaton") returns "Hello Wil Wheaton". Pretty simple and yes it's a contrived example but it shows a lot of power.

To take this further we could do pattern matching over function arguments by datatype. Imagine that!

function myFn(a: Number, b: Number): Number {
  return a + b;
}

function myFn(a: String, b: Number): String {
  return `${a} is ${b}`;
}

Now we know that this will not run as the same function name has been used twice, but pretty cool if it did right?

Through the power of JavaScript we can make it happen...kinda...ish.

Usage

const fpm = require('./fpm');
const defMyConcatFunc = fpm('myConcatFunc');

// Define some match cases
defMyConcatFunc.add = (a = Array, b = Number) =>
  a.concat([b]);

defMyConcatFunc.add = (a = Array, b = Number, c = String) =>
  a.concat([b]).concat([c]);

defMyConcatFunc.add = (a = Array, b = Number, c = Number) =>
  a.concat([b + c]);

// Run your function
myConcatFunc([1], 1, 5); // => [1, 6]
myConcatFunc([], 1); // => [1]
myConcatFunc([1], 1, '5'); // => [1, 1, '5']

Improvements

  • Add arguments.length to possibly improve speed of checking

  • Store the function name, argument sets and function in AST style might be more performant

License

MIT Licensed. Use all you like at your own risky fun. Go nuts.

Happy coding λ

About

Function argument pattern matching...in JavaScript 😮

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published