Reputation:
My last question here: How to store data of a functional chain of Monoidal List?
had many great responses and one of them suggested to implement some sort of type structure in JavaScript:
const TYPE = Symbol();
const typeOf = t => x => x == null
? x
: Object.assign(x, {
[TYPE]: t
});
const isType = t => x => x == null
? false
: x[TYPE] === t;
const Foo = x => typeOf(Foo)(x);
console.log(
isType(Foo)(1) // false
, isType(Foo)([]) // false
, isType(Foo)({}) // false
, isType(Foo)(x => x) // false
, isType(Foo)(true) // false
, isType(Foo)(undefined) // false
, isType(Foo)(null) // false
);
console.log(
isType(Foo)(Foo(1)) // true
, isType(Foo)(Foo([])) // true
, isType(Foo)(Foo({})) // true
, isType(Foo)(Foo(x => x)) // true
, isType(Foo)(Foo(true)) // true
, isType(Foo)(Foo(undefined)) // false
, isType(Foo)(Foo(null)) // false
);
console.log(Foo(1) + Foo(2)); //3
While I thought this is a great idea, another member suggests this is inconsistent since Object.assign is a mutable operation and it should not be allowed in Functional Programming context.
In response, there is another idea to use Proxy instead, so I have tried to implement the similar system by myself.
The result is unfortunately very poor since Proxy
seems only to accept Object.
var target = {};
var p = new Proxy(target, {});
p.a = 37; // operation forwarded to the target
console.log(target.a); // 37. The operation has been properly forwarded
var target = 5;
var p = new Proxy(target, {});
p.a = 37; // operation forwarded to the target
console.log(target.a); // 37. The operation has been properly forwarded
I also considered to take advantage of Object.create, but does not work in similar manner of Proxy
.
Basically, I recognise this is a challenge to implement Inheritance (object-oriented programming) in functional programming with dynamic typing/ duck typing and reflection.
Therefore, I really want to make this implementation work in ES6 Proxy context.
Any great ideas? Thanks.
Upvotes: 4
Views: 13394
Reputation:
It turns out,
all of them are not necessary to implement Reflection (computer programming) at least for this topic.
See my code below:
const selfAware = i => i[i] = i;
const isAware = i => (i[i] === i);
const I = i => (i === I) || (i == null)
? i
: selfAware(Object(i));
const amI = i => (i === I)
? true
: (i == null)
? false
: isAware(i);
const ss = I(6);
console.log(ss);
console.log(ss[ss]);
//self-similarity
console.log(ss[ss][ss]);
const obj1 = {
a: 2
};
const obj2 = I(obj1);
console.log(obj1);
console.log(obj2);
console.log(
I("Hello world!").toString() //Yes, it works!
);
console.log(I(1) + I(2)); //3
console.log(
(I) //[Function: I]
);
console.log(
(I)(I) //[Function: I]
);
console.log(
(I)(I)(I) //[Function: I]
);
console.log(
(I)(I)(I)(I) //[Function: I]
);
console.log("============================");
console.log(
amI(I) //true
, amI(1) // false
, amI([]) // false
, amI({}) // false
, amI(x => x) // false
, amI(true) // false
, amI(false) // false
, amI(undefined) // false
, amI(null) // false
);
console.log(
amI(I(I)) // true
, amI(I(1)) // true
, amI(I([])) // true
, amI(I({})) // true
, amI(I(x => x)) // true
, amI(I(true)) // true
, amI(I(false)) // true
, amI(I(undefined)) // false
, amI(I(null)) // false
);
Upvotes: 5