https://github.com/glebec/lambda-talk-practical
Some JS techniques and tools which are at least associated with lambda calculus
https://github.com/glebec/lambda-talk-practical
functional-programming javascript lambda-calculus theory
Last synced: 2 months ago
JSON representation
Some JS techniques and tools which are at least associated with lambda calculus
- Host: GitHub
- URL: https://github.com/glebec/lambda-talk-practical
- Owner: glebec
- Created: 2019-07-12T02:55:17.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-15T14:04:11.000Z (almost 6 years ago)
- Last Synced: 2025-04-08T18:54:17.695Z (6 months ago)
- Topics: functional-programming, javascript, lambda-calculus, theory
- Language: JavaScript
- Size: 9.77 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# "Practical" Lambda Calculus
Code demonstrations for talk.
- [Preceding slides on LC](https://speakerdeck.com/glebec/lambda-calc-talk-smartly-dot-io-version)
- [Original longer LC talk at Fullstack Academy](https://github.com/glebec/lambda-talk)## 00 Misc
A small smattering of things which are classically associated with or even originated in the lambda calculus, and which have become staples of mainstream programming and/or the functional programming paradigm. Do these count as "practical" lambda calculus? In a way, any time you are working with functions, you are "doing" practical LC.
## 01 The Z Combinator
Heavily inspired by Bruce J. McAdam's 1997 paper "That About Wraps It Up: Using FIX to Handle Errors Without Exceptions, and Other Programming Tricks".
JS has recursion natively, which most of the time is more than enough. But sometimes recursion's "black box" nature makes it annoying to decorate with e.g. logging, value collection, memoization etc.
Fixed-point combinators like `Y` invent recursion from scratch. The JS-compatible version of the `Y`-combinator is the `Z` combinator. We can re-write our recursive functions as pseudo-recursive "functorials", which can be decorated with custom logic without polluting our problem domain logic. The `Z` combinator then does the recursive plumbing for us.
## 02 Scott Encodings
JS has built-in syntaxes and data structures which map well to inventing _product types_ in type theory. In a product type, a piece of data holds other data in "and" fashion – e.g. a Person is a combination of name _and_ age. Unfortunately, JS lacks convenient support for inventing _sum types_, in which data is separated in _or_ fashion – e.g., a direction is North _or_ South. To work around this, most JS developers re-invent the wheel by reusing existing data types (like strings or booleans) in brittle ways.
Mogensen-Scott encodings are a purely functional way to encode sum and product types, including recursive types, as aggregations of _case-matching functions_. The result is a unified API for modeling domains, in which matching on unknown data forces the user to acknowledge all cases.