Recursive Magic: Defining e^x and Trig Functions with Infinite Python Generators

2025-04-21
Recursive Magic: Defining e^x and Trig Functions with Infinite Python Generators

This post showcases a clever trick using infinite Python generators to recursively define mathematical functions. By recursively defining a generator for positive integers and leveraging Taylor series expansions, the author demonstrates how to generate the Taylor series coefficients for e^x, sin x, and cos x recursively, relying only on their integral relationships without predefining their expressions. The post also includes an optimization using a memoize decorator to improve performance and avoid recursion depth limits.

Read more
Development Infinite Generators

A Curious Adventure in Implementing RNG and Cosine in Purely Functional Nix

2025-04-15
A Curious Adventure in Implementing RNG and Cosine in Purely Functional Nix

This post details the author's experience implementing a random number generator and a cosine function within NixOS, a Linux distribution built on the Nix language. The purely functional nature of Nix presents challenges when working with system randomness and standard mathematical functions. The author explores various approaches, including using Nix's `runCommandLocal` and custom infinite list implementations, ultimately overcoming caching and function-call quirks to achieve the goal. The journey highlights Nix's flexibility and power, but also exposes some limitations of its features.

Read more
Development