From a715084a95b3ee6de1d73bab5ec1078f9308e6e3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 2 Mar 2024 11:24:21 +0000 Subject: Add example for generic functions --- examples/32-generic-functions/shed.toml | 7 +++++++ examples/32-generic-functions/src/HelloWorld.shed | 15 +++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 examples/32-generic-functions/shed.toml create mode 100644 examples/32-generic-functions/src/HelloWorld.shed (limited to 'examples') diff --git a/examples/32-generic-functions/shed.toml b/examples/32-generic-functions/shed.toml new file mode 100644 index 00000000..9c1a6d01 --- /dev/null +++ b/examples/32-generic-functions/shed.toml @@ -0,0 +1,7 @@ +[[shed.bins]] +name = "hello-world" +main-namespace = "HelloWorld" + +[[shed.examples]] +bin = "hello-world" +expected-output = "hello, world!" diff --git a/examples/32-generic-functions/src/HelloWorld.shed b/examples/32-generic-functions/src/HelloWorld.shed new file mode 100644 index 00000000..bc7b1243 --- /dev/null +++ b/examples/32-generic-functions/src/HelloWorld.shed @@ -0,0 +1,15 @@ +pub fun main() ! Io -> Unit { + val message = compose( + fn (a: String) { a + ", " }, + fn (b: String) { b + "world!\n" }, + )("hello"); + print!(message); +} + +fun compose[A, B, C]( + f: fn (A) -> B, + g: fn (B) -> C +) -> fn (A) -> C { + return fn (a) { g(f(a)) }; +} + -- cgit v1.2.3