3: JavaScript

Programmiersprache für das Web


Auf dieser Seite

    3.1: Funktionen in JavaScript

    identify_function()

        
          
          function identity_function(param) {
            return function() {
              return param;
            }
          }
        
        
      

    addf()

        
          
          function addf(x) {
            return function(y) {
              return x + y;
            }
          }
        
        
      

    applyf()

        
          
          function applyf(fn) {
            return function(x) {
              return function(y) {
                return fn(x,y);
              }
            }
          }
        
        
      

    curry()

        
          
          function curry(fn, first) {
            return function(second) {
              return fn(first, second);
            }
          }
        
        
      

    inc()

        
          
          function inc(x) {
            return addf(x)(1);
          }
        
        
      

    methodize()

        
          
          function methodize(fn) {
            return function(x) {
              return fn(this, x);
            }
          }
        
        
      

    demethodize()

        
          
          function demethodize(fn) {
            return function(x,y) {
              return fn.call(x, y);
            }
          }
        
        
      

    twice()

        
          
          function twice(fn) {
            return function(x) {
              return fn(x,x);
            }
          }
        
        
      

    composeu()

        
          
          function composeu(first, second) {
            return function(x) {
              return second(first(x));
            }
          }
        
        
      

    composeb()

        
          
          function composeb(first, second) {
            return function(x, y, z) {
              return second(first(x, y), z);
            }
          }
        
        
      

    once()

        
          
          function once(fn) {
            this.ran = false;
            const that = this;
            return function(x,y) {
              if (!that.ran) {
                that.ran = true;
                return fn(x,y);
              } else {
                throw new Error("Function already ran!");
              }
            }
          }
        
        
      

    counterf()

        
          
          function counterf(value) {
            const counter = { value };
            counter.inc = function() {
              counter.value ++;
              return counter.value;
            }
            counter.dec = function() {
              counter.value --;
              return counter.value;
            }
    
            return counter;
          }
        
        
      

    revocable()

        
          
          function revocable(fn) {
            const state = { fn };
            state.invoke = function(value) {
              if (state.fn) {
                return fn(value);
              } else {
                throw new Error("No function available!");
              }
            }
            state.revoke = function() {
              state.fn = null;
            }
            return state;
          }
        
        
      

    Array Wrapper

        
          
          function vector() {
            this.vector = [];
            const object = {};
            const that = this;
            object.append = function(value) {
              that.vector.push(value);
            }
            object.store = function(index, value) {
              that.vector.splice(index, 0, value);
              that.vector.join();
            }
            object.get = function(index) {
              return that.vector[index];
            }
            return object;
          }
        
        
      

    3.2: Advanced Functional JavaScript Programming

    Pub/sub object

        
          
          function pubsub() {
            this.subs = [];
            const that = this;
            return {
              subscribe(fn) {
                that.subs.push(fn);
              },
              publish(value) {
                that.subs.forEach(fn => {
                  fn(value);
                });
              }
            }
          }
        
        
      

    gensym()

        
          
          function gensymf(symbol) {
            this.symbol = symbol;
            this.counter = 0;
    
            return () => {
              const output = `${this.symbol}${this.counter}`;
              this.counter++;
              return output;
            }
          }
        
        
      

    fibonacci()

        
          
          function fibonacci(first, second) {
            this.first = first;
            this.second = second;
            this.firstReturned = false;
            this.secondReturned = false;
    
            return () => {
              if (!this.firstReturned) {
                this.firstReturned = true;
                return this.first;
              } else if (!this.secondReturned) {
                this.secondReturned = true;
                return this.second;
              } else {
                const result = this.first + this.second;
                this.first = this.second;
                this.second = result;
                return result;
              }
            }
          }
        
        
      

    addg()

        
          
          function addg(value) {
            function fn(next) {
              if (!next) return value;
    
              value += next;
              return fn;
            }
    
            return fn;
          }
        
        
      

    applyg()

        
          
          function applyg(fn) {
            let store = 0;
            return function (first) {
              store = first;
              function cb(second) {
                if (second) {
                  store = fn(store, second);
                  return cb;
                } else {
                  return store;
                }
              }
              return cb;
            }
          }
        
        
      

    m()

        
          
          function m(value, source) {
            return {
              value,
              source: source || String(value)
            };
          }
        
        
      

    addm()

        
          
          function addm(first, second) {
            return {
              value: first.value + second.value,
              source: `(${first.value}+${second.value})`
            };
          }
        
        
      

    binarymf()

        
          
          function binarymf(fn, sign) {
            return (first, second) => {
              return {
                value: fn(first.value, second.value),
                source: `(${first.value}${sign}${second.value})`
              }
            }
          }
        
        
      

    binarymf2()

        
          
          function binarymf2(fn, sign) {
            return (first, second) => {
              let firstRaw, secondRaw;
              if (typeof first === 'object' && first.hasOwnProperty('value')) {
                firstRaw = first.value;
              } else {
                firstRaw = first;
              }
    
              if (typeof second === 'object' && second.hasOwnProperty('value')) {
                secondRaw = second.value;
              } else {
                secondRaw = second;
              }
    
              return {
                value: fn(firstRaw, secondRaw),
                source: `(${firstRaw}${sign}${secondRaw})`
              }
            }
          }
        
        
      

    unarymf()

        
          
          function unarymf(fn, sign) {
            return (value) => {
              return {
                value: fn(value),
                source: `(${sign} ${value})`
              }
            }
          }
        
        
      

    hyp()

        
          
          function hyp(a, b) {
            return Math.sqrt(a*a + b*b);
          }
        
        
      

    exp()

        
          
          function exp(array) {
            const fn = array[0];
            const first = array[1];
            const second = array[2];
    
            let firstRaw, secondRaw;
    
            if (typeof first === 'number') {
              firstRaw = first;
            } else if (typeof first === 'object' && first.length) {
              firstRaw = exp(first);
            }
    
            if (second) {
              if (typeof second === 'number') {
                secondRaw = second;
              } else if (typeof second === 'object' && second.length) {
                secondRaw = exp(second);
              }
            }
    
            if (secondRaw) {
              return fn(firstRaw, secondRaw);
            } else {
              return fn(firstRaw);
            }
          }
        
        
      

    store()

        
          
          var variable;
          function store(value) {
            variable = value;
          }
        
        
      

    quatre()

        
          
          function quatre(bin, first, second, cb) {
            cb(bin(first, second));
          }
        
        
      

    unaryc()

        
          
          function unaryc(fn) {
            return function(value, cb) {
              cb(fn(value));
            }
          }
        
        
      

    binaryc()

        
          
          function binaryc(binary) {
            return function (first, second, cb) {
              cb(binary(first, second));
            }
          }
        
        
      
    © Jona Ittermann 2022