Skip to content

Instantly share code, notes, and snippets.

<div id="fullpage">
<div class="section"></div>
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
</div>
</div>
<div id="fullpage">
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
</div>
<!--Load jQuery and fullpage tags -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://rawgit.com/alvarotrigo/fullPage.js/master/jquery.fullPage.js"></script>
<!--Custom script -->
<script src='fullpage.js'></script>
<!--We will overide some default styles here -->
<link rel="stylesheet" href='styles.css'>
var fib = memo(function(n) {
if (n < 2){
return 1;
}else{
//We'll console.log a loader every time we have to recurse
console.log("loading...");
return fib(n-2) + fib(n-1);
}
});
@rayinla
rayinla / memo.js
Last active September 28, 2020 02:15
function memo(func){
var cache = {};
return function(){
var key = JSON.stringify(arguments);
if (cache[key]){
console.log(cache)
return cache[key];
}
else{
val = func.apply(null, arguments);
// A more functional memoizer
//We can beef up our module by adding functions later
var Memoizer = (function(){
//Private data
var cache = {};
//named functions are awesome!
function cacher(func){
return function(){
var key = JSON.stringify(arguments);
function memo(func){
var cache = {};
return function(){
var key = JSON.stringify(arguments);
if (cache[key]){
console.log(cache)
return cache[key];
}
else{
val = func.apply(null, arguments);
//Knowing that we have access to whatever
//the user inputs into our function expression, we then write...
return function(){
var key = JSON.stringify(arguments);
if (cash[key]){
return cache[key];
}
else{
//apply() comes in handy here and will simply
/*
Closures cannot access the arguments object of the parent,
but, because functions are first class objects, we can pass a function as a parameter.
The closure can now access the arguments object of the function that is passesd as a parameter.
So, there is no confusion as to which arguments object we want the closure to access.
We're basically taking advantage of its limitations
*/
function demoMemo(func){
//we must return a function in order to keep state
//this will be more apparant in a recursive example
var zombieOne = (function(){
//private variables
var firstName = "";
var lastName = "";
//private functions
function init(data){
firstName = data.firstName;
lastName = data.lastName;
}