Last active
June 4, 2017 00:57
-
-
Save xaviervia/193a233a6f271776261d7806302989fe to your computer and use it in GitHub Desktop.
Simple Cell recomputation
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
data Recomputation a = Same a | |
| Different a | |
data Cell : Type where | |
MkCell : Eq b => (computation : (a -> b)) -> (value : Recomputation (a, b)) -> Cell | |
recompute : Cell -> Cell | |
recompute (MkCell computation (Same x)) = MkCell computation (Same x) | |
recompute (MkCell computation (Different (a, b))) = | |
let | |
result = computation a | |
in | |
case result == b of | |
True => MkCell computation (Same (a, b)) | |
False => MkCell computation (Different (a, result)) | |
testCell : Cell | |
testCell = MkCell negate (Different (1, 2)) | |
nextCell : Cell | |
nextCell = recompute testCell | |
-- Which results in `MkCell negate (Different (1, -1))` |
Modern PHP (super rusty too):
<?
$recompute = function ($cell) {
if ($cell['different'] == true) {
$result = $cell['computation']($cell['input']);
if ($result == $output) {
return array(
'different' => false,
'computation' => $cell['computation'],
'input' => $cell['input'],
'output' => $cell['output']
);
} else {
return array(
'different' => true,
'computation' => $cell['computation'],
'input' => $cell['input'],
'output' => $result
);
}
} else {
return $cell;
}
};
$testCell = array(
'different' => true,
'computation' => function ($int) { return -$int; },
'input' => 1,
'output' => 2
);
$nextCell = $recompute($testCell);
$finalCell = $recompute($nextCell);
var_dump($testCell);
var_dump($nextCell);
var_dump($finalCell);
?>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Java is rusty: