<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged functional-programming</title>
<link>https://coderlegion.com/tag/functional-programming</link>
<description>Powered by Question2Answer</description>
<item>
<title>Crafting a Functional Web Architecture</title>
<link>https://coderlegion.com/20590/crafting-a-functional-web-architecture</link>
<description>&lt;p&gt;Building a web application with a functional mindset requires rethinking how data flows through your system. Instead of viewing your application as a collection of interacting objects that mutate over time, FP treats your application as a series of pure, transformational pipelines. * Component as a Pure Function: The core UI becomes a direct result of your data. In this model, $f(\text{state}) = \text{UI}$. Passing the exact same data into a component will always render the exact same visual output, completely eliminating visual side effects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Immutability by Default:&lt;/strong&gt; Instead of changing existing arrays or objects when a user interacts with the page, you always create fresh copies with the updated values. This makes tracking application changes incredibly straightforward.&lt;br&gt;
&lt;strong&gt;Composition of Logic&lt;/strong&gt;: Small, single-purpose functions are combined like Lego blocks to build complex business logic. You pipe data through validation, formatting, and rendering functions seamlessly.The Roadblocks of Going Fully FunctionalTransitioning to functional programming is rarely a smooth ride, especially in an ecosystem like the web which was fundamentally built on mutable concepts. The biggest hurdle is that the Document Object Model (DOM) is inherently stateful and mutable, creating an immediate architectural conflict.&lt;br&gt;
&lt;strong&gt;The Steep Learning Curve:&lt;/strong&gt; Concepts like currying, monads, higher-order functions, and purity can feel like an alien language to developers used to traditional procedural or object-oriented code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance Overhead:&lt;/strong&gt; Because immutability requires creating new objects rather than modifying existing ones, it can lead to frequent memory allocation and aggressive garbage collection, potentially slowing down low-end devices.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ecosystem Incompatibility:&lt;/strong&gt; A massive portion of third-party web libraries and legacy packages rely on classes and internal state mutation, making smooth integration a challenge.How We Overcome These ChallengesThe web development community hasn't ignored these pain points. Instead, modern tools and frameworks have evolved to bridge the gap between functional purity and practical web performance. We don't need to write purely theoretical code to reap the practical benefits of FP.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Virtual DOM and Reconciliation:&lt;/strong&gt; Frameworks act as a protective translation layer. They allow you to write pure, functional component logic while the underlying engine handles the messy, mutable DOM updates efficiently behind the scenes.Structural Data Sharing: To fix the performance tax of immutability, modern libraries use smart data structures. Instead of copying an entire massive object, they reuse unchanged parts of the data tree, keeping memory usage remarkably low.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pragmatic Hybrid Architectures&lt;/strong&gt;: The most successful web projects don't chase absolute purity. They isolate side effects—like API calls and local storage access—to specific, controlled areas of the app, leaving the vast majority of the codebase clean, predictable, and fully functional.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sumita&lt;br&gt;
Web Developer&lt;/strong&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/20590/crafting-a-functional-web-architecture</guid>
<pubDate>Sun, 14 Jun 2026 17:01:31 +0000</pubDate>
</item>
<item>
<title>Functional Programming Patterns in JavaScript</title>
<link>https://coderlegion.com/3725/functional-programming-patterns-in-javascript</link>
<description>&lt;p&gt;Functional programming (FP) is a paradigm that emphasizes the use of pure functions, immutability, and declarative code. It has become increasingly popular in the JavaScript community due to its ability to produce reliable, maintainable, and scalable code.&lt;/p&gt;
&lt;p&gt;This article explores essential functional programming patterns in JavaScript, offering insights into their use cases, benefits, and practical implementations.&lt;/p&gt;
&lt;h3 id=&quot;introduction-to-functional-programming&quot;&gt;Introduction to Functional Programming&lt;/h3&gt;
&lt;p&gt;Functional programming is built around the idea of composing programs using pure functions and avoiding shared state and side effects. JavaScript, while originally multi-paradigm, offers strong support for functional principles through its first-class functions and built-in methods like map, reduce, and filter.&lt;/p&gt;
&lt;p&gt;Adopting functional patterns leads to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Improved predictability and testability&lt;/li&gt;
&lt;li&gt;Enhanced modularity and reusability&lt;/li&gt;
&lt;li&gt;Cleaner, more readable code&lt;/li&gt;
&lt;li&gt;Fewer side effects and state-related bugs&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;pure-functions&quot;&gt;Pure Functions&lt;/h3&gt;
&lt;p&gt;A function is considered pure if it meets two criteria:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It returns the same output for the same input.&lt;/li&gt;
&lt;li&gt;It does not produce side effects such as modifying external state or performing I/O operations.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Pure function
const add = (a, b) =&amp;gt; a + b;

// Impure function
let count = 0;
const increment = () =&amp;gt; ++count;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pure functions are deterministic, making them easier to test and reason about.&lt;/p&gt;
&lt;h3 id=&quot;function-composition&quot;&gt;Function Composition&lt;/h3&gt;
&lt;p&gt;Function composition refers to combining two or more functions to produce a new function. This approach allows complex operations to be built from simpler ones, improving modularity and reusability.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const addOne = x =&amp;gt; x + 1;
const double = x =&amp;gt; x * 2;

const addOneThenDouble = x =&amp;gt; double(addOne(x));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Generic compose utility:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const compose = (...fns) =&amp;gt; x =&amp;gt; fns.reduceRight((acc, fn) =&amp;gt; fn(acc), x);

const process = compose(double, addOne);
process(3); // Returns 8
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Function composition encourages declarative programming and simplifies data transformation pipelines.&lt;/p&gt;
&lt;h3 id=&quot;currying&quot;&gt;Currying&lt;/h3&gt;
&lt;p&gt;Currying is a technique where a function with multiple arguments is transformed into a series of unary functions, each accepting a single argument.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const multiply = a =&amp;gt; b =&amp;gt; a * b;

const double = multiply(2);
double(5); // Returns 10
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Currying promotes reusability and enables partial application of functions, which is particularly useful when composing higher-order functions.&lt;/p&gt;
&lt;h3 id=&quot;higher-order-functions&quot;&gt;Higher-Order Functions&lt;/h3&gt;
&lt;p&gt;Higher-order functions (HOFs) are functions that accept other functions as arguments or return them as results. JavaScript provides built-in HOFs such as map, filter, and reduce.&lt;/p&gt;
&lt;p&gt;Examples:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Edit[1, 2, 3].map(x =&amp;gt; x * 2);      // [2, 4, 6]
[1, 2, 3].filter(x =&amp;gt; x &amp;gt; 1);  // [2, 3]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Custom higher-order function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const withLogging = fn =&amp;gt; (...args) =&amp;gt; {
  console.log(`Calling with ${args}`);
  return fn(...args);
};

const add = (a, b) =&amp;gt; a + b;
const loggedAdd = withLogging(add);

loggedAdd(3, 4); // Logs input and returns 7
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Higher-order functions enable abstraction of control flow and behavior, allowing for clean and reusable code.&lt;/p&gt;
&lt;h3 id=&quot;partial-application&quot;&gt;Partial Application&lt;/h3&gt;
&lt;p&gt;Partial application refers to pre-filling a function with some of its arguments, returning a new function that requires the remaining arguments.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const greet = (greeting, name) =&amp;gt; `${greeting}, ${name}!`;

const sayHelloTo = name =&amp;gt; greet('Hello', name);
sayHelloTo('Toni'); // &quot;Hello, Toni!&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This technique is useful for creating specialized versions of general-purpose functions.&lt;/p&gt;
&lt;h3 id=&quot;recursion&quot;&gt;Recursion&lt;/h3&gt;
&lt;p&gt;Recursion is a technique where a function calls itself to solve a problem. In functional programming, recursion often replaces loops to maintain immutability.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const factorial = n =&amp;gt; (n &amp;lt;= 1 ? 1 : n * factorial(n - 1));

factorial(5); // Returns 120
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Recursion encourages the creation of elegant and expressive solutions, particularly when combined with immutable data structures.&lt;/p&gt;
&lt;h3 id=&quot;immutability&quot;&gt;Immutability&lt;/h3&gt;
&lt;p&gt;Immutability is a core principle of functional programming that involves avoiding mutations of data structures. Instead of modifying existing data, new copies are created with the necessary changes.&lt;/p&gt;
&lt;p&gt;Examples:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const arr = [1, 2, 3];
const newArr = [...arr, 4]; // [1, 2, 3, 4]

const obj = { name: 'Toni' };
const updated = { ...obj, age: 35 }; // { name: 'Toni', age: 35 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Libraries such as Immer and Immutable.js can help enforce immutability at scale.&lt;/p&gt;
&lt;h3 id=&quot;declarative-array-methods&quot;&gt;Declarative Array Methods&lt;/h3&gt;
&lt;p&gt;Functional programming favors declarative constructs that describe what should happen rather than how it should be done. JavaScript’s array methods support this approach.&lt;/p&gt;
&lt;p&gt;Examples:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const numbers = [1, 2, 3, 4];

const squared = numbers.map(n =&amp;gt; n ** 2);
const evens = numbers.filter(n =&amp;gt; n % 2 === 0);
const sum = numbers.reduce((acc, n) =&amp;gt; acc + n, 0);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These methods abstract away the control flow, resulting in concise and readable code.&lt;/p&gt;
&lt;h3 id=&quot;point-free-style&quot;&gt;Point-Free Style&lt;/h3&gt;
&lt;p&gt;Point-free style, also known as tacit programming, involves writing functions without explicitly referencing their arguments.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const toUpper = str =&amp;gt; str.toUpperCase();
const exclaim = str =&amp;gt; str + '!';

const shout = compose(exclaim, toUpper);
shout('hello'); // &quot;HELLO!&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While point-free style can enhance composability, it should be used judiciously to maintain readability.&lt;/p&gt;
&lt;h3 id=&quot;libraries-and-tools&quot;&gt;Libraries and Tools&lt;/h3&gt;
&lt;p&gt;To facilitate functional programming in JavaScript, several libraries provide utility functions and data structures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ramda – A utility library designed specifically for functional programming&lt;/li&gt;
&lt;li&gt;Lodash/fp – A functional wrapper of Lodash with auto-curried and immutable methods&lt;/li&gt;
&lt;li&gt;RxJS – A library for reactive programming using observables&lt;/li&gt;
&lt;li&gt;Immutable.js – Provides persistent immutable data structures&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These tools can greatly simplify the implementation of functional patterns in large applications.&lt;/p&gt;
&lt;h3 id=&quot;practical-example-data-transformation-pipeline&quot;&gt;Practical Example: Data Transformation Pipeline&lt;/h3&gt;
&lt;p&gt;Consider the task of transforming user data using functional techniques:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const users = [
  { name: 'Toni', age: 30 },
  { name: 'Ana', age: 17 },
  { name: 'Leo', age: 22 },
];

const isAdult = user =&amp;gt; user.age &amp;gt;= 18;
const getName = user =&amp;gt; user.name;
const toUpper = str =&amp;gt; str.toUpperCase();

const adultNamesUpper = users
  .filter(isAdult)
  .map(getName)
  .map(toUpper);

// Result: ['TONI', 'LEO']
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This declarative pipeline is concise, expressive, and easy to modify or extend.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Functional programming patterns offer a powerful way to write clean, modular, and maintainable code in JavaScript. By leveraging pure functions, composition, higher-order functions, and immutability, developers can create applications that are more robust and easier to reason about.&lt;/p&gt;
&lt;p&gt;Incorporating these patterns into everyday development can lead to significant improvements in code quality and team productivity.&lt;/p&gt;
&lt;p&gt;For developers looking to scale their applications or reduce complexity, adopting a functional mindset is a valuable investment.&lt;/p&gt;
</description>
<category>Frontend Dev</category>
<guid isPermaLink="true">https://coderlegion.com/3725/functional-programming-patterns-in-javascript</guid>
<pubDate>Fri, 04 Jul 2025 20:18:30 +0000</pubDate>
</item>
<item>
<title>Dao of Functional Programming (Chapter 1: Clean State)</title>
<link>https://coderlegion.com/3076/dao-of-functional-programming-chapter-1-clean-state</link>
<description>&lt;p&gt;This would be an irregular series of posts on my new attempt to learn Category Theory. This time, it would be based on the book titled &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/BartoszMilewski/DaoFP&quot;&gt;The Dao of Functional Programming&lt;/a&gt; written by &lt;a rel=&quot;nofollow&quot; href=&quot;https://bartoszmilewski.com/about/&quot;&gt;Dr. Bartosz Milewski&lt;/a&gt;. Currently, I am doing it with assistance of NoteBookLM to understand the material better. So let’s start with the first chapter today and answer the question — What are objects and arrows in the world of category theory?&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/1024/1*DhVto7JU_wQfaGMuuBtFkQ.png&quot; alt=&quot;&quot;&gt;&lt;br&gt;
&lt;em&gt;An illustration generated by Copilot&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The current attempt is likely my third or fourth time learning category theory myself. In my previous attempts, I usually got lost in the later chapters. Hopefully, with the help of NotebookLM playing the role as a tutor, things get easier.&lt;/p&gt;
&lt;p&gt;Ah, how things have changed.&lt;/p&gt;
&lt;p&gt;Articles in the series would mostly be unstructured, there would be a very minimal amount of effort in &lt;a rel=&quot;nofollow&quot; href=&quot;https://dev.to/jeffrey04/vibe-coding-storytelling-and-llms-a-collaborative-approach-2l4h&quot;&gt;storytelling&lt;/a&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://kitfucoda.medium.com/vibe-coding-storytelling-and-llms-a-collaborative-approach-db5e9a62c8b1&quot;&gt;.&lt;/a&gt;.) Treat this as my study notes, and I welcome all feedback, insights, corrections etc.&lt;/p&gt;
&lt;h4 id=&quot;notation&quot;&gt;Notation&lt;/h4&gt;
&lt;p&gt;Due to the limitation of Medium’s editor, for Mathematical symbols involving subscripts, say x ** 2 in Python, it would be written as x^2. In contrast, a subscript, would be written as x_2.&lt;/p&gt;
&lt;h4 id=&quot;types-and-functions&quot;&gt;Types and Functions&lt;/h4&gt;
&lt;p&gt;The text advised against associating the concept of type and functions to concrete programming implementation. Therefore, please read the remainder of this article with a clean state of mind (now I understand the rationale behind the chapter’s name).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the context of category theory, a type can be considered as an &lt;em&gt;object&lt;/em&gt; or a &lt;em&gt;proposition&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;We can then assign names to each of them, e.g. a, b, c, or even familiar ones like Int, Bool, Double etc.&lt;/li&gt;
&lt;li&gt;The names are assigned &lt;strong&gt;purely to differentiate them from each other&lt;/strong&gt; whenever applicable.&lt;/li&gt;
&lt;li&gt;The relationship between types, instead of what it is, is more essential.&lt;/li&gt;
&lt;li&gt;A relationship is established by connecting the types with arrows.&lt;/li&gt;
&lt;li&gt;Each arrow connects one source and one target type.&lt;/li&gt;
&lt;li&gt;A special case to consider is that the arrow can loop around, forming a connection with the type itself.&lt;/li&gt;
&lt;li&gt;Depending on the context or field of study, the arrows are named differently. We have been talking about types so far, so in this setting an arrow connecting types is called a function.&lt;/li&gt;
&lt;li&gt;When used in the context of category theory, an arrow connecting objects is called a morphism.&lt;/li&gt;
&lt;li&gt;In the world of logic, an arrow connecting propositions is called an entailment.&lt;/li&gt;
&lt;li&gt;A proposition is something that may be true.&lt;/li&gt;
&lt;li&gt;An implication is usually established with one premise mapping to an outcome or conclusion. The author uses the example “if wishes were horses, beggars would ride”. In this case, “wishes were horses” is the premise, and “beggars would ride” is the outcome. In this case, both premise and outcome are two different kinds of propositions. The implication, written in arrow form &lt;code&gt;(wishes = horses) -&amp;gt; (beggars would ride)&lt;/code&gt;, is therefore an entailment.&lt;/li&gt;
&lt;li&gt;The names of arrows can be used interchangeably, but the core idea is that they establish connections.&lt;/li&gt;
&lt;li&gt;Like what we did for types, we name arrows to differentiate them, as they may be more than one arrow between two types (objects, propositions).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/93/1*BAs6NSg3MlMG1-1YpceS7A.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In this case, the arrow can be interpreted as a function f mapping values of type a to type b.&lt;/li&gt;
&lt;li&gt;Alternatively, it could also mean f is a proof that if a is true then b is also true.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;yin-and-yang&quot;&gt;Yin and Yang&lt;/h4&gt;
&lt;p&gt;I suppose types, and the corresponding functions, are considered as a specialized application of category theory. Each corresponding to a specialized object, and morphism respectively.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We define objects by their connections&lt;/li&gt;
&lt;li&gt;An arrow is a proof/witness of the connection&lt;/li&gt;
&lt;li&gt;If there’s no arrow, there’s no proof, therefore the objects are considered disconnected.&lt;/li&gt;
&lt;li&gt;It is possible to have multiple arrows, hence many proofs.&lt;/li&gt;
&lt;li&gt;Sometimes there’s a single proof — a unique arrow&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unique:&lt;/strong&gt; even if we manage to find two arrows, they &lt;strong&gt;must be identical&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;initial-object&quot;&gt;Initial Object&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;An object with a unique outgoing arrow to every object, including itself.&lt;/li&gt;
&lt;li&gt;Unlike an initial node in a graph, an initial object can have incoming arrows, but in the context of the categories covered (types, logic), we are assuming no incoming arrow for initial objects.&lt;/li&gt;
&lt;li&gt;In Mathematics, often denoted by 0&lt;/li&gt;
&lt;li&gt;The arrow from 0 to any object a is denoted by ¡ (or specifically ¡_a).&lt;/li&gt;
&lt;li&gt;Can be seen as a source of everything.&lt;/li&gt;
&lt;li&gt;As a type in Haskell, it is the Void type.&lt;/li&gt;
&lt;li&gt;As an initial type, there is also an arrow (function) from Void to itself. Hence, it can derive Void and everything else. All the unique outgoing arrows (functions) are all called absurd.&lt;/li&gt;
&lt;li&gt;In logic, it signifies logical falsehood/contradiction/counterfactual, written as F, False or ⊥.&lt;/li&gt;
&lt;li&gt;Again, the outgoing arrow characteristic means we can establish any entailment (proving) starting from false premises. ⊥ -&amp;gt; a, for every proposition a&lt;/li&gt;
&lt;li&gt;The earlier implication “If wishes were horses, beggars would ride” shows that, even the premise “wishes were horses” is false, but the arrow pointing to the proposition “beggars would ride” can still be drawn.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;terminal-object&quot;&gt;Terminal Object&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;An object with a unique incoming arrow from every object&lt;/li&gt;
&lt;li&gt;Unlike a terminal node in a graph in a graph, a terminal object can have outgoing arrows.&lt;/li&gt;
&lt;li&gt;In Mathematics, often denoted by 1&lt;/li&gt;
&lt;li&gt;The arrow from any object a to 1 is denoted by ! (or specifically !_a).&lt;/li&gt;
&lt;li&gt;Can be seen as an object that unites everything.&lt;/li&gt;
&lt;li&gt;As a type in Haskell, it is the Unit type, denoted by ().&lt;/li&gt;
&lt;li&gt;In logic, it signifies the ultimate truth, written as T, True or ⊤.&lt;/li&gt;
&lt;li&gt;The incoming arrow characteristic means any proposition can entail True, regardless what they are. a -&amp;gt; ⊤, for every proposition a&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;| Category theory | Haskell | Logic |
|-------------------|----------|-------------|
| object | type | proposition |
| morphism | function | entailment |
| initial object, 0 | Void | ⊥ |
| terminal object | () | ⊤ |
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;elements&quot;&gt;Elements&lt;/h4&gt;
&lt;h4 id=&quot;structure&quot;&gt;Structure&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Objects are primitive and cannot be broken down into smaller components&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Structure:&lt;/strong&gt; arrows pointing at the object (incoming arrows)&lt;/li&gt;
&lt;li&gt;We can probe an object with arrows.&lt;/li&gt;
&lt;li&gt;Assumption: For programming and logic, we want our initial object to have no structure, therefore no incoming arrow (aside from the self-looping arrow).&lt;/li&gt;
&lt;li&gt;Void and False therefore have no structure.&lt;/li&gt;
&lt;li&gt;Terminal object has only one way of probing it from any direction into the object, hence having the simplest structure.&lt;/li&gt;
&lt;li&gt;Considered as the indivisible point: a single, uniform target reached by a unique connection from anywhere&lt;/li&gt;
&lt;li&gt;Only property is that it exists, proved by the existence arrow from any other object (recall proof: having an arrow connecting objects).&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;probing-other-objects&quot;&gt;Probing other objects&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Terminal object is simple, can use it to probe other more complex objects&lt;/li&gt;
&lt;li&gt;If there are more than one arrow from the terminal object to an object a, then a has some structure.&lt;/li&gt;
&lt;li&gt;Each arrow means a way to look at an object, having multiple arrows means more than a way of looking at it.&lt;/li&gt;
&lt;li&gt;Now we know a terminal project behaves like a (indivisible) point, can visualize each arrow from it as picking another point or element of its target (another object)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;global-element&quot;&gt;Global Element&lt;/h4&gt;
&lt;p&gt;As mentioned previously, initial object can have incoming arrows. However, in the context of the Cartesian closed categories covered, incoming arrows are not allowed.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/96/1*jrZ7ZOT1Mug2r_6XBscg0A.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The arrow x is defined as a &lt;em&gt;(global) element&lt;/em&gt; of_ _&lt;em&gt;a.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;The notation in type theory: x : A, x is of type A&lt;/li&gt;
&lt;li&gt;The Haskell counterpart (use double colon notation): x :: A&lt;/li&gt;
&lt;li&gt;Haskell uses capitalized names for concrete types such as Void, Int, Double etc. In the example, it is A&lt;/li&gt;
&lt;li&gt;Lowercased names for type variable, in the example it is x&lt;/li&gt;
&lt;li&gt;The example defined x as a term of type A&lt;/li&gt;
&lt;li&gt;Categorically, we interpret it as an arrow x : 1 -&amp;gt; A, which is a global element of A&lt;/li&gt;
&lt;li&gt;In logic, x is the proof of A because it corresponds to the implication (a type of entailment, or an arrow) T -&amp;gt; A (if T is true then A is true)&lt;/li&gt;
&lt;li&gt;There may be multiple proofs of A (many arrows from T to A)&lt;/li&gt;
&lt;li&gt;Initial object like Void has no incoming arrow including from the Terminal, therefore Void has no elements.&lt;/li&gt;
&lt;li&gt;Void is therefore considered empty.&lt;/li&gt;
&lt;li&gt;Terminal object has only one unique arrow coming from itself 1 -&amp;gt; 1&lt;/li&gt;
&lt;li&gt;Due to the unique self-pointing arrow, it is sometimes called a &lt;em&gt;singleton&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;the-object-of-arrows&quot;&gt;The Object of Arrows&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Arrows between two objects form a set (with some exceptions, but I am ignoring that for now)&lt;/li&gt;
&lt;li&gt;In programming we talk about the type of functions from a to b, in Haskell it would be f :: a -&amp;gt; b&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Side note:&lt;/strong&gt; In Python it would be f: Callable[[a], [b]]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Meaning:&lt;/strong&gt; f is of the type “function from a to b” (a -&amp;gt; b)&lt;/li&gt;
&lt;li&gt;So if a -&amp;gt; b is a type, then there would be an object that represents the set of arrows (functions) from a to b.&lt;/li&gt;
&lt;li&gt;Further steps to define this object of arrows to be covered in later chapters.&lt;/li&gt;
&lt;li&gt;In addition to the set of arrows, there is an object of arrows.&lt;/li&gt;
&lt;li&gt;Recall, an element of an object is defined as an arrow from a terminal object (Unit) to a target object.&lt;/li&gt;
&lt;li&gt;Therefore, in this case, an element of the object b^a is an arrow from the terminal object to b^a, and this arrow corresponds to a function of type a -&amp;gt; b&lt;/li&gt;
&lt;li&gt;In category theory, the object of arrows is called an exponential, and is written as b^a (source object in the exponent)&lt;/li&gt;
&lt;li&gt;Therefore f :: a -&amp;gt; b, written as an element of an object of arrows is as shown below.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/109/1*nje-BbtzAZrWAiheXcXk5Q.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One thing to note is that if there is no arrow connecting a and b, then the set of arrow a -&amp;gt; b is empty, and we will fail to find the global element arrow (prove) between the terminal object and corresponding b^a object.&lt;/li&gt;
&lt;li&gt;For example, in programming, if there is no way to map an Int to Bool, then the set of the function Int -&amp;gt; Bool is empty, and there will be no elements in the corresponding Int^Bool object.&lt;/li&gt;
&lt;li&gt;In logic, an arrow A -&amp;gt; B is an implication, it states the fact that “if A then B”.&lt;/li&gt;
&lt;li&gt;Therefore, the exponential object B^A, is a proposition in the context of the following expression:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/138/1*RIRhaY5X3-WQeknwv2Ax2g.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Recall in logic T -&amp;gt; A means if T is true, then the proposition A is True.&lt;/li&gt;
&lt;li&gt;Also recall an arrow is a proof where two objects are connected.&lt;/li&gt;
&lt;li&gt;So the global element x means the entailment from A to B exists, and is also valid.&lt;/li&gt;
&lt;li&gt;Therefore when we revisit the statement “If wishes were horses, beggars would ride” as a proposition (an object), then we should be able to prove the arrow from A (wishes were horses) to B (beggars would ride).&lt;/li&gt;
&lt;li&gt;However, in the case where “wishes = horses” cannot be proven as there is no arrow from the ultimate truth to the proposition. Therefore, the original implication is valid, but the premise cannot be found, hence beggars would not ride.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;closing-thoughts&quot;&gt;Closing thoughts&lt;/h4&gt;
&lt;p&gt;Understanding the object of arrows in the context of logic takes up a lot more time than I initially expected, but I am quite happy I finally managed to complete the article. One piece of advice I would share is try to learn as if you have not done much Mathematics before. Prior knowledge in other fields has only brought confusion so far. Anyway as this is going to be irregular, I will continue when I have the time.&lt;/p&gt;
&lt;p&gt;This article was written with assistance from Gemini, a large language model by Google. Gemini provided editorial help, fixing language errors and checking for article flow. If you’d like to collaborate or discuss job opportunities, please connect with me on &lt;a rel=&quot;nofollow&quot; href=&quot;https://linkedin.com/in/jeffrey04/&quot;&gt;LinkedIn&lt;/a&gt; or via &lt;a rel=&quot;nofollow&quot; href=&quot;https://kitfucoda.medium.com/&quot;&gt;Medium&lt;/a&gt;.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/3076/dao-of-functional-programming-chapter-1-clean-state</guid>
<pubDate>Sat, 17 May 2025 20:57:34 +0000</pubDate>
</item>
</channel>
</rss>