<commit_before>/*
 * milktea : lib/function.js
 * copyright (c) 2015 Susisu
 */

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "__id__"          : __id__,
        "__const__"       : __const__,
        "__compose__"     : __compose__,
        "__flip__"        : __flip__,
        "__apply__"       : __apply__,
        "__reverseApply__": __reverseApply__
    });
}

var __id__ =
    new Value(
        DataType.FUNCTION,
        function (x) {
            return x;
        }
    );

var __const__ =
    new Value(
        DataType.FUNCTION,
        function (x) {
            return new Value(
                DataType.FUNCTION,
                function (y) {
                    return x;
                }
            );
        }
    );

var __compose__ =
    new Value(
        DataType.FUNCTION,
        function (g) {
            utils.assertType(g, DataType.FUNCTION);
            return new Value(
                DataType.FUNCTION,
                function (f) {
                    utils.assertType(f, DataType.FUNCTION);
                    return new Value(
                        DataType.FUNCTION,
                        function (x) {
                            return g.data(calcTailCall(f.data(x)));
                        }
                    );
                }
            );
        }
    );

var __flip__ =
    new Value(
        DataType.FUNCTION,
        function (f) {
            utils.assertType(f, DataType.FUNCTION);
            return new Value(
                DataType.FUNCTION,
                function (x) {
                    return new Value(
                        DataType.FUNCTION,
                        function (y) {
                            var g = calcTailCall(f.data(y));
                            utils.assertType(g, DataType.FUNCTION);
                            return g.data(x);
                        }
                    );
                }
            );
        }
    );

var __apply__ =
    new Value(
        DataType.FUNCTION,
        function (f) {
            utils.assertType(f, DataType.FUNCTION);
            return new Value(
                DataType.FUNCTION,
                function (x) {
                    return f.data(x);
                }
            );
        }
    );

var __reverseApply__ =
    new Value(
        DataType.FUNCTION,
        function (x) {
            return new Value(
                DataType.FUNCTION,
                function (f) {
                    utils.assertType(f, DataType.FUNCTION);
                    return f.data(x);
                }
            );
        }
    );

end_module();<commit_msg>Add Function class<commit_after>/*
 * milktea : lib/function.js
 * copyright (c) 2015 Susisu
 */

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "__Function__proto__": __Function__proto__,
        "__Function__"       : __Function__,
        "__compose__"        : __compose__,
        "__flip__"           : __flip__,
        "__apply__"          : __apply__,
        "__reverseApply__"   : __reverseApply__
    });
}

var core         = require("../core.js"),
    Value        = core.Value,
    DataType     = core.DataType,
    calcTailCall = core.calcTailCall;

var utils         = require("./utils.js"),
    assertType    = utils.assertType,
    createObject  = utils.createObject,
    readProperty  = utils.readProperty,
    writeProperty = utils.writeProperty;

var __object__ = require("./object.js");

/**
 * Prototype of Function class.
 */
var __Function__proto__ = createObject(__object__.__Object__proto__);
__Function__proto__.data["toString"] =
    new Value(
        DataType.FUNCTION,
        function (obj) {
            assertType(obj, DataType.OBJECT);
            var func = readProperty(obj, "value");
            assertType(func, DataType.FUNCTION);
            return new Value(
                DataType.STRING,
                "<function>"
            );
        }
    );

/**
 * Function class.
 */
var __Function__ = createObject(__object__.__Class__proto__);
__Function__.data["proto"] = __Function__proto__;
__Function__.data["ctor"] =
    new Value(
        DataType.FUNCTION,
        function (obj) {
            assertType(obj, DataType.OBJECT);
            return new Value(
                DataType.FUNCTION,
                function (value) {
                    assertType(value, DataType.FUNCTION);
                    writeProperty(obj, "value", value);
                    return obj;
                }
            );
        }
    );

/**
 * Function composition.
 * <pre> compose g f x = g (f x)</pre>
 */
var __compose__ =
    new Value(
        DataType.FUNCTION,
        function (g) {
            assertType(g, DataType.FUNCTION);
            return new Value(
                DataType.FUNCTION,
                function (f) {
                    assertType(f, DataType.FUNCTION);
                    return new Value(
                        DataType.FUNCTION,
                        function (x) {
                            return g.data(calcTailCall(f.data(x)));
                        }
                    );
                }
            );
        }
    );

/**
 * Flips two arguments.
 * <pre>flip f x y = f y x</pre>
 */
var __flip__ =
    new Value(
        DataType.FUNCTION,
        function (f) {
            assertType(f, DataType.FUNCTION);
            return new Value(
                DataType.FUNCTION,
                function (x) {
                    return new Value(
                        DataType.FUNCTION,
                        function (y) {
                            var g = calcTailCall(f.data(y));
                            assertType(g, DataType.FUNCTION);
                            return g.data(x);
                        }
                    );
                }
            );
        }
    );

/**
 * Application.
 * <pre>apply f x = f x</pre>
 */
var __apply__ =
    new Value(
        DataType.FUNCTION,
        function (f) {
            assertType(f, DataType.FUNCTION);
            return new Value(
                DataType.FUNCTION,
                function (x) {
                    return f.data(x);
                }
            );
        }
    );

/**
 * Reverse application.
 * <pre>reverseApply x f = f x</pre>
 */
var __reverseApply__ =
    new Value(
        DataType.FUNCTION,
        function (x) {
            return new Value(
                DataType.FUNCTION,
                function (f) {
                    assertType(f, DataType.FUNCTION);
                    return f.data(x);
                }
            );
        }
    );

end_module();
{'fraction_non_alphanumeric': 0.10087060942659862, 'fraction_numerical': 0.0012008405884118883, 'mean_word_length': 1.1342088404868673, 'pattern_counts': {'":': 2, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2086998', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1737, 'n_words': 361}