Skip to content

Instantly share code, notes, and snippets.

@owskio
Created November 27, 2011 16:36
Show Gist options
  • Save owskio/1397775 to your computer and use it in GitHub Desktop.
Save owskio/1397775 to your computer and use it in GitHub Desktop.
FunctionalEuler
#!/sw/bin/js
function euler(firstPoint) {
print("Curr is:" + firstPoint)
function inner(passedPoint) {
var prevAsOuter = passedPoint
return function(currInner) {
print("\nPrev was:" + prevAsOuter)
print("Curr is:" + currInner)
//Rewrite the fn yet again so prev will be stored 4 next time
euler = inner(currInner);
}
}
//Rewrite the fn so that initial conditions are not repeated again
euler = inner(firstPoint)
}
//Test Suite
euler(1); //Output --> "Curr is:1"
euler(2); //Output --> "Prev was:1"
// "Curr is:2"
euler(3); //Output --> "Prev was:2"
// "Curr is:3"
euler(4); //Output --> "Prev was:3"
// "Curr is:4"
euler(5); //Output --> "Prev was:4"
// "Curr is:5"
@owskio
Copy link
Author

owskio commented Nov 27, 2011

This function was originally intended to provide a simple functional means for creating line elements between successive click events on a DOM element (ie if you want to create a set of way-points). However, it just occurred to me that you could use this for an implementation of the functional 'reduce' construct. You could also extend this to remembering more than one previous datum. If you used 2 datums, you could produce a shooting function to solve a 2nd order differential equation.

@owskio
Copy link
Author

owskio commented Nov 27, 2011

If you wanted to make a nice curve between a series of points on an svg canvas, you could do a piece-wise continuous regression based upon the last 2 or 3 datums, instead of storing them all and doing a regression on all the points at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment