Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Functional Programming with Array Methods

- 17.08.26 - ErcanOPAK

๐Ÿงช Functional Programming = Clean Code

Imperative code is hard to maintain. Functional programming with array methods creates cleaner, more predictable code.

๐Ÿ“ Functional Patterns

// Pure Functions
const add = (a, b) => a + b;  // Pure
const getTotal = (cart) => cart.reduce(add, 0);  // Pure

// Higher-Order Functions
const map = (fn, arr) => arr.map(fn);
const filter = (fn, arr) => arr.filter(fn);
const reduce = (fn, initial, arr) => arr.reduce(fn, initial);

// Function Composition
const compose = (...fns) => (x) => 
    fns.reduceRight((acc, fn) => fn(acc), x);

const pipe = (...fns) => (x) => 
    fns.reduce((acc, fn) => fn(acc), x);

// Example: Data Pipeline
const processUsers = pipe(
    (users) => users.filter(u => u.active),
    (users) => users.map(u => ({ name: u.name, age: u.age })),
    (users) => users.sort((a, b) => a.age - b.age),
    (users) => users.reduce((acc, u) => {
        acc[u.age] = (acc[u.age] || 0) + 1;
        return acc;
    }, {})
);

// Currying
const multiply = (a) => (b) => a * b;
const double = multiply(2);
const triple = multiply(3);

// Immutability
const updateUser = (user, updates) => ({
    ...user,
    ...updates
});

const addItem = (cart, item) => [...cart, item];
const removeItem = (cart, id) => cart.filter(item => item.id !== id);

๐ŸŽฏ Advanced Functional Programming

// Memoization
const memoize = (fn) => {
    const cache = new Map();
    return (...args) => {
        const key = JSON.stringify(args);
        if (!cache.has(key)) {
            cache.set(key, fn(...args));
        }
        return cache.get(key);
    };
};

const expensiveFn = (n) => {
    console.log('Computing...');
    return n * n;
};

const memoizedFn = memoize(expensiveFn);
memoizedFn(5); // Computing...
memoizedFn(5); // From cache

// Lazy Evaluation
const lazy = (fn) => {
    let computed = false;
    let result = null;
    return () => {
        if (!computed) {
            result = fn();
            computed = true;
        }
        return result;
    };
};

// Monad Pattern
const Maybe = (value) => ({
    map: (fn) => Maybe(value !== null && value !== undefined ? fn(value) : null),
    chain: (fn) => value !== null && value !== undefined ? fn(value) : Maybe(null),
    get: () => value
});

// Functor Pattern
const map = (fn, functor) => functor.map(fn);

// Applicative Functor
const Applicative = (value) => ({
    map: (fn) => Applicative(fn(value)),
    apply: (functor) => Applicative(functor.get()(value)),
    get: () => value
});

๐Ÿ’ก Functional Programming Tips

  • Use pure functions
  • Avoid side effects
  • Use immutable data
  • Compose functions
  • Use higher-order functions

Functional programming makes your code more predictable and easier to test. It’s a powerful paradigm for modern JavaScript.

โ€” Functional Programming Advocate

Related posts:

JavaScript: Why a Set Still Contains 'Duplicate' Values That Look Completely Identical

JavaScript: Why a Set Still Contains 'Duplicate' Values That Look Completely Identical

JavaScript: Use AbortController to Cancel Multiple Requests at Once

JavaScript: Use AbortController to Cancel Multiple Requests at Once

JavaScript: Fix a NaN Check That Silently Breaks After Swapping Array.prototype.includes() for index...

JavaScript: Fix a NaN Check That Silently Breaks After Swapping Array.prototype.includes() for index...

Views: 10

Post navigation

HTML: Optimize Page Performance with Semantic HTML
AJAX: Real-time Communication with WebSockets

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com