Skip to content
/ tacit Public

Short talk on tacit programming, meant to be an introduction to functional programming

Notifications You must be signed in to change notification settings

davidmh/tacit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Just in like bash

You can write this

echo "Edward,Chris,Gabriel,Erika,David" | tr , \n | grep -e "^E" | xargs | tr \  ,

Or create a reusable alias

alias getNamesStartingWithE="tr , \\\n | grep -e \"^E\" | xargs | tr \  ,"

And use it

echo "Elaine,Jerry,George,Eric" | getNamesStartingWithE

Compose

Suppose we have two functions f and g

Composing them means we first compute y = g(x), and then use y to compute z = f(y)

In JavaScript

We can do this

const y = g(x);
const z = f(y);

Or if those two functions are useful together frequently

const foo = y => f(g(y));
const z = foo(x);
const z2 = foo(x2);
// ... etc

Or

const foo = compose(f, g);
// foo is the `f` of `g` of something

In Haskell

foo = f . g

Pipe

compose consumes the functions right-to-left, pipe does it left-to-right

Which reads a bit closer to what we are used to in both JavaScript and Bash

const foo = pipe(g, f);
// foo applies `g`, then `f` to something

The Pipeline Operator

ESNext Proposal: The Pipeline Operator

It's a backwards-compatible way of streamlining chained function calls in a readable, functional manner, and provides a practical alternative to extending built-in prototypes.

Rules

Your functions need to be:

  1. Predictable.
const double = x => x * x;
const toUpper = x => x.toUpperCase();
  1. Unary
const map = fn => list => list.map(fn);
const filter = fn => list => list.filter(fn);

const doubleAll = map(double);
// doubleAll is a function ready to receive an array

Caveats

Not all functions are unary.

Currying

Partial application of a variadic function into a fixed number of values

const replace = curry(
  (exp, replacement, str) => str.replace(exp, replacement)
);

const replaceWords = replace(/\w+/g);

const bananafy = replaceWords(
  (word, i) => word.length === 6
    ? 'banana'
    : word
);

const bananasburg = bananafy(`
  Four score and seven years ago our fathers brought forth on this continent, a
  new nation, conceived in Liberty, and dedicated to the proposition that all
  men are created equal.
`);

Algebraic Data Types

Containers or wrappers with shared methods following a specific set of rules

Links

About

Short talk on tacit programming, meant to be an introduction to functional programming

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published