Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foldable typeclass #681

Open
nimiwio opened this issue Sep 1, 2022 · 0 comments
Open

Foldable typeclass #681

nimiwio opened this issue Sep 1, 2022 · 0 comments

Comments

@nimiwio
Copy link

nimiwio commented Sep 1, 2022

It would be nice to have a Foldable typeclass similar to Haskell. Then things such as the following would work:

function sum
Foldable f => Integer ::= xs::f Integer { return foldl(\x::Integer y::Integer -> x + y, 0, xs); }

local aSet::Set<Integer> = ...
local aList::[Integer] = ...

local setSum::Integer = sum(aSet);
local listSum::Integer = sum(aList);

In my case, I was hoping for something like flatMap (foldMap in Haskell) to work on Set<a>, but that is specialized to lists only, so I had to write something like:

function foldMapSet
Monoid m => m ::= f::(m ::= a) s::set:Set<a> {
  return flatMap(f, set:toList(s));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant