2

I have following function:

_processSelectedPlantWcOb: function (oSelectOb, oSetNewDates, oWpServiceOb, fnQueryDatesPlantWcOb) {
  let self = this;

  return oSelectOb
    .map(function (oEvent) {
      return oEvent.getSource();
    })
    .switchMap(function (oSelect) {
      return oSetNewDates.mapTo(oSelect);
    })
    .map(function (oSelect) {
      let oItem = oSelect.getSelectedItem();
      let aKeys = oItem.getKey().split("/");
      return {sPlant: aKeys[0], sWc: aKeys[1]};
    })
    .switchMap(function (oSelected) {
      return fnQueryDatesPlantWcOb(oWpServiceOb, oSelected.sPlant, oSelected.sWc);
    });
},

as you can see the last parameter is expect a function and the implementation of the function fnQueryDatesPlantWcOb looks as follow:

_processQueryDatesPlantWcOb: function (oWpServiceOb, sPlant, sWc) {

  return oWpServiceOb
    .switchMap(function (oModel) {
      let oPlantFilter = new sap.ui.model.Filter("Plant", sap.ui.model.FilterOperator.EQ, sPlant);
      let oWcFilter = new sap.ui.model.Filter("WorkCenter", sap.ui.model.FilterOperator.EQ, sWc);

      return Rx.Observable.create(function (subscriber) {
        oModel.read("/CostCenterCalendarSet", {
          success: function (oData, oResponse) {
            subscriber.next(oResponse);
          },
          error: function (oError) {
            subscriber.error(oError);
          },
          filters: [oPlantFilter, oWcFilter]
        });
      });
    })
    .filter(function (oData) {
      return oData.data.results.length > 0
    })
    .mergeMap(function (oData) {
      return Rx.Observable.from(oData.data.results);
    })
    .map(function (oData) {
      let oDate = oData.InspectionDate;
      return new Date(oDate.getFullYear(), oDate.getMonth(), oDate.getDate());
    })
    .filter(function (oDate) {
      let oToday = new Date();
      return oDate.getTime() > oToday.getTime();
    })
    .map(function (oDate) {
      return oDate.getTime();
    });
},

As you can see, the parameters sPlant and sWc will be use in the switchMap function.

What I want to know is, do I break the functional paradigm? In my opinion, I do not break it, because every time when I pass the same sPlant and sWc values, I will get the same result, but I am not sure.

softshipper
  • 26,415
  • 36
  • 123
  • 260

1 Answers1

0

I don't program in JS, but the "functional paradigm" actually defines two properties regarding (pure) functions:

  • It always returns the same result if given the same arguments. This is called referential transparency, and you can add it to your list of $5 programming terms.
  • It can’t cause any side effects. That is, the function can’t make any changes that are observable outside the function itself—for example, by changing an externally accessible mutable object or writing to a file.

Since oModel.read("/CostCenterCalendarSet", ...) seems to do I/O, it's not a pure function (check out this question to see why reading is a side effect, too).

Furthermore, it's a controversial topic if functions using date/time can be referentially transparent at all—especially if it's not part of the parameter list.

Community
  • 1
  • 1
beatngu13
  • 3,672
  • 2
  • 24
  • 51