# Higher order functions and templates

**URL:** <https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780>\
**Category:** Google Group\
**Created:** [January 4, 2016, 1:19am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780 "2016-01-04T01:19:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [January 4, 2016, 1:19am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/1 "2016-01-04T01:19:02Z")

</div>

The introduction has this statement:

In ATS2, linear closure-functions become much less important as programming with higher-order functions in a setting without GC can be more conveniently achieved through the use of templates.

I’m struggling to know why this conclusion. A higher order function takes a function as an argument, which is just a closure, or closure with no values in an environment. I can see that templates add flexibility in the signature of the closure as the template can take compatible parameters and be more generic, such as a generic map function.

But I don’t understand how this makes closures less important.

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [January 4, 2016, 2:58am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/2 "2016-01-04T02:58:23Z")

</div>

And the implements functions must be templates? I’ll guess yes, because template is a natural thing to expand.  
Can the function around the implementations be a non template? I’ll guess no, because the inner expansion probably relies on an outer expansion.

Where expand means instance.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [January 4, 2016, 1:41am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/3 "2016-01-04T01:41:56Z")

</div>

Maybe the last example on the homepage of ats-lang could shed some light:

[http://www.ats-lang.org/On](http://www.ats-lang.org/On) Sunday, January 3, 2016 at 8:19:03 PM UTC-5, Mike Jones wrote:

> The introduction has this statement:
> 
> In ATS2, linear closure-functions become much less important as  
> programming with higher-order functions in a setting without GC can be more  
> conveniently achieved through the use of templates.
> 
> I’m struggling to know why this conclusion. A higher order function takes  
> a function as an argument, which is just a closure, or closure with no  
> values in an environment. I can see that templates add flexibility in the  
> signature of the closure as the template can take compatible parameters and  
> be more generic, such as a generic map function.
> 
> But I don’t understand how this makes closures less important.

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [January 4, 2016, 2:11am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/4 "2016-01-04T02:11:17Z")

</div>

Does the implement in a where clause only work for templates?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [January 4, 2016, 3:23am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/5 "2016-01-04T03:23:41Z")

</div>

> > Can the function around the implementations be a non template?

Yes, it can be a non-template function.

It is very often that you want to implement a template inside a  
non-template function. In this way, the template function gets to  
access to the arguments of the non-template function.

Please take a look at wordcnt2.dats at the word-counting example:

[http://ats-lang.sourceforge.net/EXAMPLE/EFFECTIVATS/](http://ats-lang.sourceforge.net/EXAMPLE/EFFECTIVATS/)

Using templates properly can greatly reduce complexity in programming.

Say someone is asked to implement the following example involving some  
drawing:

[http://ats-lang.sourceforge.net/EXAMPLE/EFFECTIVATS/Sierpinski-3angle/Sierpinski-3angle.html](http://ats-lang.sourceforge.net/EXAMPLE/EFFECTIVATS/Sierpinski-3angle/Sierpinski-3angle.html)

Most likely, the person would immediately think about the canvas. In my  
opinion, the person should  
focus on drawing and forget about the canvas for a while. This is very easy  
to achieve with templates.

Having too much stuff on one’s mind clutters the mind.On Sunday, January 3, 2016 at 9:58:23 PM UTC-5, Mike Jones wrote:

> And the implements functions must be templates? I’ll guess yes, because  
> template is a natural thing to expand.  
> Can the function around the implementations be a non template? I’ll guess  
> no, because the inner expansion probably relies on an outer expansion.
> 
> Where expand means instance.

---

<div class="post-metadata">

**Author:** ![Mike\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Mike\_Jones](https://discourse.ats-lang.org/u/Mike_Jones)\
**Post date:** [January 4, 2016, 2:12am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/6 "2016-01-04T02:12:56Z")

</div>

And I assume the implement can’t capture values like a closure because it has to compile everything up front. Correct?

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [January 4, 2016, 2:33am UTC](https://discourse.ats-lang.org/t/higher-order-functions-and-templates/780/7 "2016-01-04T02:33:40Z")

</div>

You are right. It cannot.On Sun, Jan 3, 2016 at 9:12 PM, Mike Jones [proc...@gmail.com](mailto:proc...@gmail.com) wrote:

> And I assume the implement can’t capture values like a closure because it  
> has to compile everything up front. Correct?
> 
> –  
> You received this message because you are subscribed to the Google Groups  
> “ats-lang-users” group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [ats-lang-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [https://groups.google.com/group/ats-lang-users](https://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ats-lang-users/6b75bdc8-619b-4295-af73-91f1b3c98a11%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/6b75bdc8-619b-4295-af73-91f1b3c98a11%40googlegroups.com)  
> .
