Last active
October 10, 2017 03:32
-
-
Save atweiden/9a4c260b56991e64b6b3bb2c0d770657 to your computer and use it in GitHub Desktop.
Evaluate earnings opportunities with Telescope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
# p6doc {{{ | |
=begin pod | |
=head NAME | |
Telescope | |
=head SYNOPSIS | |
Calculate dollars earned per second given a dollar amount earned in one year. | |
$ telescope.p6 --dollars-per-year=<num> [--gist] calc-dollars-per-second | |
Calculate how many seconds it would take to earn a desired amount given | |
a dollars per second earnings figure. | |
$ telescope.p6 \ | |
--dollars-per-second=<num> \ | |
--desired-amount=<num> \ | |
[--gist] \ | |
calc-seconds-required | |
Print constants. | |
$ telescope.p6 [--gist] print-constants | |
=head SCENARIO | |
A coin-operated telescope manufacturer claims its $5000 device pays for | |
itself in 2-4 years. | |
To be conservative, we assume the device earns $1000 per year in gross | |
revenue. | |
How many dollars is that per second? | |
$ telescope.p6 --dollars-per-year=1000 calc-dollars-per-second | |
{ | |
"dollars-per-second" : "0.000032", | |
"dollars-per-year" : "1000" | |
} | |
It's $0.000032 per second. | |
Assume the coin-operated telescope has a price of $0.25 per use. How | |
many seconds would it take to earn $0.25? | |
$ telescope.p6 \ | |
--dollars-per-second=0.000032 \ | |
--desired-amount=0.25 \ | |
calc-seconds-required | |
{ | |
"dollars-per-second" : "0.000032", | |
"required-hours" : "2.170139", | |
"desired-amount" : "0.25", | |
"required-years" : "0.0002477", | |
"required-minutes" : "130.208333", | |
"required-seconds" : "7812.5", | |
"required-days" : "0.090422" | |
} | |
It would take 7812.5 seconds (130 minutes) for the device to earn $0.25. | |
We could interpret this in different ways. Is someone really paying to | |
use the telescope on average once every two hours? Since telescopes are | |
likely not operated at night, those usages would need to be squeezed | |
into 12 hours or less of daylight. | |
More likely, an individual or child or large group of people uses the | |
telescope and pays a lot for it on occasion. | |
Alternatively, each machine could have its dedicated fans who use it | |
frequently throughout the year. Perhaps locals who live nearby are | |
regular customers. | |
=end pod | |
# end p6doc }}} | |
# constants {{{ | |
constant $seconds-per-minute = 60; | |
constant $minutes-per-hour = 60; | |
constant $hours-per-day = 24; | |
constant $days-per-year = 365; | |
constant $months-per-year = 12; | |
constant $seconds-per-year = | |
$seconds-per-minute | |
* $minutes-per-hour | |
* $hours-per-day | |
* $days-per-year; | |
# end constants }}} | |
# roles {{{ | |
# --- role Telescope['CalcDollarsPerSecond'] {{{ | |
role Telescope['CalcDollarsPerSecond'] | |
{ | |
has Rat:D $.dollars-per-year is required; | |
method dollars-per-second(::?CLASS:D: --> Rat:D) | |
{ | |
Rat($.dollars-per-year / $seconds-per-year); | |
} | |
method gist(::?CLASS:D: --> Str:D) | |
{ | |
my Rat:D $dollars-per-second = self.dollars-per-second(); | |
qq:to/EOF/.trim; | |
dollars per year : $.dollars-per-year | |
dollars per second : $dollars-per-second | |
EOF | |
} | |
method json(::?CLASS:D: --> Str:D) | |
{ | |
my Rat:D $dollars-per-second = self.dollars-per-second(); | |
Rakudo::Internals::JSON.to-json: | |
%(:$.dollars-per-year, :$dollars-per-second); | |
} | |
} | |
# --- end role Telescope['CalcDollarsPerSecond'] }}} | |
# --- role Telescope['CalcDollarsPerYear'] {{{ | |
role Telescope['CalcDollarsPerYear'] | |
{ | |
has Rat:D $.dollars-per-second is required; | |
method dollars-per-year(::?CLASS:D: --> Rat:D) | |
{ | |
Rat($.dollars-per-second * $seconds-per-year); | |
} | |
method gist(::?CLASS:D: --> Str:D) | |
{ | |
my Rat:D $dollars-per-year = self.dollars-per-year(); | |
qq:to/EOF/.trim; | |
dollars per year : $dollars-per-year | |
dollars per second : $.dollars-per-second | |
EOF | |
} | |
method json(::?CLASS:D: --> Str:D) | |
{ | |
my Rat:D $dollars-per-year = self.dollars-per-year(); | |
Rakudo::Internals::JSON.to-json: | |
%(:$dollars-per-year, :$.dollars-per-second); | |
} | |
} | |
# --- end role Telescope['CalcDollarsPerYear'] }}} | |
# --- role Telescope['CalcSecondsRequired'] {{{ | |
role Telescope['CalcSecondsRequired'] | |
{ | |
has Rat:D $.desired-amount is required; | |
has Rat:D $.dollars-per-second is required; | |
has Rat:D $!required-seconds = $!desired-amount / $!dollars-per-second; | |
has Rat:D $!required-years = $!required-seconds / $seconds-per-year; | |
has Rat:D $!required-months = $!required-years * $months-per-year; | |
has Rat:D $!required-days = $!required-years * $days-per-year; | |
has Rat:D $!required-hours = $!required-days * $hours-per-day; | |
has Rat:D $!required-minutes = $!required-hours * $minutes-per-hour; | |
method gist(::?CLASS:D: --> Str:D) | |
{ | |
qq:to/EOF/.trim; | |
desired amount : $.desired-amount | |
dollars per second : $.dollars-per-second | |
required seconds : $!required-seconds | |
required minutes : $!required-minutes | |
required hours : $!required-hours | |
required days : $!required-days | |
required months : $!required-months | |
required years : $!required-years | |
EOF | |
} | |
method json(::?CLASS:D: --> Str:D) | |
{ | |
Rakudo::Internals::JSON.to-json: | |
%(:$.desired-amount, | |
:$.dollars-per-second, | |
:$!required-seconds, | |
:$!required-minutes, | |
:$!required-hours, | |
:$!required-days, | |
:$!required-months, | |
:$!required-years); | |
} | |
} | |
# --- end role Telescope['CalcSecondsRequired'] }}} | |
# --- role Telescope['PrintConstants'] {{{ | |
role Telescope['PrintConstants'] | |
{ | |
method gist(--> Str:D) | |
{ | |
qq:to/EOF/.trim; | |
seconds per minute : $seconds-per-minute | |
minutes per hour : $minutes-per-hour | |
hours per day : $hours-per-day | |
days per year : $days-per-year | |
seconds per year : $seconds-per-year | |
EOF | |
} | |
method json(--> Str:D) | |
{ | |
Rakudo::Internals::JSON.to-json: | |
%(:$seconds-per-minute, | |
:$minutes-per-hour, | |
:$hours-per-day, | |
:$days-per-year, | |
:$months-per-year, | |
:$seconds-per-year); | |
} | |
} | |
# --- end role Telescope['PrintConstants'] }}} | |
# end roles }}} | |
# cli {{{ | |
# --- calc-dollars-per-second {{{ | |
multi sub MAIN( | |
'calc-dollars-per-second', | |
:dollars-per-year($d)!, | |
Bool:D :gist($)! where *.so | |
--> Nil | |
) | |
{ | |
my Rat:D $dollars-per-year = Rat($d); | |
Telescope['CalcDollarsPerSecond'].new(:$dollars-per-year).gist.say; | |
} | |
multi sub MAIN( | |
'calc-dollars-per-second', | |
:dollars-per-year($d)! | |
--> Nil | |
) | |
{ | |
my Rat:D $dollars-per-year = Rat($d); | |
Telescope['CalcDollarsPerSecond'].new(:$dollars-per-year).json.say; | |
} | |
# --- end calc-dollars-per-second }}} | |
# --- calc-dollars-per-year {{{ | |
multi sub MAIN( | |
'calc-dollars-per-year', | |
:dollars-per-second($d)!, | |
Bool:D :gist($)! where *.so | |
--> Nil | |
) | |
{ | |
my Rat:D $dollars-per-second = Rat($d); | |
Telescope['CalcDollarsPerYear'].new(:$dollars-per-second).gist.say; | |
} | |
multi sub MAIN( | |
'calc-dollars-per-year', | |
:dollars-per-second($d)! | |
--> Nil | |
) | |
{ | |
my Rat:D $dollars-per-second = Rat($d); | |
Telescope['CalcDollarsPerYear'].new(:$dollars-per-second).json.say; | |
} | |
# --- end calc-dollars-per-year }}} | |
# --- calc-seconds-required {{{ | |
multi sub MAIN( | |
'calc-seconds-required', | |
:dollars-per-second($d)!, | |
:desired-amount($a)!, | |
Bool:D :gist($)! where *.so | |
--> Nil | |
) | |
{ | |
my Rat:D $dollars-per-second = Rat($d); | |
my Rat:D $desired-amount = Rat($a); | |
Telescope['CalcSecondsRequired'].new( | |
:$dollars-per-second, | |
:$desired-amount | |
).gist.say; | |
} | |
multi sub MAIN( | |
'calc-seconds-required', | |
:dollars-per-second($d)!, | |
:desired-amount($a)! | |
--> Nil | |
) | |
{ | |
my Rat:D $dollars-per-second = Rat($d); | |
my Rat:D $desired-amount = Rat($a); | |
Telescope['CalcSecondsRequired'].new( | |
:$dollars-per-second, | |
:$desired-amount | |
).json.say; | |
} | |
# --- end calc-seconds-required }}} | |
# --- print-constants {{{ | |
multi sub MAIN( | |
'print-constants', | |
Bool:D :gist($)! where *.so | |
--> Nil | |
) | |
{ | |
Telescope['PrintConstants'].new.gist.say; | |
} | |
multi sub MAIN( | |
'print-constants' | |
--> Nil | |
) | |
{ | |
Telescope['PrintConstants'].new.json.say; | |
} | |
# --- end print-constants }}} | |
# end cli }}} | |
# vim: set filetype=perl6 foldmethod=marker foldlevel=0: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment