Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

function as arguments

18 views
Skip to first unread message

Friedemann Groh

unread,
Sep 14, 2024, 3:29:38 PM9/14/24
to Macaulay2
Hello

Just now, I have passed a function defined in the main program as an argument to a function in a package. It works perfectly, but are there any limits and is that correct at all?

best
Friedemann

Doug Torrance

unread,
Sep 15, 2024, 6:52:41 AM9/15/24
to Macaulay2
Yes, that's totally correct!  Macaulay2 has "first-class functions".

Friedemann Groh

unread,
Sep 15, 2024, 8:06:22 AM9/15/24
to Macaulay2
That's quite impressive!

Mahrud Sayrafi

unread,
Sep 16, 2024, 10:08:16 AM9/16/24
to maca...@googlegroups.com
There's only a very subtle point, which is that identical functions are never equal, unless they are literally the same:

i1 : f = x -> x^2;

i2 : g = x -> x^2;

i3 : f === g

o3 = false

i4 : f === f

o4 = true

So you should be careful about caching outputs (e.g. using memoize) if the arguments themselves are functions:

i5 : myfunc = (func, x) -> (print "long computation"; func(x));

i6 : cachedfunc = memoize myfunc;

i7 : cachedfunc(f, 2)
long computation

o7 = 4

i8 : cachedfunc(f, 2)

o8 = 4

i9 : cachedfunc(g, 2)
long computation

o9 = 4

Mahrud

--
You received this message because you are subscribed to the Google Groups "Macaulay2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to macaulay2+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/macaulay2/fcb1526e-84f0-44e9-8ff5-504e1d5f8e81n%40googlegroups.com.

Friedemann Groh

unread,
Sep 16, 2024, 4:52:09 PM9/16/24
to Macaulay2
Thank you.
Reply all
Reply to author
Forward
0 new messages