@tesla809/

currying - fp - Fun Fun Function

JavaScript

Notes on currying from Fun Fun Function's series on functional programing.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// from fun fun function
// Currying - Part 6 of Functional Programming in JavaScript
// https://www.youtube.com/watch?v=iZLP4qOwY8I

// functional programming - the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. You can avoid lots of bugs this way.

// currying- is the process of breaking down a function into a series of functions that each take a single argument. Then the function at the end of the chain returns the value you want.

// The idea of currying is that your function can pass thru the application and gradually recieve the argument it needs.

// Its an idea of the principle of composition.

// uncurried function
let dragon = (name, size, element) => 
  `${name} is a ${size} dragon that breathes ${element}!`;

// curried function
let dragonCurried = 
  name =>
    size =>
      element =>
      `${name} is a ${size} dragon that breathes ${element}!`;

console.log(dragon('fluffykins', 'tiny', 'lighting'));
// curried- notice the paretheses
// They are a chain of functions
console.log(dragonCurried('fluffykins')('tiny')('lighting'));

// say you are gradually getting data in the app.
let fluffykinsDragon = dragonCurried('fluffykins')
let tinyDragon = fluffykinsDragon('tiny')

console.log(tinyDragon('lighting'));

// Every functional library has a way of making regular functions curriable.
// lodash is one of these libraries.
// you could npm install lodash
// All libraries with a functional nature have a curry function.
// write
/*
// goes up top
import _ from 'lodash'

// turns regular function into curried function!
dragonLoDash = _.curry(dragon);
*/

// Why is it useful?
// before
// same dragons array

/*
let hasElement = 
  (element, obj) => obj.element === element;

let lightingDragons =
  dragons.filter(x => hasElement('lighting'), x);
*/

// after
import _ from 'lodash';

let dragons = [
  { name: 'fluffykins', element: 'lighting' },
  { name: 'nibbles', element: 'lighting' },
  { name: 'MarMar', element: 'water' },
  { name: 'Kookie', element: 'fire/cocaine' },
];

let hasElement = 
  _.curry((element, obj) => obj.element === element);

// no need for x => ... 
// since we have curried has element.
// So its "waiting" for the next element already
// No need to pass it in with x =>.
// Just call it and pass it the next element.
let lightingDragons =
  dragons.filter(hasElement('lighting'));

console.log(lightingDragons);

// curried functions are useful when you don't know exactly all the parameters of the function immediately.
// currying is also good for partial application where you can use part of the curried chain for different parts. These different parts are generalized and can be remixed

// generalized
let daFlufferDragon = dragonCurried('fluff');

// now breathes ice and is small
console.log(daFlufferDragon('small')('methols'));
// now breathes smoke and is massive
console.log(daFlufferDragon('massive')('smoke'));

Native Browser JavaScript