|
| 1 | +const { List } = require('immutable-ext') |
| 2 | +const { fromNullable } = require('./lib') |
| 3 | + |
| 4 | +const Sum = x => ({ |
| 5 | + x, |
| 6 | + |
| 7 | + // destructuring value for the key 'x' |
| 8 | + concat: ({x: y}) => Sum(x + y), |
| 9 | + |
| 10 | + // custom getter used by console.log |
| 11 | + inspect: _ => `Sum(${x})` |
| 12 | +}) |
| 13 | + |
| 14 | +Sum.empty = _ => Sum(0) |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +const res = Sum |
| 21 | + .empty() |
| 22 | + .concat(Sum(1)) |
| 23 | + .concat(Sum(44)) |
| 24 | + |
| 25 | +console.log(res) |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +const All = x => ({ |
| 31 | + x, |
| 32 | + concat: ({x: y}) => All(x && y), |
| 33 | + |
| 34 | + // custom getter used by console.log |
| 35 | + inspect: _ => `All(${x})` |
| 36 | +}) |
| 37 | + |
| 38 | +All.empty = _ => All(true) |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +const resAll = All |
| 44 | + .empty() |
| 45 | + .concat(All(true)) |
| 46 | + .concat(All(true)) |
| 47 | + .concat(All.empty()) |
| 48 | + |
| 49 | +console.log(resAll) |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +// First cannot be promoted to Monoid! |
| 54 | +const First = x => ({ |
| 55 | + x, |
| 56 | + // throw away the arg and keep our First |
| 57 | + concat: _ => First(x), |
| 58 | + |
| 59 | + // custom getter used by console.log |
| 60 | + inspect: _ => `First(${x})` |
| 61 | +}) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +const Max = x => ({ |
| 67 | + x, |
| 68 | + concat: ({x: y}) => Max(x > y ? x : y), |
| 69 | + inspect: _ => `Max(${x})` |
| 70 | +}) |
| 71 | + |
| 72 | +Max.empty = _ => Max(-Infinity) |
| 73 | + |
| 74 | +console.log( |
| 75 | + 'Max(4).concat(Max(5)): ', |
| 76 | + Max(4).concat(Max(5)) |
| 77 | +) |
| 78 | + |
| 79 | +console.log( |
| 80 | + 'Max(4).concat(Max(5)).concat(Max(7)): ', |
| 81 | + Max(4).concat(Max(5)).concat(Max(7)) |
| 82 | +) |
| 83 | + |
| 84 | +console.log( |
| 85 | + 'Max.empty().concat(Max(4)).concat(Max(5)).concat(Max(7)): ', |
| 86 | + Max.empty().concat(Max(4)).concat(Max(5)).concat(Max(7)) |
| 87 | +) |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +// Wrap semigroup into Either |
| 92 | + |
| 93 | +const Right = x => ({ |
| 94 | + |
| 95 | + // f has lifted values |
| 96 | + chain: f => f(x), |
| 97 | + |
| 98 | + // assumes x is a lifted function |
| 99 | + ap: other => other.map(x), |
| 100 | + traverse: (of, f) => f(x).map(Right), |
| 101 | + fold: (f, g) => g(x), |
| 102 | + map: f => Right(f(x)), |
| 103 | + concat: o => o.fold( |
| 104 | + err => Left(err), |
| 105 | + |
| 106 | + // use the concat for x |
| 107 | + res => Right(x.concat(res)) |
| 108 | + ), |
| 109 | + inspect: _ => `Right(${x})` |
| 110 | +}) |
| 111 | + |
| 112 | +const Left = x => ({ |
| 113 | + chain: f => f(x), |
| 114 | + |
| 115 | + // Left ignores apply |
| 116 | + ap: other => Left(x), |
| 117 | + traverse: (of, f) => of(Left(x)), |
| 118 | + fold: (f, g) => f(x), |
| 119 | + |
| 120 | + // ignore f when apply to Left |
| 121 | + map: f => Left(x), |
| 122 | + |
| 123 | + // ignore concat when apply to Left |
| 124 | + concat: o => o.fold( |
| 125 | + _ => Left(x), |
| 126 | + y => o |
| 127 | + ), |
| 128 | + inspect: _ => `Left(${x})` |
| 129 | +}) |
| 130 | + |
| 131 | +const stats = List.of( |
| 132 | + {page: 'Home', views: 40}, |
| 133 | + {page: 'About', views: 10}, |
| 134 | + {page: 'Blog', views: 4} |
| 135 | +) |
| 136 | +const totalStats = stats.foldMap(x => |
| 137 | + fromNullable(x.views).map(Sum), |
| 138 | + Right(Sum(0)) |
| 139 | +) |
| 140 | +// Right(Sum(54)) |
| 141 | +// console.log(totalStats) |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +// Wrap Either into First |
| 147 | +const FirstEither = either => ({ |
| 148 | + fold: f => f(either), |
| 149 | + concat: o => either.isLeft ? o : FirstEither(either), |
| 150 | + inspect: _ => `FirstEither(${either.inspect()})` |
| 151 | +}) |
| 152 | + |
| 153 | +FirstEither.empty = _ => FirstEither(Left()) |
| 154 | + |
| 155 | +console.log( |
| 156 | + FirstEither(Right(55)), |
| 157 | + FirstEither.empty().concat(FirstEither(Right(55))) |
| 158 | +) |
| 159 | + |
| 160 | +const find = (xs, f) => List(xs).foldMap( |
| 161 | + x => FirstEither(f(x) ? Right(x) : Left()), |
| 162 | + FirstEither.empty() |
| 163 | +) |
| 164 | +.fold(x => x) |
| 165 | + |
| 166 | +console.log( |
| 167 | + find([3,4,5,6,7], x => x > 4) |
| 168 | +) |
| 169 | +// => Right(5) ??? |
| 170 | + |
| 171 | + |
| 172 | +const sum = xs => |
| 173 | + xs.reduce((acc, x) => acc + x, 0) |
| 174 | + |
| 175 | +const all = xs => |
| 176 | + xs.reduce((acc, x) => acc && x, true) |
| 177 | + |
| 178 | +const first = xs => |
| 179 | + xs.reduce((acc, x) => acc) |
| 180 | + |
| 181 | +console.log(sum([1,3,4])) |
| 182 | +console.log(all([true, false, true])) |
| 183 | + |
| 184 | +// unsafe if empty array is provided, |
| 185 | +// because no Monoid structure |
| 186 | +console.log(first([1,3,4])) |
0 commit comments