New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement .fmt #393
Labels
Comments
From @masakNow also found in S29. :) |
From @masakImplementation attached. |
From @masakfmt.patchIndex: src/builtins/any-str.pir
===================================================================
--- src/builtins/any-str.pir (revision 32421)
+++ src/builtins/any-str.pir (working copy)
@@ -191,6 +191,23 @@ scalar .fmt
.return ($P0)
.end
+=item fmt
+
+ our Str multi Any::fmt ( Str $format )
+
+Returns the invocant formatted by an implicit call to sprintf.
+
+=cut
+
+.sub 'fmt' :method :multi(_)
+ .param string format
+ .local pmc retv
+
+ retv = 'sprintf'(format, self)
+
+ .return(retv)
+.end
+
=item index()
=cut
Index: src/classes/Mapping.pir
===================================================================
--- src/classes/Mapping.pir (revision 32421)
+++ src/classes/Mapping.pir (working copy)
@@ -137,6 +137,51 @@ mapping .fmt
.end
+=item fmt
+
+ our Str multi Mapping::fmt ( Str $format, $separator = "\n" )
+
+Returns the invocant mapping formatted by an implicit call to sprintf on
+the key and value of every pair, joined by newlines or an explicitly given
+separator.
+
+=cut
+
+.sub 'fmt' :method :multi('Hash')
+ .param pmc format
+ .param string sep :optional
+ .param int has_sep :opt_flag
+
+ .local pmc pairs
+ .local pmc res
+ .local pmc iter
+ .local pmc retv
+ .local pmc elem
+ .local pmc key
+ .local pmc value
+ .local pmc elemres
+
+ if has_sep goto have_sep
+ sep = "\n"
+ have_sep:
+ pairs = self.'pairs'()
+ res = new 'List'
+ iter = pairs.'iterator'()
+ elem_loop:
+ unless iter goto done
+
+ invoke:
+ elem = shift iter
+ elemres = elem.'fmt'(format)
+ push res, elemres
+ goto elem_loop
+
+ done:
+ retv = 'join'(sep, res)
+ .return(retv)
+.end
+
+
.sub 'keys' :method :multi('Hash')
.local pmc iter
.local pmc rv
Index: src/classes/Pair.pir
===================================================================
--- src/classes/Pair.pir (revision 32421)
+++ src/classes/Pair.pir (working copy)
@@ -75,6 +75,29 @@ pair .fmt
.end
+=item fmt
+
+ our Str multi Pair::fmt ( Str $format )
+
+Returns the invocant pair formatted by an implicit call to sprintf on
+the key and value.
+
+=cut
+
+.sub 'fmt' :method
+ .param pmc format
+
+ .local pmc retv
+ .local pmc key
+ .local pmc value
+
+ key = self.'key'()
+ value = self.'value'()
+ retv = 'sprintf'(format, key, value)
+
+ .return(retv)
+.end
+
=item perl
Returns a Perl code representation of the pair.
Index: src/classes/List.pir
===================================================================
--- src/classes/List.pir (revision 32421)
+++ src/classes/List.pir (working copy)
@@ -270,7 +270,45 @@ list .fmt
.return values.'first'(test)
.end
+=item fmt
+ our Str multi List::fmt ( Str $format, $separator = ' ' )
+
+Returns the invocant list formatted by an implicit call to sprintf on each
+of the elements, then joined with spaces or an explicitly given separator.
+
+=cut
+
+.sub 'fmt' :method :multi('ResizablePMCArray')
+ .param pmc format
+ .param string sep :optional
+ .param int has_sep :opt_flag
+
+ .local pmc res
+ .local pmc iter
+ .local pmc retv
+ .local pmc elem
+ .local pmc elemres
+
+ if has_sep goto have_sep
+ sep = ' '
+ have_sep:
+ res = new 'List'
+ iter = self.'iterator'()
+ elem_loop:
+ unless iter goto done
+
+ invoke:
+ elem = shift iter
+ elemres = 'sprintf'(format, elem)
+ push res, elemres
+ goto elem_loop
+
+ done:
+ retv = 'join'(sep, res)
+ .return(retv)
+.end
+
=item grep(...)
=cut
|
From @masakRemoved a few trailing spaces, and rewrote the comment in Mapping::fmt |
From @masakfmt-small-fixes.patchIndex: src/builtins/any-str.pir
===================================================================
--- src/builtins/any-str.pir (revision 32421)
+++ src/builtins/any-str.pir (working copy)
@@ -191,6 +191,23 @@
.return ($P0)
.end
+=item fmt
+
+ our Str multi Any::fmt ( Str $format )
+
+Returns the invocant formatted by an implicit call to C<sprintf>.
+
+=cut
+
+.sub 'fmt' :method :multi(_)
+ .param string format
+ .local pmc retv
+
+ retv = 'sprintf'(format, self)
+
+ .return(retv)
+.end
+
=item index()
=cut
Index: src/classes/Mapping.pir
===================================================================
--- src/classes/Mapping.pir (revision 32421)
+++ src/classes/Mapping.pir (working copy)
@@ -137,6 +137,50 @@
.end
+=item fmt
+
+ our Str multi Mapping::fmt ( Str $format, $separator = "\n" )
+
+Returns the invocant mapping formatted by an implicit call to C<.fmt> on
+every pair, joined by newlines or an explicitly given separator.
+
+=cut
+
+.sub 'fmt' :method :multi('Hash')
+ .param pmc format
+ .param string sep :optional
+ .param int has_sep :opt_flag
+
+ .local pmc pairs
+ .local pmc res
+ .local pmc iter
+ .local pmc retv
+ .local pmc elem
+ .local pmc key
+ .local pmc value
+ .local pmc elemres
+
+ if has_sep goto have_sep
+ sep = "\n"
+ have_sep:
+ pairs = self.'pairs'()
+ res = new 'List'
+ iter = pairs.'iterator'()
+ elem_loop:
+ unless iter goto done
+
+ invoke:
+ elem = shift iter
+ elemres = elem.'fmt'(format)
+ push res, elemres
+ goto elem_loop
+
+ done:
+ retv = 'join'(sep, res)
+ .return(retv)
+.end
+
+
.sub 'keys' :method :multi('Hash')
.local pmc iter
.local pmc rv
Index: src/classes/Pair.pir
===================================================================
--- src/classes/Pair.pir (revision 32421)
+++ src/classes/Pair.pir (working copy)
@@ -75,6 +75,29 @@
.end
+=item fmt
+
+ our Str multi Pair::fmt ( Str $format )
+
+Returns the invocant pair formatted by an implicit call to C<sprintf> on
+the key and value.
+
+=cut
+
+.sub 'fmt' :method
+ .param pmc format
+
+ .local pmc retv
+ .local pmc key
+ .local pmc value
+
+ key = self.'key'()
+ value = self.'value'()
+ retv = 'sprintf'(format, key, value)
+
+ .return(retv)
+.end
+
=item perl
Returns a Perl code representation of the pair.
Index: src/classes/List.pir
===================================================================
--- src/classes/List.pir (revision 32421)
+++ src/classes/List.pir (working copy)
@@ -270,7 +270,45 @@
.return values.'first'(test)
.end
+=item fmt
+ our Str multi List::fmt ( Str $format, $separator = ' ' )
+
+Returns the invocant list formatted by an implicit call to C<sprintf> on each
+of the elements, then joined with spaces or an explicitly given separator.
+
+=cut
+
+.sub 'fmt' :method :multi('ResizablePMCArray')
+ .param pmc format
+ .param string sep :optional
+ .param int has_sep :opt_flag
+
+ .local pmc res
+ .local pmc iter
+ .local pmc retv
+ .local pmc elem
+ .local pmc elemres
+
+ if has_sep goto have_sep
+ sep = ' '
+ have_sep:
+ res = new 'List'
+ iter = self.'iterator'()
+ elem_loop:
+ unless iter goto done
+
+ invoke:
+ elem = shift iter
+ elemres = 'sprintf'(format, elem)
+ push res, elemres
+ goto elem_loop
+
+ done:
+ retv = 'join'(sep, res)
+ .return(retv)
+.end
+
=item grep(...)
=cut
|
From @pmichaudApplied in r32478, thanks! Pm |
@pmichaud - Status changed from 'new' to 'resolved' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Migrated from rt.perl.org#60382 (status was 'resolved')
Searchable as RT60382$
The text was updated successfully, but these errors were encountered: