# Function pattern

- pattern(struct): ((arg0: any) => any)

- Sorta like Pattern Matching (a functional programming conditional construct)

  See http://c2.com/cgi/wiki?PatternMatching

  This is a conditional construct which allows a series of predicates and output functions to be checked and then applied. Each predicate receives the input. If the predicate returns truthy, then its matching output function (mapping function) is provided with the input and, then the result is returned.

  Each combination (2-tuple) of predicate + output function should be placed in an array of size 2: [ predicate, mapFn ]

  These 2-tuples should be put in an outer array.

  #### Parameters

  - struct: any

    A 2D array. Each element of the array should be an array, a 2-tuple, with a Predicate and a mapping/output function

  #### Returns ((arg0: any) => any)

  - - (arg0): any

    - #### Parameters

      - arg0: any

      #### Returns any

  #### Example

      // Here's a 2-tuple where the first element is the isString predicate
      // and the second element is a function that returns a description of the input
      let firstTuple = [ angular.isString, (input) => `Heres your string ${input}` ];

      // Second tuple: predicate "isNumber", mapfn returns a description
      let secondTuple = [ angular.isNumber, (input) => `(${input}) That's a number!` ];

      let third = [ (input) => input === null,  (input) => `Oh, null...` ];

      let fourth = [ (input) => input === undefined,  (input) => `notdefined` ];

      let descriptionOf = pattern([ firstTuple, secondTuple, third, fourth ]);

      console.log(descriptionOf(undefined)); // 'notdefined'
      console.log(descriptionOf(55)); // '(55) That's a number!'
      console.log(descriptionOf("foo")); // 'Here's your string foo'

  - Defined in shared/hof.js:161