Re: pint: Elizabeth, sort list???
hello, > ==> sort({ | map { +$_ // $_ }, .split: /\d+/, :v }) ==> say() ok … so I'm lost but I'm not even curious to understand why (because of my lack of interest for the ==> operator :)) regards marc -- Marc Chantreux Pôle CESAR (Calcul et services avancés à la recherche) Université de Strasbourg 14 rue René Descartes, BP 80010, 67084 STRASBOURG CEDEX 03.68.85.60.79
Re: pint: Elizabeth, sort list???
Hi Marc, This works: ==> sort({ | map { +$_ // $_ }, .split: /\d+/, :v }) ==> say() As the documentation says here https://docs.raku.org/routine/%3D%3D%26gt%3B The precedence is very loose so you will need to use parentheses to assign the result On Thu, Mar 7, 2024 at 8:37 AM Marc Chantreux wrote: > hello, > > > How would you write that expression using the feed operator? > > I tried > > < afoo12 afoo2 > > ==> {.sort: { | map { +$_ // $_ }, .split: /\d+/, :v }} > ==> {map } > > and the error message is really interesting > > Only routine calls or variables that can '.append' may > appear on either side of feed operators. > > on the other hand: I really don't understand why ==> even exists > as method call syntax works well. > > < afoo12 afoo2 > > .sort( { | map { +$_ // $_ }, .split: /\d+/, :v } ) > .map() > > what I would love instead is something closer than the haskell's $ > operator with a very low priority so it could be possible to be > parenthesis free. > > as example. I would like > > 1..10 ==> map * * 2 ==> say > > to be a joyful version of > > (1..10).map(* * 2).say > > regards > > -- > Marc Chantreux > Pôle CESAR (Calcul et services avancés à la recherche) > Université de Strasbourg > 14 rue René Descartes, > BP 80010, 67084 STRASBOURG CEDEX > 03.68.85.60.79 > > -- Fernando Santagata
Re: pint: Elizabeth, sort list???
hello, > How would you write that expression using the feed operator? I tried < afoo12 afoo2 > ==> {.sort: { | map { +$_ // $_ }, .split: /\d+/, :v }} ==> {map } and the error message is really interesting Only routine calls or variables that can '.append' may appear on either side of feed operators. on the other hand: I really don't understand why ==> even exists as method call syntax works well. < afoo12 afoo2 > .sort( { | map { +$_ // $_ }, .split: /\d+/, :v } ) .map() what I would love instead is something closer than the haskell's $ operator with a very low priority so it could be possible to be parenthesis free. as example. I would like 1..10 ==> map * * 2 ==> say to be a joyful version of (1..10).map(* * 2).say regards -- Marc Chantreux Pôle CESAR (Calcul et services avancés à la recherche) Université de Strasbourg 14 rue René Descartes, BP 80010, 67084 STRASBOURG CEDEX 03.68.85.60.79
Re: pint: Elizabeth, sort list???
Hi Elizabeth! How would you write that expression using the feed operator? Thanks, Shimon > On Mar 3, 2024, at 7:22 AM, Elizabeth Mattijsen wrote: > >> On 3 Mar 2024, at 03:32, ToddAndMargo via perl6-users >> wrote: >> >>> On 3/2/24 05:13, Elizabeth Mattijsen wrote: .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) >> >>> Hi Elizabeth, >>> It works perfectly. Thank you! >>> I have no idea why, I will ask you in another post >> Would you take apart your sort piece by piece and explain >> each part? > > Actually, the expression can be refined a bit: > > say .sort(*.split(/\d+/, :v).map({ (try .Int) // $_}).List) > > Sort works by using `cmp` semantics by default. > > If `cmp` is called on two lists, it will `cmp` each element and return the > result of the first comparison that did not produce `Same`. > > If you call `sort` with a Callable that takes only one argument, it is taken > as the producer of the actual values that will be compared, basically doing a > Schwartzian transform under the hood. > > A whatever code (which is what we specified here) produces a Callable with a > single argument. So we'll be doing a Schwartzian transform under the hood. > > The `split` splits the value (each element in the list) on any set of Numeric > characters (`\d+`), *but* also produces the strings that were split on > (`:v`). (The previous version had .kv, but that just produces more identical > entries in the list, which only will make comparisons slower). > > The resulting values from the `split` are then mapped to an integer value (if > possible: `(try .Int)` and if that fails, just returns the value (`// $_`). > (The previous version had `.Numeric`, which will also work, but since `\d+` > can only produce integers, it's an extra unnecessary step). > > Then convert the `.Seq` that is produced by the `.map` to a `List`. > Otherwise we'd be comparing `Seq` objects, and the semantics of those are > undefined. > > So for `"afoo12" cmp "afoo2"`, we will be doing `("afoo", 12) cmp ("afoo", > 2)`. Which would do: `"afoo" cmp "afoo"`, which produces `Same`, and then do > `12 cmp 2`, which will return `More`, and thus will cause swapping the order > of . > > > Hope that made sense!
Re: pint: Elizabeth, sort list???
Hi rakoons, On Sun, Mar 03, 2024 at 01:22:45PM +0100, Elizabeth Mattijsen wrote: > Actually, the expression can be refined a bit: > > say .sort(*.split(/\d+/, :v).map({ (try .Int) // $_}).List) Which is gorgeous! thanks for that. I read the documentation which is clear about all the magic in action there. So I played a little with it and came with this sort .say for < afoo12 afoo2 abar12 abar9foo abar64foo abar64bang abar64bang4foo abar64bang4bar abar64bang5bar abar64foo abar64foo4foo abar64foo4bar abar64foo14bar abar64foo5bar afoo13 afoo4 >.sort: { | map { +$_ // $_ }, .split: /\d+/, :v } The ouput seems to be ok. regards, -- Marc Chantreux Pôle CESAR (Calcul et services avancés à la recherche) Université de Strasbourg 14 rue René Descartes, BP 80010, 67084 STRASBOURG CEDEX 03.68.85.60.79
Re: pint: Elizabeth, sort list???
> On 3 Mar 2024, at 03:32, ToddAndMargo via perl6-users > wrote: > >> On 3/2/24 05:13, Elizabeth Mattijsen wrote: >>> .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) > >> Hi Elizabeth, >> It works perfectly. Thank you! >> I have no idea why, I will ask you in another post > Would you take apart your sort piece by piece and explain > each part? Actually, the expression can be refined a bit: say .sort(*.split(/\d+/, :v).map({ (try .Int) // $_}).List) Sort works by using `cmp` semantics by default. If `cmp` is called on two lists, it will `cmp` each element and return the result of the first comparison that did not produce `Same`. If you call `sort` with a Callable that takes only one argument, it is taken as the producer of the actual values that will be compared, basically doing a Schwartzian transform under the hood. A whatever code (which is what we specified here) produces a Callable with a single argument. So we'll be doing a Schwartzian transform under the hood. The `split` splits the value (each element in the list) on any set of Numeric characters (`\d+`), *but* also produces the strings that were split on (`:v`). (The previous version had .kv, but that just produces more identical entries in the list, which only will make comparisons slower). The resulting values from the `split` are then mapped to an integer value (if possible: `(try .Int)` and if that fails, just returns the value (`// $_`). (The previous version had `.Numeric`, which will also work, but since `\d+` can only produce integers, it's an extra unnecessary step). Then convert the `.Seq` that is produced by the `.map` to a `List`. Otherwise we'd be comparing `Seq` objects, and the semantics of those are undefined. So for `"afoo12" cmp "afoo2"`, we will be doing `("afoo", 12) cmp ("afoo", 2)`. Which would do: `"afoo" cmp "afoo"`, which produces `Same`, and then do `12 cmp 2`, which will return `More`, and thus will cause swapping the order of . Hope that made sense!
Re: pint: Elizabeth, sort list???
On 3/2/24 19:14, Clifton Wood wrote: .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // $_}).List) Can you take apart the above for me?
Re: pint: Elizabeth, sort list???
https://gist.github.com/Xliff/286f1359e40d192d724ed9b900354015 On Sat, Mar 2, 2024 at 9:32 PM ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > > On 3/2/24 05:13, Elizabeth Mattijsen wrote: > >> .sort(*.split(/\d+/, :kv).map({ (try .Numeric) // > $_}).List) > > > Hi Elizabeth, > > > > It works perfectly. Thank you! > > > > I have no idea why, I will ask you in another post > > Hi Elizabeth, > > Would you take apart your sort piece by piece and explain > each part? > > Many thanks, > -T > > >