Last active
January 29, 2017 19:02
-
-
Save xaviervia/17830805718ff2fe8942115a59c0dfbd to your computer and use it in GitHub Desktop.
2017-01-28 notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Cell = f => { | |
const _c = Arrow((x, [h, t]) => { | |
const fx = f(x) | |
cond( | |
[() => fx === h, Left([h, t])]] | |
[true, Right([fx, t])]] | |
) | |
}) | |
_c.pipe = g => Arrow(id).sum(g).compose(_c) | |
return _c | |
} | |
const add1Cell = Cell( x => x + 1 ) | |
const mul2Cell = Cell( y => y * 2 ) | |
const add1ThenMul2Cell = add1Cell.pipe(mul2Cell) | |
const add1Values = add1Cell(1, [1, 2]) // => Left([1, 2]) | |
const add1NewValues = add1Cell(2, [1, 2]) // => Right([2, 3]) | |
const values = add1ThenMul2Cell(1, [1, [2, 4]]) // Left([1, [2, 4]]) | |
const newValues = add1ThenMul2Cell(1, [2, [4, 6]]) // Right([2, Right([3, 6])]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HardEqualityCell = Cell( (a, b) => a === b ) | |
const dom = HardEqualityCell(x => (x + 1), x2 => render(x2)) | |
dom(2) | |
dom(2) | |
m(x => x + 1) => Left y | Right y | |
.map(x => ) | |
Task | |
effect = [ | |
x => x % 4, | |
[ | |
x2 => x2 * 2, | |
y => Math.cos(y) | |
] | |
] | |
const myCell2 = cell( x => x % 4, cell( x => x * 2, cell( y => y / 2 ))) | |
// [output, args] | |
const outputsList = myCell2(2, [0, 0]) // [2, [4, 2]] | |
myCell2(6, [2, [7, 2]]) // [2, [4, 2]] | |
myCell2({'man', timestamp}, ['man', [4, 2]]) // [3, [6, 3]] | |
:t myCell2 | |
const myCell = cell(f1, cell(f2, f3)) | |
[ | |
1, | |
[ | |
2, | |
3 | |
] | |
] = myCell(2, [1, 2, 3]) | |
args = [ | |
1, // args effect[0] | |
2 // args effect[1] | |
] | |
app({ | |
actions: [], | |
state, | |
effects: [ | |
[cell, lastArgs] | |
] | |
}) | |
const myCell = cell(x => x + 2, id) | |
myCell(2, [0, 0]) // [2, 2] | |
myCell(3, [3]) // [3, 2] | |
const sum1 = cell( x => x + 1 ) | |
const mul2 = cell( y => y * 2 ) | |
const myCell = sum1.cellComposition(mul2) | |
sum1([1, undefined]) // => [1, 2] | |
myCell([1, undefined]) | |
// [1, 2] -> mul2([2, undefined]) | |
myCell(1, [1, [2, 4]]) // => [1, [2, 4]] | |
const myGraph = ComputationGraph([ | |
x => x * 2, | |
y => y + 2, | |
[ | |
y => y % 5, | |
] | |
]) | |
myGraph( | |
[ | |
2, | |
2, | |
[ | |
6 | |
] | |
], | |
[ | |
2, | |
4, | |
[ | |
8 | |
] | |
] | |
) | |
leostera
commented
Jan 28, 2017
Add(1).concat(Add(2)) => Add(3)
Add(1).map(x => x * 3) => Add(3)
Box(1).map(x => x * 3) => Box(3)
// List (canon)
List.of(a) => List a
Cell.of(a) => Cell a
// What?
List([a]) => List a
Cell((a -> b)) => Cell ((a, (h, t)) => (h, t))
export type CellFn = (fn: Function) => CellT
const Cell: CellFn = f => {
const _c = (x, [h, t]) => {
const fx = f(x)
return cond(
[() => fx === h, Left([h, t])],
[true, Right([fx, t])])
}
// first comes from product
// second comes from first
_c.compose = g => _c.compose(g.sum(Cell(id)))
// pipe comes from compose
_c.product = g => Cell( pair(f)(g) )
// fanout comes from product
_c.sum = g => Cell( ([t,a]) =>
cond(
[eq(t, 'Left'), Left(f(a))],
[eq(t, 'Right'), Right(g(a))]))
// fanin comes from sum and pipe
// left comes from sum
_c.right = x => Cell(id).sum(f)(x)
return _c
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment