Skip to content

Instantly share code, notes, and snippets.

Created May 28, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/558b760efaeb9e1e7a32419d27027b32 to your computer and use it in GitHub Desktop.
Save anonymous/558b760efaeb9e1e7a32419d27027b32 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/vujowamiyo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://rawgit.com/baconjs/bacon.js/master/dist/Bacon.js"></script>
</head>
<body>
<label>A: <input id="a"></input></label>
<label>B: <input id="b"></input></label>
<div>Sum: <span id="sum"></span></div>
<div>Product: <span id="product"></span></div>
<script id="jsbin-javascript">
var a = valueStream($("#a"));
var b = valueStream($("#b"));
a.log("A:");
b.log("B:");
// map the streams to more sensible number values
a.map(str2Number);
b.map(str2Number);
// combine the streams to create sum
var sum = a.combine(b.plus);
// use onValue to show sum
sum.onValue(showSum);
// combine the streams to create product and show
// it in the correct span
function valueStream(element) {
return element
.asEventStream("keyup")
.map(".currentTarget.value");
}
function str2Number(str) {
return Number(str);
}
function showSum(result) {
$("#sum").text(result);
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">var a = valueStream($("#a"));
var b = valueStream($("#b"));
a.log("A:");
b.log("B:");
// map the streams to more sensible number values
a.map(str2Number);
b.map(str2Number);
// combine the streams to create sum
var sum = a.combine(b.plus);
// use onValue to show sum
sum.onValue(showSum);
// combine the streams to create product and show
// it in the correct span
function valueStream(element) {
return element
.asEventStream("keyup")
.map(".currentTarget.value");
}
function str2Number(str) {
return Number(str);
}
function showSum(result) {
$("#sum").text(result);
}</script></body>
</html>
var a = valueStream($("#a"));
var b = valueStream($("#b"));
a.log("A:");
b.log("B:");
// map the streams to more sensible number values
a.map(str2Number);
b.map(str2Number);
// combine the streams to create sum
var sum = a.combine(b.plus);
// use onValue to show sum
sum.onValue(showSum);
// combine the streams to create product and show
// it in the correct span
function valueStream(element) {
return element
.asEventStream("keyup")
.map(".currentTarget.value");
}
function str2Number(str) {
return Number(str);
}
function showSum(result) {
$("#sum").text(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment