Box

Verpacken -> Verarbeiten -> Auspacken

Beschreibung

Das Box Konstrukt erleichtert das Verarbeiten von beliebigen Werten. Die Werte werden in eine "Box" eingepackt und danach gemapped (weiterverarbeitet). Dabei entsteht eine Art linearer Datenfluss, der die Leserlichkeit des Codes erhöht. Ausserdem werden keine Variablen-Deklarationen für die Zwischenstände benötigt, weil das Resultat der Verarbeitung direkt in die nächste Funktion weitergeleitet wird.

Mit dem Box Konstrukt kann eine Art Pipeline aufgebaut werden, bei dem ein Wert durch diese Pipeline geschickt wird und bei jedem fmap wird der Wert weiter prozessiert. Um am Schluss an den verarbeiteten Wert zu kommen wird die letzte Prozessierung nicht mit fmap sondern mit fold durchgeführt.

Beispiel Anwendung

Code ohne Verwendung von Box

const p = {firstName: "Lukas", lastName: "Mueller"};

const addSalutation = fullName => male => (male ? "Mr. " : "Mrs. ") + fullName;

const fullName           = p.firstName + " " + p.lastName;
const fullNameUpperCase  = fullName.toUpperCase();
const nameWithSalutation = addSalutation(fullNameUpperCase)(true); // Mr. LUKAS MUELLER

Code mit Verwendung von Box

const p = {firstName: "Lukas", lastName: "Mueller"};

const addSalutation = fullName => male => (male ? "Mr. " : "Mrs. ") + fullName;

const nameWithSalutation = Box(p)
                             (fmap)(p => p.firstName + " " + p.lastName)
                             (fmap)(fullName => fullName.toUpperCase())
                             (fold)(fullNameUpperCase => addSalutation(fullNameUpperCase)(true)); // Mr. LUKAS MUELLER

Verwendung

Die Funktion Box wird verwendet um einen beliebigen Wert in eine "Box" zu verpacken.

// Implementation
const fmap  = x => f => g => g(f(x));  

const Box   = x => fmap(x)(id);        // Box.of

// Anwendung
Box(10);                 // { 10 }
Box("Hello World");      // { "Hello World" }
Box(p);                  // { p }

Die Funktion fmap wird verwendet um den Inhalt einer Box zu verarbeiten (mappen). Diese fmapFunktionsaufrufe können beliebig oft hintereinander angewendet werden (chainning von Funktionen). Durch das "chainning" wird eine Art Pipeline aufgebaut.

// Implementation
const fmap = x => f => g => g(f(x));

// Anwendung
Box(5)                                 // { 5 }
 (fmap)(n => n * 10)                   // { 50 }
 (fmap)(n => n + 15)                   // { 65 }
 (fmap)(n => String.fromCharCode(n));  // { 'A' }

Die Funktion fold wird verwendet um einen Wert in der "Box" zu mappen und anschliessend zu extrahieren (den Inhalt aus der Box auszupacken).

// Implementation
const fold  = x => f => f(x);

// Anwendung
Box(5)                                 // { 5 }
 (fmap)(n => n * 10)                   // { 50 }
 (fmap)(n => n + 15)                   // { 65 }
 (fold)(n => String.fromCharCode(n));  // 'A'

Die Funktion chain wird verwendet um ein flatMap durchzuführen. Wenn eine Map-Funktion eine Box erstellt, würde mit fmap eine Box in einer Box entstehen. Um diese extra Box zu entfernen bzw. das gemappte Ergebnis abzuflachen gibt es die Methode chain. Dadurch können auch geschachtelte Box Aufrufe stattfinden.

// Implementation
const chain = x => f => g => g((f(x)(id)));

// Anwendung
Box(5)                                     // { 5 }
 (fmap)(num => num + 5)                    // { 10 }
 (chain)(num => Box(num * 2)
                 (fmap)(num => num + 1))   // { 21 }
 (chain)(num => Box(num * 3)
                 (fmap)(num => num + 1))   // { 64 }

Die Funktion getContent wird verwendet um den Inhalt einer "Box" zu entpacken.

// Implementation
const getContent = b => b(id);

// Anwendung
const p = { firstName: "Tyrion", lastName: "Lannister" };

const box1 = Box(p);

const mapped1 = box1(fmap)(p => p.firstName);

const mapped2 = mapped1(fmap)(firstName => firstName.toUpperCase());

getContent( box1    )   // { firstName: "Tyrion", lastName: "Lannister" } 
getContent( mapped1 )   // "Tyrion"
getContent( mapped2 )   // "TYRION"

Die Funktion app wird verwendet um eine eingepackte Funktion (Funktion in einer Box) auf einen eingepackten Wert anzuwenden.

// Implementation
const app = x => f => g => g(f(fmap)(x)(id));

// Anwendung
Box(x => x + 5)          // { 10 + 5 }
 (app)( Box(10) );       // { 10 }
 
Box( x => y => x + y)    // { 10 + 24 }
 (app)( Box(10) )        // { 10 }
 (app)( Box(14) );       // { 24 }

Die Funktion liftA2 wird verwendet um eine Funktion auf zweit eingepackte Werte anzuwenden.

// Implementation
const liftA2 = f => fx => fy => fx(fmap)(f)(app)(fy);

// Anwendung
liftA2(name1 => name2 => name1 + " " + name2)  // { "Tyrion Lannister" }
 ( Box("Tyrion"   ) )
 ( Box("Lannister") );

Helferfunktion

Die Funktion debug ist eine Helferfunktion, die für debug Zwecke da ist. Die Funktion hilft dem Anwender die Zwischenresultate zu untersuchen in einer Pipeline.

// Implementation
const debug = x => {
    console.log(x);
    return x;
}

// Anwendung
Box(10)
 (fmap)(debug)        // Ausgabe auf der Konsole: 10
 (fmap)(n => n + 2)
 (fold)(debug);       // Ausgabe auf der Konsole: 12

Box Featurings

const Stream = (...elements) => Box(convertArrayToStack(elements));

Stream(1,2,3,4)
 (fmap)( map(x => x * 2)    )
 (fold)( filter(x => x > 4) )

// Synchron
Box( HttpGetSync( URL ) )
 (fmap)( JSON.parse   )
 (fold)( x => x.value )
 
 
// Asynchron
HttpGet( URL )(resp => Box(resp)
                        (fmap)(JSON.parse   )
                        (fold)(x => x.value ))

Box mit Maybe

Um die die Box Konstruktion mit Maybe Werten zu verwenden, gibt es spezielle Funktion, die das verarbeiten von Maybe Types erleichtern. Somit wird das prozessieren mit dem Maybe Type vereinfacht und die Maybe Types können verknüpft werden.

Die Funktion fmapMaybe entspricht der Funktion fmap für einen Maybe Type.

// Implementation
const fmapMaybe = x => f => g => g(mapMaybe(x)(f));

// Anwendung
 const maybePerson = () => Just( {firstName: "Tyrion", lastName: "Lannister"} );
 
 const maybeFirstName = obj =>
      obj && obj.hasOwnProperty('firstName')
          ? Just(obj.firstName)
          : Nothing;
          
 Box( maybePerson() )                                  // { Just({firstName: "Tyrion", lastName: "Lannister"}) }
  (chainMaybe)( maybeFirstName )                       // { Just("Tyrion") }
  (foldMaybe)( firstName => firstName.toUpperCase() )  //   Just("TYRION")

Die Funktion foldMaybe entspricht der Funktion fold für einen Maybe Type

// Implementation
const foldMaybe = mapMaybe;

// Anwendung
Box( Just(10) )                   // { Just(10) }
 (fmapMaybe)(x => x + 10)         // { Just(20) }
 (foldMaybe)(num => num + '$')    // Just("20$")

Die Funktion chainMaybe entspricht der Funktion chain für einen Maybe Type.

// Implementation
const chainMaybe    = x => f => g => g(flatMapMaybe(x)(f));

// Anwendung
const maybePerson = () => Just({firstName: "Tyrion", lastName: "Lannister"});

const maybeFirstName = obj =>
        obj && obj.hasOwnProperty('firstName')
            ? Just(obj.firstName)
            : Nothing;

Box( maybePerson() )                                  // { Just({firstName: "Tyrion", lastName: "Lannister"}) 
 (chainMaybe)( maybeFirstName )                       // { Just("Tyrion") }
 (foldMaybe)( firstName => firstName.toUpperCase() )  //   Just("TYRION")

Die Funktion appMaybe entspricht der Funktion app für einen Maybe Type.

// Implementation
const apMaybe = x => f => g => g(flatMapMaybe(x)(func => mapMaybe(f)(func)));

// Anwendung
Box( Just(x => x + 5) )          // { Just(15 + 5) }
 (appMaybe)( Just(10) );         // { Just(10) }

Die Funktion liftA2Maybe entspricht der Funktion liftA2 für einen Maybe Type.

// Implementation
const liftA2Maybe = f => fx => fy =>
    Box(fx)
     (fmapMaybe)(f)
     (appMaybe)(fy);
        
// Anwendung
liftA2Maybe( x => y => x + y )  // (10 + 5)
 ( Just(10) )
 ( Just(5)  );

Last updated

Was this helpful?