<commit_before>var clock,
    ballstream;
var clockSub;

var svg;
var w = 800, h = 600;
var balls;
var boundaries;
var collided;

/*
 * Ball
 */
function Ball(x,y,r,a){
  this.x = x;
  this.y = y;
  this.r = r;
  this.acc = a;
}

/*
 * Ground
 */
function Ground(x,y,h,w){
  this.x = x;
  this.y = y;
  this.h = h;
  this.w = w;
}

var collisionDetection = function(){
  for(var i in balls) {
    var ball = balls[i];
    for(var j in boundaries) {
      var boundary = boundaries[j];
      if((ball.y*ball.acc)>boundary.y-ball.r){
        return true;
      }
    }
  }
  return false;
}

var initEnvironment = function(){
  balls = [
    new Ball(w/2,h/8,30,1.03),
    new Ball(w/3,h/8,20,1.03)
  ];
  boundaries = [
    new Ground(0,h-20,20,w)
  ];

  var collided = false;

  svg = d3.select("#environment").insert("svg") .attr("width", w).attr("height", h);
  
  for(var i in boundaries) {
    var boundary = boundaries[i];
    svg.append("rect").attr("x", boundary.x)
                      .attr("y", boundary.y)
                      .attr("height", boundary.h)
                      .attr("width", boundary.w)
                      .attr("class", "boundary")
  }
  for(var i in balls) {
    var ball = balls[i];
    svg.append("circle").attr("r", ball.r)
                        .attr("cx", ball.x)
                        .attr("cy", ball.y)
                        .attr("class", "ball")
  }
}

function initBallStream() {
  ballstream = Rx.Observable.from(svg.selectAll(".ball")[0]).subscribe(
    function (x) {
      var cy = parseInt(x.getAttribute("cy"));
      
      x.setAttribute("cy", cy+1);
      //x.setAttribute("cy", function(d) { return d.y=d.y*d.acc; });
    },
    function (err) {
      console.log('Error: ' + err);
    },
    function () {
      console.log('Completed');
    }
  );
}

function createObservable(element, eventType) {

  return Rx.Observable.create(function (observer) {

    var eventHandler = function (eventObj) {
        observer.onNext(eventObj);
      }
      //adds event listener to the element.
    element.addEventListener(eventType, eventHandler);

    //on dispose
    return function () {
      element.removeEventListener(eventType, eventHandler);
    };
  });
};

function init() {
  initButtons();
  initEnvironment();
  initBallStream();
  clockInit();
}

function initButtons() {
  //button
  createObservable(document.getElementById('subClock'), 'click')
    .subscribe(
      function (evt) {
        subscribeToClock()
      },
      function (err) {
        console.log('error: ' + err);
      },
      function () {
        console.log("complete");
      }
  );

  createObservable(document.getElementById('unsubClock'), 'click')
    .subscribe(
      function (evt) {
        unsubscribeFromClock();
      },
      function (err) {
        console.log('error: ' + err);
      },
      function () {
        console.log("complete");
      }
  );
}

function multiObserverables() {
  // Creates an observable sequence of 5 integers, starting from 1
  var a = Rx.Observable.range(1, 10);
  var b = Rx.Observable.range(5, 10)

  var observer = Rx.Observer.create(
    function (x) {
      console.log('onNext: %s', x);
    },
    function (e) {
      console.log('onError: %s', e);
    },
    function () {
      console.log('onCompleted');
    }
  );

  var x = Rx.Observable.zip(a, b, function (a1, b1) {
      return a1 * b1;
    })
    .subscribe(observer);
}

function clockInit() {
  source = Rx.Observable.timer(
    0, /* 0 seconds */
    10 /* 250 ms */
  )
  clock = source.publish();
  clock.connect();
}

function unsubscribeFromClock() {
  clockSub.dispose();
}

function subscribeToClock() {
  clockSub = clock.subscribe(
    function (x) {
      if(!collisionDetection()){
        initBallStream();
      }
    },
    function (e) {
      console.log('onError: %s', e);
    },
    function () {
      console.log('onCompleted');
    }
  );
}

$(document).ready(init);
<commit_msg>Integrated the scan function<commit_after>var clock,
  ballstream;
var clockSub;

var svg;
var w = 800,
  h = 600;
var balls;
var boundaries;
var collided;

/*
 * Ball
 */
function Ball(x, y, r, a) {
  this.x = x;
  this.y = y;
  this.r = r;
  this.acc = a;
}

/*
 * Ground
 */
function Ground(x, y, h, w) {
  this.x = x;
  this.y = y;
  this.h = h;
  this.w = w;
}

var collisionDetection = function () {
  for (var i in balls) {
    var ball = balls[i];
    for (var j in boundaries) {
      var boundary = boundaries[j];
      if ((ball.y * ball.acc) > boundary.y - ball.r) {
        return true;
      }
    }
  }
  return false;
}

var initEnvironment = function () {
  balls = [
    new Ball(w / 2, h / 8, 30, 1.03),
    new Ball(w / 3, h / 8, 20, 1.03)
  ];
  boundaries = [
    new Ground(0, h - 20, 20, w)
  ];

  var collided = false;

  svg = d3.select("#environment").insert("svg").attr("width", w).attr("height", h);

  for (var i in boundaries) {
    var boundary = boundaries[i];
    svg.append("rect").attr("x", boundary.x)
      .attr("y", boundary.y)
      .attr("height", boundary.h)
      .attr("width", boundary.w)
      .attr("class", "boundary")
  }
  for (var i in balls) {
    var ball = balls[i];
    svg.append("circle").attr("r", ball.r)
      .attr("cx", ball.x)
      .attr("cy", ball.y)
      .attr("class", "ball")
  }
}

function initBallStream() {
  ballstream = Rx.Observable.from(svg.selectAll(".ball")[0]);
  var ballSub = ballstream.subscribe(
    function (x) {
      var cy = parseInt(x.getAttribute("cy"));

      x.setAttribute("cy", cy + 1);
      //x.setAttribute("cy", function(d) { return d.y=d.y*d.acc; });
    },
    function (err) {
      console.log('Error: ' + err);
    },
    function () {
      console.log('Completed');
    }
  );
}

function createObservable(element, eventType) {

  return Rx.Observable.create(function (observer) {

    var eventHandler = function (eventObj) {
        observer.onNext(eventObj);
      }
      //adds event listener to the element.
    element.addEventListener(eventType, eventHandler);

    //on dispose
    return function () {
      element.removeEventListener(eventType, eventHandler);
    };
  });
};

function init() {
  initButtons();
  // initEnvironment();
  // initBallStream();
  clockInit();
  clock
    .scan(initState(), function(state,time) { return state.update(time) })
    .subscribe(
      function (x) {
        console.log("Current Time: " + x.time);
      },
      function (e) {
        console.log('onError: %s', e);
      },
      function () {
        console.log('onCompleted');
      }
    );
}

function initButtons() {
  //button
  createObservable(document.getElementById('subClock'), 'click')
    .subscribe(
      function (evt) {
        subscribeToClock()
      },
      function (err) {
        console.log('error: ' + err);
      },
      function () {
        console.log("complete");
      }
  );

  createObservable(document.getElementById('unsubClock'), 'click')
    .subscribe(
      function (evt) {
        unsubscribeFromClock();
      },
      function (err) {
        console.log('error: ' + err);
      },
      function () {
        console.log("complete");
      }
  );
}

function initState() {
  var state = {
    circles: [
      new Ball(w / 2, h / 8, 30, 1.03),
      new Ball(w / 3, h / 8, 20, 1.03)
    ],
    time : 0,
    update : function(time) {
      this.time += 1;
      return this;
    }
  }
  return state;
}

function multiObserverables() {
  // Creates an observable sequence of 5 integers, starting from 1
  var a = Rx.Observable.range(1, 10);
  var b = Rx.Observable.range(5, 10)

  var observer = Rx.Observer.create(
    function (x) {
      console.log('onNext: %s', x);
    },
    function (e) {
      console.log('onError: %s', e);
    },
    function () {
      console.log('onCompleted');
    }
  );

  var x = Rx.Observable.zip(a, b, function (a1, b1) {
      return a1 * b1;
    })
    .subscribe(observer);
}

function clockInit() {
  clock = Rx.Observable.timer(
    0, /* 0 seconds */
    1000 /* 250 ms */
  )
  // clock = source.publish();
  // clock.connect();
}

function unsubscribeFromClock() {
  clockSub.dispose();
}

function subscribeToClock() {
  clockSub = clock.subscribe(
    function (x) {
      if (!collisionDetection()) {
        initBallStream();
      }
    },
    function (e) {
      console.log('onError: %s', e);
    },
    function () {
      console.log('onCompleted');
    }
  );
}

$(document).ready(init);
{'fraction_non_alphanumeric': 0.1694615021256495, 'fraction_numerical': 0.013226263580538498, 'mean_word_length': 2.263583815028902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2506645', 'n_tokens_mistral': 2989, 'n_tokens_neox': 2866, 'n_words': 622}