📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Point-free via equational reasoning
  • Point-free refactor
  • Value of the refactor
  • Advanced Point-free Techniques
  1. Javascript
  2. Functional Light Javascript

Point Free

Fixed-point functions are where you take one of the function's inputs and fix it at a certain value. As a result, if you were to graph that input along an axis, it would always be fixed in one position. Kind of like this:

function multiply(x, y) {
  x = 3;
  return x * y;
}

In contrast, point-free functions are functions where you don't need to define its points, i.e., inputs.

Point-free via equational reasoning

Imagine a function that takes a callback, where that callback calls another function:

getPerson(function onPerson(person) {
  return renderPerson(person);
});

Notice that onPerson and renderPerson always accept the same inputs and return the same outputs. They have the same shape.

As a result, they are interchangeable, so we can replace onPerson with renderPerson directly. (Note: If you've reasoned like this, it's called equational reasoning.)

getPerson(renderPerson);

Important: The key takeaway is that renderPerson is point-free. You don't have to explicitly include the person point.

Point-free refactor

Take the following function relationship:

function isOdd(num) {
  return num % 2 === 1;
}

function isEven(num) {
  return !isOdd(num);
}

This is pretty good code; it shows the negation relationship between isOdd and isEven.

But by using an adapter function, we can refactor isEven to be point-free.

// This adapter function is formally called a "complement"
function not(fn) {
  return function negated(...args) {
    return !fn(...args);
  };
}

function isOdd(num) {
  return num % 2 === 1;
}

// Generates the complement of isOdd
const isEven = not(isOdd);

Value of the refactor

In the original implementation, isEven had unnecessary imperative details. Specifically, we didn't need to know that num was accepted as a parameter and passed to isOdd as an argument.

By using a not adapter function and removing the need for the num point, we free up the reader to see the negation relationship between isOdd and isEven even more clearly.

We are effectively moving towards a more declarative style.

Note: Often times, declarative programming is clearer by being more implicit. Declarative code says a lot without needing to literally say it.

Advanced Point-free Techniques

We can refactor isOdd to be point-free as well! We just need adapter functions for modulus and strict equality.

function mod(y) {
  return function forX(x) {
    return x % y;
  };
}

function eq(y) {
  return function forX(x) {
    return x === y;
  };
}

const mod2 = mod(2);
const eq1 = eq(1);

function isOdd(num) {
  return eq1(mod2(num));
}

(Note: Notice how in our currying functions, mod and eq, we pass y first. This is on purpose. Generally, our code is more ergonomic and usable when we pass y first.)

Now isOdd is defined in terms of mod and eq, where the return value of mod2 becomes the argument for eq1. This pattern is known as composition.

We can therefore refactor once more to use a compose adapter function.

function compose(fn2, fn1) {
  return function composed(x) {
    return fn2(fn1(x));
  };
}

const isOdd = compose(eq(1), mod(2));

Note: Using equational reasoning, we swap eq1 with eq(1) and mod2 with mod(2).

PreviousArgument AdaptersNextClosure

Last updated 3 years ago