tadeuzagallo / verve-lang

A functional language for the working hacker
https://verve-lang.org
MIT License
346 stars 7 forks source link

Using map/filter as a method of List #52

Closed alexeyraspopov closed 6 years ago

alexeyraspopov commented 6 years ago

Hi!

I'm really excited to try Verve. I like the syntax and ideas behind it. Looking forward to try the lang on nontrivial tasks.

I've tried to write a bit of code today, and wanted to try how functions can be used as a method (I found this idea amazing).

So, basically, I have a function:

fn inc(x: Int) -> Int {
  x + 1
}

Which I want to apply to the list of numbers. And that's what I can easily do by using map()

> map(inc, [1, 2, 3])
Cons(U)(2)(Cons(U)(3)(Cons(U)(4)(Nil))) : List<Int>

However, when I try to use map as a method, I receive next output:

> [1, 2, 3].map(inc)
TypeError: Expected a value of type `(T) -> U`, but found `List<Int>

As I understand, it happens because 1) function as a method receives the target as a first argument 2) map(), filter(), and others receive the data as a second argument. I understand the idea behind the second point though.

Edit: as also expected from the things above, next code works

> inc.map([1, 2, 3])
Cons(U)(2)(Cons(U)(3)(Cons(U)(4)(Nil))) : List<Int>

Though, in a chain of calls like list.map(f).filter(g).reduce(h), I can't imagine the way to describe it in Verve.

Do you think there is any way the usage of map/filter/etc as methods can be achieved?

Thanks

tadeuzagallo commented 6 years ago

Hey, thanks for giving it a try and reporting this!

You're right, that was a "bug" in the standard library definition. (I say "bug" because it worked, but you're right that it wasn't great.) I had fixed it on d8e09fb5f6031f6cefafff7b524da47ce8b34326 a few weeks back, but had forgotten to push it. If you pull the most recent master it should work now.

Let me know if it works for you or if you have any further questions! 😃

alexeyraspopov commented 6 years ago

@tadeuzagallo, pulled latest master, verified, it now works. Thanks for that quick response.

Generics docs seem to not directly describe functions from Std, though use the pattern that now no longer applicable (for the sake of using functions as methods). Do you think the page should also be updated?

alexeyraspopov commented 6 years ago

@tadeuzagallo, I've opened #53 based on my previous comment.