The explanation in https://github.com/hemanth/functional-programming-jargon is unfortunately not very accurate.
A pointed functor is really a functor F
together with a function of
defined for every type a
, and sending a value x
of type a
into a value of(x)
of type F a
. In Hindley-Milner signature it looks like this:
of :: a -> F a
For instance, the Array functor is pointed with of = x => [x]
, defined for every value x
of any type a
.
Further, the function of
(or more precisely, the collection of functions of
as you have one for each type a
) must be a natural transformation from the identity functor into F
. Which means that of
applied to a function's value equals of
applied to the function's argument and then mapped over the function:
of(f(x)) === of(x).map(f)
For instance, in the Array example you have
[f(x)] === [x].map(f),
so x => [x]
is indeed a natural transformation.
But you can also redefine of
as
of = x => [x, x]
[f(x), f(x)] === [x, x].map(f)
which makes Array
into another pointed functor, even if the map
method remains the same. (Note that in each case, you only get very special arrays as values of of(x)
.)
However, you cannot define your of
as e.g.
of = x => [x, 0]
[f(x), 0] !== [x, 0].map(f)
Now
var grid = Grid.of({ width: 2, height: 2, list: [1, 2, 3, 4] })
is perfectly ok and returns your object passed wrapped into Grid
. Then you can map your grid
with any regular function f
from plain objects into plain objects and the result will be the same as applying f
and wrapping into Grid
, because of the natural transformation law. Note that this way you can also call Grid.of
with any other value like Grid.of({width: 2})
of even Grid.of(2)
. Alternatively, you can restrict the types for which Grid.of
is defined, then the value must only be of a type that you allow.
This one is a bit tricky:
Grid.of(2, 2, [1, 2, 3, 4])
This applies Grid.of
to several arguments. Since Grid.of
is by definition a function of only one argument, the result will be Grid.of(2)
, which may not be what you want. If you really want to feed all values, you probably want to write
Grid.of([2, 2, [1, 2, 3, 4]])
Alternatively, you can extend Grid.of
to multiple arguments by pre-wrapping them into an array internally and then applying Grid.of
. It really depends on what you are after.
For a real world usage example, see e.g. here where a "boring" Task is defined via Task.of
from a plain value. On the other hand here is a more interesting Task wrapping a function that you wouldn't get with Task.of
. What is important though, is that both Tasks can be used with the same uniform interface as shown on both examples.
Also note that no applicative functors are used in these examples, so there are still uses of pointed functors without being applicative.
ADDED.
See also https://github.com/MostlyAdequate/mostly-adequate-guide-it/blob/master/ch9.md#pointy-functor-factory for a nice introduction and real world uses of the Pointed Functor.
Foo.of
? did you mean Array.of? I thought, as of Es2015, only Array and TypedArray hadof
– Jaromanda XFoo
– Jaromanda XArray.of
is pointed function because, unlikeArray
, it interprets passed arguments in a single way no metter what arguments and how many arguments you pass, whileArray
differently interpretsArray(5)
andArray(5, 7)
. If you want to make yourFoo.of
a pointed function, force it to interpret arguments in a single way no matter what you pass to it. – user6586783