tacit
所属分类:其他
开发工具:JavaScript
文件大小:0KB
下载次数:0
上传日期:2017-09-15 20:48:24
上 传 者:
sh-1993
说明: 关于隐性编程的简短谈话,意在介绍函数式编程
(Short talk on tacit programming, meant to be an introduction to functional programming)
文件列表:
.editorconfig (324, 2017-09-15)
curry.debuggable.js (431, 2017-09-15)
index.js (3118, 2017-09-15)
log.js (181, 2017-09-15)
package.json (235, 2017-09-15)
yarn.lock (76767, 2017-09-15)
Just in like bash
=================
You can write this
```bash
echo "Edward,Chris,Gabriel,Erika,David" | tr , \n | grep -e "^E" | xargs | tr \ ,
```
Or create a reusable alias
```bash
alias getNamesStartingWithE="tr , \\\n | grep -e \"^E\" | xargs | tr \ ,"
```
And use it
```bash
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
```javascript
const y = g(x);
const z = f(y);
```
Or if those two functions are useful together frequently
```javascript
const foo = y => f(g(y));
const z = foo(x);
const z2 = foo(x2);
// ... etc
```
Or
```javascript
const foo = compose(f, g);
// foo is the `f` of `g` of something
```
In Haskell
```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
```javascript
const foo = pipe(g, f);
// foo applies `g`, then `f` to something
```
The Pipeline Operator
=====================
[ESNext Proposal: The Pipeline Operator](https://github.com/tc39/proposal-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.
```javascript
const double = x => x * x;
const toUpper = x => x.toUpperCase();
```
2. Unary
```javascript
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
```javascript
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
=====
- [Ramda](http://ramdajs.com)
Like lodash, but all functions are curried and pointfree.
- [Folktale](http://folktale.origamitower.com/)
A standard library for functional programming in JavaScript
- [Crocks](https://github.com/evilsoft/crocks)
A collection of well known Monadic Containers for your utter enjoyment.
- [Professor Frisby's Mostly Adequate Guide to Functional Programming](https://drboolean.gitbooks.io/mostly-adequate-guide/)
A full guide for FP in JS.
- [The Elm Architecture](https://guide.elm-lang.org/architecture/)
Shh, just read it. Trust me.
- [Learn You a Haskell for Great Good](http://learnyouahaskell.com/)
A beginner's guide.
近期下载者:
相关文件:
收藏者: