Skip to content
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

Eliminate some repetition in the setting #43

Closed
masak opened this issue Oct 14, 2015 · 0 comments
Closed

Eliminate some repetition in the setting #43

masak opened this issue Oct 14, 2015 · 0 comments

Comments

@masak
Copy link
Owner

masak commented Oct 14, 2015

I just caught sight of this in the setting code:

abs      => -> $arg { Val::Int.new(:value($arg.value.abs)) },
min      => -> $a, $b { Val::Int.new(:value(min($a.value, $b.value))) },
max      => -> $a, $b { Val::Int.new(:value(max($a.value, $b.value))) },
chr      => -> $arg { Val::Str.new(:value($arg.value.chr)) },
ord      => -> $arg { Val::Int.new(:value($arg.value.ord)) },
chars    => -> $arg { Val::Int.new(:value($arg.value.Str.chars)) },
uc       => -> $arg { Val::Str.new(:value($arg.value.uc)) },
lc       => -> $arg { Val::Str.new(:value($arg.value.lc)) },
trim     => -> $arg { Val::Str.new(:value($arg.value.trim)) },
elems    => -> $arg { Val::Int.new(:value($arg.elements.elems)) },
reversed => -> $arg { Val::Array.new(:elements($arg.elements.reverse)) },
sorted   => -> $arg { Val::Array.new(:elements($arg.elements.sort)) },
join     => -> $a, $sep { Val::Str.new(:value($a.elements.join($sep.value.Str))) },
split    => -> $s, $sep { Val::Array.new(:elements($s.value.split($sep.value))) },
index    => -> $s, $substr { Val::Int.new(:value($s.value.index($substr.value) // -1)) },
substr   => sub ($s, $pos, $chars?) { Val::Str.new(:value($s.value.substr($pos.value, $chars.defined ?? $chars.value !! $s.value.chars))) },

Clearly we're manually wrapping a lot of these in the right Val type. How about we do that automatically once and for all somewhere, instead of in every single subroutine?

  • The easy way: define a wrap which does the right thing for the various Val types, and call it everywhere. Would still make things shorter.
  • The HOP way: define a wrap which takes a function that returns a Perl 6 value and returns a function that returns an appropriate Val value. Call it once from the loop that put-vars everything in the setting.

Oh, and I noticed two bugs while I was in there:

say      => -> $arg { self.output.say(~$arg) },
type     => sub ($arg) { return 'Sub' if $arg ~~ Val::Sub; $arg.^name.substr('Val::'.chars) },

type returns a Str, not a Val::Str. The HOP way mentioned above would make this implementation right without any further changes. say should proabably return None, but right now it is likely to return a Perl 6 Bool, because that's what $*OUT.say returns:

$ perl6 -e 'say (say "hi").^name'
hi
Bool
masak pushed a commit that referenced this issue Oct 18, 2015
@masak masak closed this as completed in 93ea805 Oct 18, 2015
vendethiel pushed a commit to vendethiel/007 that referenced this issue Oct 20, 2015
vendethiel pushed a commit to vendethiel/007 that referenced this issue Oct 20, 2015
Instead of doing it repeatedly in all the built-in functions in
the setting.

Closes masak#43.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant