Skip to content

Instantly share code, notes, and snippets.

@ryyppy
Last active June 3, 2019 18:33
Show Gist options
  • Save ryyppy/a63fd2ad65619bd87b064622e269da41 to your computer and use it in GitHub Desktop.
Save ryyppy/a63fd2ad65619bd87b064622e269da41 to your computer and use it in GitHub Desktop.

Revisions

  1. ryyppy revised this gist May 31, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions 0_functor.re
    Original file line number Diff line number Diff line change
    @@ -27,9 +27,9 @@ module Make =

    /*
    Here we are creating a module which is constraint to the rules of our Functor parameter `Component`.
    - Comp defines a `type t`, this time we are concrete and define `t` as a `string`.
    - StringComponent defines a `type t`, this time we are concrete and define `t` as a `string`.
    - It defines a `render(t): string` function to satisfy the Functor interface
    - We also define a `make()` function, which will create us a new instance of `Comp.t`
    - We also define a `make()` function, which will create us a new instance of `StringComponent.t`
    This is a great way for encapsulation btw :-)
    */

    @@ -45,7 +45,7 @@ module StringComponent = {

    /*
    Now with everything in place, let's create a new module called `ComponentHandler) with our `Make` functor
    by providing our Comp module as a parameter.
    by providing our StringComponent module as a parameter.
    It looks like a function call, in fact that's all there is. It is a function which
    creates a new module in build time!
  2. ryyppy revised this gist May 31, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 0_functor.re
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ StringComponent.make() |> ComponentHandler.handleComponent |> Js.log;
    That's it. Have fun with functors, y'all!
    Link to Reason Try example:
    https://reasonml.github.io/en/try?rrjsx=true&reason=PQKgUABAMgpgLgcgM4QIJKQVwLYwgdz3wEMA7OCOAewgAtiA3PYiAMU1IGNqAnA2gJadaETjxjE4MFC1Ix8EALJUAJpgA2eAGY8q2SCyQAHGJwFahEAMJ6jVOeSWqY6yJAAqtPOy68I4sxgmGVIqOC8+ZTVNCFJiXBUIAAMbbDsHOCSAGn4hETkYFRRqCCRJASQtAE9RGB44YgFSUXskOB5G8iQALkgAWmtbexhHcQBHTAFxEIhiAC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J61KKRUNqZdtQr+jCSrCIujJSHNgtG3gtZzJMlaUN8MDMcOo6J4MEvmlLUF1HBSswBskr419IkU+AS7uBngYc8QNQpg6WJy-rXp0gye3MonWdLh1PImaQpB6wrEBa6RZuKpFveCtmSQV6V8JfWCC2RvaxoBUlY15IuSCmOIcBpmHSSpWCAWQZPxAYDWFC1sZSTqkXa-h5VpDZ2Ay-El9FoDWXf8AAfF8X1C6aB4BaF+0FnKOBARAIiFoSLwhPEAA
    https://bit.ly/2wrU1e6
    */


  3. ryyppy revised this gist May 31, 2019. 3 changed files with 65 additions and 12 deletions.
    57 changes: 53 additions & 4 deletions 0_functor.re
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,15 @@
    /*
    Let's Assume we want to have a Functor which creates a new Module from
    a specific Component Model
    The Functor recieves another Module named `Component`, which needs to satisfy certain constraints:
    - Component requires an abstract type t (no matter what concrete type)
    - It also needs to implement a `render` function which gets said type t and returns a `string`
    Also, the newly created module will then contain a function `doSomething`, which will handle any
    instance of `Component.t`. So our `Component` also will need a function to create an instance for that,
    similarly what a `new MyConstructor()` would do in Object Oriented languages.
    */
    module Make =
    (
    Component: {
    @@ -7,12 +19,21 @@ module Make =
    }
    ) => {

    let doSomething = (something: Component.t) => {
    let handleComponent = (something: Component.t): string => {
    Js.log("Handled a component!");
    Component.render(something);
    };
    };

    module Comp = {
    /*
    Here we are creating a module which is constraint to the rules of our Functor parameter `Component`.
    - Comp defines a `type t`, this time we are concrete and define `t` as a `string`.
    - It defines a `render(t): string` function to satisfy the Functor interface
    - We also define a `make()` function, which will create us a new instance of `Comp.t`
    This is a great way for encapsulation btw :-)
    */

    module StringComponent = {
    type t = string;

    let make = () => {
    @@ -22,6 +43,34 @@ module Comp = {
    let render = (test: string): string => test;
    };

    module Foo = Make(Comp);
    /*
    Now with everything in place, let's create a new module called `ComponentHandler) with our `Make` functor
    by providing our Comp module as a parameter.
    It looks like a function call, in fact that's all there is. It is a function which
    creates a new module in build time!
    */
    module ComponentHandler = Make(StringComponent);

    /*
    Time to play around with our newly created module!
    When running our program, we create a new `StringComponent` instance of type `t`, which
    secretly is a `string` type and pass it in the `handleComponent` function, which will
    log `"Handled a component!"` and later own return the result of the `StringComponent.render`
    function.
    */
    StringComponent.make() |> ComponentHandler.handleComponent |> Js.log;

    Comp.make() |> Foo.doSomething;
    /*
    It's very interesting to see how BuckleScript compiles Functors into JavaScript code.
    `Make` is now factory function, which will return as a new module, which is represented
    as a linked list / nested arrays, a similar way on how BS handles immutable records.
    That's it. Have fun with functors, y'all!
    Link to Reason Try example:
    https://reasonml.github.io/en/try?rrjsx=true&reason=PQKgUABAMgpgLgcgM4QIJKQVwLYwgdz3wEMA7OCOAewgAtiA3PYiAMU1IGNqAnA2gJadaETjxjE4MFC1Ix8EALJUAJpgA2eAGY8q2SCyQAHGJwFahEAMJ6jVOeSWqY6yJAAqtPOy68I4sxgmGVIqOC8+ZTVNCFJiXBUIAAMbbDsHOCSAGn4hETkYFRRqCCRJASQtAE9RGB44YgFSUXskOB5G8iQALkgAWmtbexhHcQBHTAFxEIhiACM2ju5KKpNKCAAKUIhsSSk+fHoKTnsxeDw4VZgASn6IAEkKYnUkGgKiyhoBNM1cRxYkuJSCo6kkIFoONwBPZcsIIABzeAoMoCRKXNZPYH+eCYHikGTJRZNeFJNxoF5UHLhPByfDqGpnSSFHaqDREATqdSULzNE7kTqzcGQuDQ5pJFRUADKengglIJJyhzyBA5XPowJiZCqkCabTInDwVC0yVS6RGcAAdJkLRBpRAqLiTUMMmDnq8VZzYjBmSwIb5RZ9ROImbNmrqGlxtFQ+OFJFlIEhvhziDx6fxJIKkrSlFUbPj2phuNGNtcwfgHepEhKIE0IAB5OYAK1MFDrPAE5uZ6jI8MwxERSAtYBAwDA2FZMUUxAA1ngALyQCBL5cbRfL9em4bkboQADea-X6-RFwA3GAD4el5oKECQTwdxQ5wA+UrtYlny-LgC+F6X1wgz57r+y7AVe8B0GQKiaJuGQAZsry4OExI7jB5pWtcO5EvKAEvvun7rgAUoO6hUPCGwAEQABKQZoiQsCcaRbnAACE5HXB++FLqh5AWredQbAhsrEuxoEQF+Z7ieeoCQJRdREMw4hBhIIrYSw47REQghwhULT5h0TQUCU1L+GyKBGvajo+EWfBGCm8TnHwKTOuaSRDhAAymhAIIWHIBJJMelDZNyOkirgBAKXgfJnFIoZVjAPl4P5rp+VhJJuQMjxefFTTSJmfE8BscAYa+7bymCfpQjCJRlCKlQ1MZVl+AZdRaMQBp3AA6swFJZQlma7LOJblcKoqKlpIj4KqSkhpgBLZuG+qGsaTlpFapLLp4Ok6Sw8LBhQJA1Fo0YQCMnDEEYWDdiKMJzHACjdH0twjue6lsrab7ytxj5AUuAXfalHGLteOwzvOmz-oBeHLlRLgkRAHXRpW5EcZJS5A+B+VwYV0hwJhH3wsVqU4ZQOMSWeYDSUuAByVAKJN4QnUwPBVEh2G1kY3YGjk17INNMWyPILIaaIzy0U6jEZNRGp1P+9MiA6jlTrOw2+NGi5zDURi6AwqLEhZfCea9moErZHSIXUbkQIumUkVQ04oOoAizoKFXXbyos5LWrXLLGiAyJ61KKRUNqZdtQr+jCSrCIujJSHNgtG3gtZzJMlaUN8MDMcOo6J4MEvmlLUF1HBSswBskr419IkU+AS7uBngYc8QNQpg6WJy-rXp0gye3MonWdLh1PImaQpB6wrEBa6RZuKpFveCtmSQV6V8JfWCC2RvaxoBUlY15IuSCmOIcBpmHSSpWCAWQZPxAYDWFC1sZSTqkXa-h5VpDZ2Ay-El9FoDWXf8AAfF8X1C6aB4BaF+0FnKOBARAIiFoSLwhPEAA
    */


    */
    18 changes: 10 additions & 8 deletions 1_functor.bs.js
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,11 @@
    var Curry = require("./stdlib/curry.js");

    function Make(Component) {
    var doSomething = function (something) {
    var handleComponent = function (something) {
    console.log("Handled a component!");
    return Curry._1(Component[/* render */0], something);
    };
    return /* module */[/* doSomething */doSomething];
    return /* module */[/* handleComponent */handleComponent];
    }

    function make(param) {
    @@ -18,20 +19,21 @@ function render(test) {
    return test;
    }

    var Comp = /* module */[
    var StringComponent = /* module */[
    /* make */make,
    /* render */render
    ];

    function doSomething(something) {
    function handleComponent(something) {
    console.log("Handled a component!");
    return something;
    }

    var Foo = /* module */[/* doSomething */doSomething];
    var ComponentHandler = /* module */[/* handleComponent */handleComponent];

    render("Hello World");
    console.log(handleComponent("Hello World"));

    exports.Make = Make;
    exports.Comp = Comp;
    exports.Foo = Foo;
    exports.StringComponent = StringComponent;
    exports.ComponentHandler = ComponentHandler;
    /* Not a pure module */
    2 changes: 2 additions & 0 deletions 2_output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    "Handled a component!"
    "Hello World"
  4. ryyppy revised this gist May 31, 2019. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. ryyppy created this gist May 31, 2019.
    37 changes: 37 additions & 0 deletions functor.bs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // Generated by BUCKLESCRIPT VERSION 5.0.4, PLEASE EDIT WITH CARE
    'use strict';

    var Curry = require("./stdlib/curry.js");

    function Make(Component) {
    var doSomething = function (something) {
    return Curry._1(Component[/* render */0], something);
    };
    return /* module */[/* doSomething */doSomething];
    }

    function make(param) {
    return "Hello World";
    }

    function render(test) {
    return test;
    }

    var Comp = /* module */[
    /* make */make,
    /* render */render
    ];

    function doSomething(something) {
    return something;
    }

    var Foo = /* module */[/* doSomething */doSomething];

    render("Hello World");

    exports.Make = Make;
    exports.Comp = Comp;
    exports.Foo = Foo;
    /* Not a pure module */
    27 changes: 27 additions & 0 deletions functor.re
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    module Make =
    (
    Component: {
    type t;

    let render: t => string;
    }
    ) => {

    let doSomething = (something: Component.t) => {
    Component.render(something);
    };
    };

    module Comp = {
    type t = string;

    let make = () => {
    "Hello World";
    };

    let render = (test: string): string => test;
    };

    module Foo = Make(Comp);

    Comp.make() |> Foo.doSomething;