Created
March 22, 2015 02:15
-
-
Save samarpanda/53ad77e217cd4f8a1ba8 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/woqevo
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.8.0/ramda.min.js"></script> | |
<script src="http://looprecur.com/hostedjs/pointfree.browser.js"></script> | |
<script src="http://looprecur.com/hostedjs/monoids.js"></script> | |
<script src="http://looprecur.com/hostedjs/maybe.js"></script> | |
<script src="http://looprecur.com/hostedjs/id.js"></script> | |
<script src="http://looprecur.com/hostedjs/io.js"></script> | |
<script src="http://looprecur.com/hostedjs/data.either.umd.js"></script> | |
<script src="http://looprecur.com/hostedjs/data.future.umd.js"></script> | |
<script src="http://looprecur.com/hostedjs/Bacon.min.js"></script> | |
<script src="http://looprecur.com/hostedjs/function.js"></script> | |
<script src="http://looprecur.com/hostedjs/array.js"></script> | |
<script src="http://looprecur.com/hostedjs/string.js"></script> | |
</script> | |
<script src="http://looprecur.com/hostedjs/exec_helpers.js"></script> | |
</head> | |
<body> | |
<input id="text" value="honkey tonk" /> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
var _ = R; | |
var P = PointFree; | |
var map = P.fmap; | |
var compose = P.compose; | |
var Maybe = P.Maybe; | |
var Identity = P.Id; | |
var Either = folktale.data.Either; | |
var Left = Either.Left; | |
var Right = Either.Right; | |
var IO = P.IO.IO; | |
var runIO = P.IO.runIO; | |
P.IO.extendFn(); | |
// Exercise 1 | |
// ========== | |
// Write a function that uses checkActive() and showWelcome() to grant access or return the error | |
console.log("--------Start exercise 1--------") | |
var showWelcome = compose(_.add( "Welcome "), _.get('name')) | |
var checkActive = function(user) { | |
return user.active ? Right(user) : Left('Your account is not active') | |
} | |
var ex1 = compose(map(showWelcome), checkActive); | |
assertDeepEqual(Left('Your account is not active'), ex1({active: false, name: 'Gary'})) | |
assertDeepEqual(Right('Welcome Theresa'), ex1({active: true, name: 'Theresa'})) | |
console.log("exercise 1...ok!") | |
// Exercise 2 | |
// ========== | |
// Write a validation function that checks for a length > 3. It should return Right(x) if it is greater than 3 and Left("You need > 3") otherwise | |
console.log("--------Start exercise 2--------") | |
var ex2 = function(x) { | |
return x.length > 3 ? Right(x) : Left("You need > 3"); | |
} | |
assertDeepEqual(Right("fpguy99"), ex2("fpguy99")) | |
assertDeepEqual(Left("You need > 3"), ex2("...")) | |
console.log("exercise 2...ok!") | |
// Exercise 3 | |
// ========== | |
// Use ex2 above and Either as a functor to save the user if they are valid | |
var save = function(x){ console.log("SAVED USER!"); return x; } | |
var ex3 = compose(map(save), ex2); | |
console.log("--------Start exercise 2--------") | |
assertDeepEqual(Right("fpguy99"), ex3("fpguy99")) | |
assertDeepEqual(Left("You need > 3"), ex3("duh")) | |
console.log("exercise 3...ok!") | |
// Exercise 4 | |
// ========== | |
// Get the text from the input and strip the spaces | |
console.log("--------Start exercise 4--------") | |
var getValue = function(x){ return document.querySelector(x).value }.toIO() | |
var stripSpaces = function(s){ return s.replace(/\s+/g, ''); } | |
var ex4 = compose(map(stripSpaces), getValue); | |
assertEqual("honkeytonk", runIO(ex4('#text'))) | |
console.log("exercise 4...ok!") | |
// Exercise 5 | |
// ========== | |
// Use getHref() / getProtocal() and runIO() to get the protocal of the page. | |
var getHref = function(){ return location.href; }.toIO(); | |
var getProtocal = compose(_.head, _.split('/')) | |
var ex5 = compose(map(getProtocal), getHref) | |
console.log("--------Start exercise 5--------") | |
assertEqual('http:', runIO(ex5(null))) | |
console.log("exercise 5...ok!") | |
// Exercise 6* | |
// ========== | |
// Write a function that returns the Maybe(email) of the User from getCache(). Don't forget to JSON.parse once it's pulled from the cache so you can _.get() the email | |
// setup... | |
localStorage.user = JSON.stringify({email: "[email protected]"}) | |
var getCache = function(x){ return Maybe(localStorage[x]); }.toIO(); | |
var getStringEmail = compose(_.get('email'), JSON.parse); | |
var ex6 = compose(map(map(getStringEmail)), getCache); | |
assertDeepEqual(Maybe("[email protected]"), runIO(ex6('user'))) | |
console.log("exercise 6...ok!") | |
// TEST HELPERS | |
// ===================== | |
function inspectIt(x){ | |
return (x.inspect && x.inspect()) || (x.toString && x.toString()) || x.valueOf(); //hacky for teachy. | |
} | |
function assertEqual(x,y){ | |
if(x !== y){ throw("expected "+x+" to equal "+y); } | |
} | |
function assertDeepEqual(x,y){ | |
if(x.val !== y.val) throw("expected "+inspectIt(x)+" to equal "+inspectIt(y)); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
var _ = R; | |
var P = PointFree; | |
var map = P.fmap; | |
var compose = P.compose; | |
var Maybe = P.Maybe; | |
var Identity = P.Id; | |
var Either = folktale.data.Either; | |
var Left = Either.Left; | |
var Right = Either.Right; | |
var IO = P.IO.IO; | |
var runIO = P.IO.runIO; | |
P.IO.extendFn(); | |
// Exercise 1 | |
// ========== | |
// Write a function that uses checkActive() and showWelcome() to grant access or return the error | |
console.log("--------Start exercise 1--------") | |
var showWelcome = compose(_.add( "Welcome "), _.get('name')) | |
var checkActive = function(user) { | |
return user.active ? Right(user) : Left('Your account is not active') | |
} | |
var ex1 = compose(map(showWelcome), checkActive); | |
assertDeepEqual(Left('Your account is not active'), ex1({active: false, name: 'Gary'})) | |
assertDeepEqual(Right('Welcome Theresa'), ex1({active: true, name: 'Theresa'})) | |
console.log("exercise 1...ok!") | |
// Exercise 2 | |
// ========== | |
// Write a validation function that checks for a length > 3. It should return Right(x) if it is greater than 3 and Left("You need > 3") otherwise | |
console.log("--------Start exercise 2--------") | |
var ex2 = function(x) { | |
return x.length > 3 ? Right(x) : Left("You need > 3"); | |
} | |
assertDeepEqual(Right("fpguy99"), ex2("fpguy99")) | |
assertDeepEqual(Left("You need > 3"), ex2("...")) | |
console.log("exercise 2...ok!") | |
// Exercise 3 | |
// ========== | |
// Use ex2 above and Either as a functor to save the user if they are valid | |
var save = function(x){ console.log("SAVED USER!"); return x; } | |
var ex3 = compose(map(save), ex2); | |
console.log("--------Start exercise 2--------") | |
assertDeepEqual(Right("fpguy99"), ex3("fpguy99")) | |
assertDeepEqual(Left("You need > 3"), ex3("duh")) | |
console.log("exercise 3...ok!") | |
// Exercise 4 | |
// ========== | |
// Get the text from the input and strip the spaces | |
console.log("--------Start exercise 4--------") | |
var getValue = function(x){ return document.querySelector(x).value }.toIO() | |
var stripSpaces = function(s){ return s.replace(/\s+/g, ''); } | |
var ex4 = compose(map(stripSpaces), getValue); | |
assertEqual("honkeytonk", runIO(ex4('#text'))) | |
console.log("exercise 4...ok!") | |
// Exercise 5 | |
// ========== | |
// Use getHref() / getProtocal() and runIO() to get the protocal of the page. | |
var getHref = function(){ return location.href; }.toIO(); | |
var getProtocal = compose(_.head, _.split('/')) | |
var ex5 = compose(map(getProtocal), getHref) | |
console.log("--------Start exercise 5--------") | |
assertEqual('http:', runIO(ex5(null))) | |
console.log("exercise 5...ok!") | |
// Exercise 6* | |
// ========== | |
// Write a function that returns the Maybe(email) of the User from getCache(). Don't forget to JSON.parse once it's pulled from the cache so you can _.get() the email | |
// setup... | |
localStorage.user = JSON.stringify({email: "[email protected]"}) | |
var getCache = function(x){ return Maybe(localStorage[x]); }.toIO(); | |
var getStringEmail = compose(_.get('email'), JSON.parse); | |
var ex6 = compose(map(map(getStringEmail)), getCache); | |
assertDeepEqual(Maybe("[email protected]"), runIO(ex6('user'))) | |
console.log("exercise 6...ok!") | |
// TEST HELPERS | |
// ===================== | |
function inspectIt(x){ | |
return (x.inspect && x.inspect()) || (x.toString && x.toString()) || x.valueOf(); //hacky for teachy. | |
} | |
function assertEqual(x,y){ | |
if(x !== y){ throw("expected "+x+" to equal "+y); } | |
} | |
function assertDeepEqual(x,y){ | |
if(x.val !== y.val) throw("expected "+inspectIt(x)+" to equal "+inspectIt(y)); | |
} | |
</script></body> | |
</html> |
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
console.clear(); | |
var _ = R; | |
var P = PointFree; | |
var map = P.fmap; | |
var compose = P.compose; | |
var Maybe = P.Maybe; | |
var Identity = P.Id; | |
var Either = folktale.data.Either; | |
var Left = Either.Left; | |
var Right = Either.Right; | |
var IO = P.IO.IO; | |
var runIO = P.IO.runIO; | |
P.IO.extendFn(); | |
// Exercise 1 | |
// ========== | |
// Write a function that uses checkActive() and showWelcome() to grant access or return the error | |
console.log("--------Start exercise 1--------") | |
var showWelcome = compose(_.add( "Welcome "), _.get('name')) | |
var checkActive = function(user) { | |
return user.active ? Right(user) : Left('Your account is not active') | |
} | |
var ex1 = compose(map(showWelcome), checkActive); | |
assertDeepEqual(Left('Your account is not active'), ex1({active: false, name: 'Gary'})) | |
assertDeepEqual(Right('Welcome Theresa'), ex1({active: true, name: 'Theresa'})) | |
console.log("exercise 1...ok!") | |
// Exercise 2 | |
// ========== | |
// Write a validation function that checks for a length > 3. It should return Right(x) if it is greater than 3 and Left("You need > 3") otherwise | |
console.log("--------Start exercise 2--------") | |
var ex2 = function(x) { | |
return x.length > 3 ? Right(x) : Left("You need > 3"); | |
} | |
assertDeepEqual(Right("fpguy99"), ex2("fpguy99")) | |
assertDeepEqual(Left("You need > 3"), ex2("...")) | |
console.log("exercise 2...ok!") | |
// Exercise 3 | |
// ========== | |
// Use ex2 above and Either as a functor to save the user if they are valid | |
var save = function(x){ console.log("SAVED USER!"); return x; } | |
var ex3 = compose(map(save), ex2); | |
console.log("--------Start exercise 2--------") | |
assertDeepEqual(Right("fpguy99"), ex3("fpguy99")) | |
assertDeepEqual(Left("You need > 3"), ex3("duh")) | |
console.log("exercise 3...ok!") | |
// Exercise 4 | |
// ========== | |
// Get the text from the input and strip the spaces | |
console.log("--------Start exercise 4--------") | |
var getValue = function(x){ return document.querySelector(x).value }.toIO() | |
var stripSpaces = function(s){ return s.replace(/\s+/g, ''); } | |
var ex4 = compose(map(stripSpaces), getValue); | |
assertEqual("honkeytonk", runIO(ex4('#text'))) | |
console.log("exercise 4...ok!") | |
// Exercise 5 | |
// ========== | |
// Use getHref() / getProtocal() and runIO() to get the protocal of the page. | |
var getHref = function(){ return location.href; }.toIO(); | |
var getProtocal = compose(_.head, _.split('/')) | |
var ex5 = compose(map(getProtocal), getHref) | |
console.log("--------Start exercise 5--------") | |
assertEqual('http:', runIO(ex5(null))) | |
console.log("exercise 5...ok!") | |
// Exercise 6* | |
// ========== | |
// Write a function that returns the Maybe(email) of the User from getCache(). Don't forget to JSON.parse once it's pulled from the cache so you can _.get() the email | |
// setup... | |
localStorage.user = JSON.stringify({email: "[email protected]"}) | |
var getCache = function(x){ return Maybe(localStorage[x]); }.toIO(); | |
var getStringEmail = compose(_.get('email'), JSON.parse); | |
var ex6 = compose(map(map(getStringEmail)), getCache); | |
assertDeepEqual(Maybe("[email protected]"), runIO(ex6('user'))) | |
console.log("exercise 6...ok!") | |
// TEST HELPERS | |
// ===================== | |
function inspectIt(x){ | |
return (x.inspect && x.inspect()) || (x.toString && x.toString()) || x.valueOf(); //hacky for teachy. | |
} | |
function assertEqual(x,y){ | |
if(x !== y){ throw("expected "+x+" to equal "+y); } | |
} | |
function assertDeepEqual(x,y){ | |
if(x.val !== y.val) throw("expected "+inspectIt(x)+" to equal "+inspectIt(y)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment