Implement heap in functional style