map, fold, etc in Roy?

105 views
Skip to first unread message

Leif Warner

unread,
Jul 9, 2012, 1:14:24 AM7/9/12
to roy...@googlegroups.com
Since [] is a type in Roy, and seems to map to the Array in JavaScript - would it be possible to have map, fold, filter, etc, in Roy, and have those map to the native definitions of Array.prototype.map, etc?  Those things are defined in most any JavaScript version apart from old IE, and in those people could use a shim to assign them to Array.prototype, e.g. https://github.com/jivesoftware/jiverscripts/blob/master/src/compat/array.js.  If [a] is well-typed, you ought to be able to have a map :: (a -> b) -> [a] -> [b] that simply outputs something like function(f, a) { return a.map(f); } in the resulting JavaScript.

Richard Dallaway

unread,
Jul 10, 2012, 4:56:13 AM7/10/12
to roy...@googlegroups.com
On 9 July 2012 06:14, Leif Warner <abim...@gmail.com> wrote:
Since [] is a type in Roy, and seems to map to the Array in JavaScript - would it be possible to have map, fold, filter, etc, in Roy, and have those map to the native definitions of Array.prototype.map, etc?  Those things are defined in most any JavaScript version apart from old IE, and in those people could use a shim to assign them to Array.prototype, e.g. https://github.com/jivesoftware/jiverscripts/blob/master/src/compat/array.js.  If [a] is well-typed, you ought to be able to have a map :: (a -> b) -> [a] -> [b] that simply outputs something like function(f, a) { return a.map(f); } in the resulting JavaScript.

Not an answer to your message, but a thank you:  I'd been struggling on how to implement tail (and similar) for [] and your email gave me the clue:

roy> let xs = [1,2,3]
roy> let tail xs = Array.prototype.slice.call xs 1
roy> tail xs
[2,3] : Native

Cheers
Richard

Dirk Detering

unread,
Jul 10, 2012, 11:39:51 AM7/10/12
to roy...@googlegroups.com

Presumably those functions belong into typeclasses...?

Leif Warner

unread,
Jul 10, 2012, 2:31:59 PM7/10/12
to roy...@googlegroups.com
Alright, so you can define map as:

let map f xs = Array.prototype.map.call xs f
ost reply
More message actions

But then you lose type information on the result.  Is there any way of telling Roy, "no really, if xs is [a], and f is a ->b, then this will result in [b]".  Explicit type annotations, typecasting, etc?

Also, if you want to keep the resulting JavaScript looking like what you'd expect / readable, it might be nicer to "map f xs" output as "xs.map(f)", and not "Array.prototype.map.call(xs, f)".

-Leif
On Tuesday, July 10, 2012 1:56:13 AM UTC-7, Richard Dallaway wrote:

Brian McKenna

unread,
Jul 11, 2012, 6:39:42 PM7/11/12
to roy...@googlegroups.com
Yes. Type classes!

Roy will have a chunk of type classes built in and Array will have
instances for:

* Functor (map)
* Semigroup (append)
* Monoid (empty)
* Foldable (fold, sum)
* Foldable1 (fold1, sum1)
* Length (length)
* + more!

The JS output would get ugly with the current implementation of type
classes. My idea is to have certain function instances be marked as
"inlinable". For example, if you do "length []" then it should compile
to:

[].length

Instead of:

arrayLength.length([])

Proper abstraction is one reason to be tied to the typeclassopedia.
The other is that we might be able to implement lenses properly :)

What do you think?
Reply all
Reply to author
Forward
0 new messages