From 6ee0dd15e5ed7eb5341b2a4800cd04fde11ac632 Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Thu, 16 Jul 2020 09:48:44 +0800 Subject: [PATCH 1/7] Add RFC for SortBy --- text/0000-sort-by.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 text/0000-sort-by.md diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md new file mode 100644 index 00000000..3c9a42d6 --- /dev/null +++ b/text/0000-sort-by.md @@ -0,0 +1,38 @@ +- Feature Name: sort-by +- Start Date: 2020-04-27 +- RFC PR: (leave this empty) +- Pony Issue: (leave this empty) + +# Summary + +Add a `SortBy` primitive to `collections` package. + +# Motivation + +`SortBy` primitive like `Sort`, but `SortBy` use any type as the key. + +# Detailed design + +`SortBy` uses a `lambda` as the hash evaluation method instead of `interface Comparable`. + +```pony +use "collections" + +actor Main + new create(env:Env) => + let array = [ "aa"; "aaa"; "a" ] + SortBy(array, {(x: String): USize => x.size() }) + for e in array.values() do + env.out.print(e) // prints "a \n aa \n aaa" + end +``` + +# How We Test This + +All test cases for `Sort` are also valid for `SortBy`. Its design and implementation are already complete and working. +Source : https://github.com/damon-kwok/pony-shoe/blob/master/sort_by.pony + +# Drawbacks + +The user must ensure that the lambda evaluation function is valid. + From 382a9a93948602e7e1b98e46232fade288fb9aba Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Thu, 16 Jul 2020 10:31:05 +0800 Subject: [PATCH 2/7] Update Motivation and Detailed design --- text/0000-sort-by.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md index 3c9a42d6..6cdd5cf5 100644 --- a/text/0000-sort-by.md +++ b/text/0000-sort-by.md @@ -9,11 +9,13 @@ Add a `SortBy` primitive to `collections` package. # Motivation -`SortBy` primitive like `Sort`, but `SortBy` use any type as the key. +- Sort is not beginner friendly. +- Sort requires the implementation of the `Comparable interface`, if the existing type does not implement this interface will be helpless. # Detailed design -`SortBy` uses a `lambda` as the hash evaluation method instead of `interface Comparable`. +- `SortBy` has the same interface as `Sort`, but `SortBy` use any type as the key. +- `SortBy` uses a `lambda` as the hash evaluation method instead of `interface Comparable`. ```pony use "collections" From 8cc52b10576895d4394db474cb645f29598ed0e6 Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Thu, 16 Jul 2020 11:02:56 +0800 Subject: [PATCH 3/7] Update Motivation --- text/0000-sort-by.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md index 6cdd5cf5..c2ac45c5 100644 --- a/text/0000-sort-by.md +++ b/text/0000-sort-by.md @@ -9,12 +9,12 @@ Add a `SortBy` primitive to `collections` package. # Motivation -- Sort is not beginner friendly. -- Sort requires the implementation of the `Comparable interface`, if the existing type does not implement this interface will be helpless. +- Sort is not beginner friendly,The generic parameter of sort requires the implementation of the A interface.`SortBy` allows any type. +- If the existing type does not implement `Comparable` interface will be helpless. sort solves this problem by injecting a lambda. # Detailed design -- `SortBy` has the same interface as `Sort`, but `SortBy` use any type as the key. +- `SortBy` has the same interface as `Sort`. - `SortBy` uses a `lambda` as the hash evaluation method instead of `interface Comparable`. ```pony From 2a90cfc79196836a434f9db80b025a23c51a21bc Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Thu, 16 Jul 2020 11:21:23 +0800 Subject: [PATCH 4/7] Update Motivation and Drawbacks --- text/0000-sort-by.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md index c2ac45c5..b10accc9 100644 --- a/text/0000-sort-by.md +++ b/text/0000-sort-by.md @@ -9,8 +9,7 @@ Add a `SortBy` primitive to `collections` package. # Motivation -- Sort is not beginner friendly,The generic parameter of sort requires the implementation of the A interface.`SortBy` allows any type. -- If the existing type does not implement `Comparable` interface will be helpless. sort solves this problem by injecting a lambda. +The generic parameter of sort requires the implementation of the A interface,`SortBy` allows any type. If the existing type does not implement `Comparable` interface will be helpless. `SortBy` solves this problem by injecting a lambda. # Detailed design @@ -36,5 +35,6 @@ Source : https://github.com/damon-kwok/pony-shoe/blob/master/sort_by.pony # Drawbacks -The user must ensure that the lambda evaluation function is valid. +- The user must ensure that the lambda evaluation function is valid. +- Compared to Sort's compile-time check, `SortBy` has a little runtime overhead. From 5cc0c0a8552cc8a9f0d993764515b0bec856a9ae Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Thu, 16 Jul 2020 11:23:59 +0800 Subject: [PATCH 5/7] Update Motivation --- text/0000-sort-by.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md index b10accc9..ab63722f 100644 --- a/text/0000-sort-by.md +++ b/text/0000-sort-by.md @@ -9,7 +9,7 @@ Add a `SortBy` primitive to `collections` package. # Motivation -The generic parameter of sort requires the implementation of the A interface,`SortBy` allows any type. If the existing type does not implement `Comparable` interface will be helpless. `SortBy` solves this problem by injecting a lambda. +The generic parameter of sort requires the implementation of the `Comparable` interface, if the existing type does not implement `Comparable` interface will be helpless. `SortBy` allows any type, it solves this problem by injecting a lambda. # Detailed design From bbad0ace4ae41e38deb5c5026ce811bfcdaf059a Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Thu, 16 Jul 2020 11:40:06 +0800 Subject: [PATCH 6/7] Formatting fix --- text/0000-sort-by.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md index ab63722f..d17fb44d 100644 --- a/text/0000-sort-by.md +++ b/text/0000-sort-by.md @@ -9,20 +9,22 @@ Add a `SortBy` primitive to `collections` package. # Motivation -The generic parameter of sort requires the implementation of the `Comparable` interface, if the existing type does not implement `Comparable` interface will be helpless. `SortBy` allows any type, it solves this problem by injecting a lambda. +The generic parameter of `Sort` requires the implementation of the `Comparable` interface, if the existing type does not implement `Comparable` interface will be helpless, `SortBy` allows any type, it solves this problem by injecting a lambda. # Detailed design - `SortBy` has the same interface as `Sort`. - `SortBy` uses a `lambda` as the hash evaluation method instead of `interface Comparable`. +For example: + ```pony use "collections" actor Main new create(env:Env) => let array = [ "aa"; "aaa"; "a" ] - SortBy(array, {(x: String): USize => x.size() }) + SortBy[String](array, {(x: String): USize => x.size() }) for e in array.values() do env.out.print(e) // prints "a \n aa \n aaa" end From 4cf5453acb23f417af68f344c1dbe420dfaf3f94 Mon Sep 17 00:00:00 2001 From: damon-kwok Date: Sun, 23 Aug 2020 14:11:16 +0800 Subject: [PATCH 7/7] USize -> U64 --- text/0000-sort-by.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-sort-by.md b/text/0000-sort-by.md index d17fb44d..bdd8d3d7 100644 --- a/text/0000-sort-by.md +++ b/text/0000-sort-by.md @@ -24,7 +24,7 @@ use "collections" actor Main new create(env:Env) => let array = [ "aa"; "aaa"; "a" ] - SortBy[String](array, {(x: String): USize => x.size() }) + SortBy[String](array, {(x: String): U64 => x.size().u64() }) for e in array.values() do env.out.print(e) // prints "a \n aa \n aaa" end