These are the modules I wrote while playing around with Python's Matplotlib in Perl6.
I blogged about the whole experience here
| use Inline::Python; | |
| my $py = Inline::Python.new(); | |
| $py.run('import matplotlib.pyplot'); | |
| class Matplotlib::Mlab { | |
| method FALLBACK($name, |c) { | |
| $py.call('matplotlib.mlab', $name, |c) | |
| } | |
| } | |
| class Matplotlib::Plot::Style { | |
| method use($arg) { | |
| $py.run("matplotlib.pyplot.style.use('$arg')", :eval) | |
| } | |
| method available { | |
| $py.run("matplotlib.pyplot.style.available", :eval) | |
| } | |
| method FALLBACK($name, |c) { | |
| $py.call('matplotlib.pyplot.style', $name, |c) | |
| } | |
| } | |
| class Matplotlib::Plot { | |
| method cm { | |
| class { | |
| method FALLBACK($name, $idx) { | |
| $py.run("matplotlib.pyplot.cm.{$name}($idx)", :eval); | |
| } | |
| }.new(); | |
| } | |
| method style { | |
| Matplotlib::Plot::Style.new | |
| } | |
| method rc-params { | |
| $py.run("dict(matplotlib.pyplot.rcParams)", :eval); | |
| } | |
| method rcParams { | |
| class { | |
| multi method AT-KEY('axes.prop_cycle') { | |
| $py.run( | |
| "list(matplotlib.pyplot.rcParams['axes.prop_cycle'])", :eval | |
| ).map(|*.values); | |
| } | |
| multi method AT-KEY($key) { | |
| $py.run("matplotlib.pyplot.rcParams['$key']", :eval); | |
| } | |
| multi method ASSIGN-KEY($key, Str $value) { | |
| $py.run("matplotlib.pyplot.rcParams['$key'] = '$value'"); | |
| return $value; | |
| } | |
| multi method ASSIGN-KEY($key, $value) { | |
| $py.run("matplotlib.pyplot.rcParams['$key'] = $value"); | |
| return $value; | |
| } | |
| }.new(); | |
| } | |
| method FALLBACK($name, |c) { | |
| $py.call('matplotlib.pyplot', $name, |c) | |
| } | |
| } | |
| class Matplotlib { | |
| method FALLBACK($name, |c) { | |
| $py.call('matplotlib', $name, |c) | |
| } | |
| } |
| class Numpl::Random { | |
| method randn($n) { | |
| sqrt( -2 × log(rand) ) × cos( τ × rand ) xx $n; | |
| } | |
| sub norm ($m, $s) { | |
| my $r = sqrt -2 * log rand; | |
| my $t = τ * rand; | |
| $r * cos($t) * $s + $m; | |
| } | |
| } | |
| class Numpl { | |
| proto method linspace(Numeric $start, Numeric $end, Int $steps?) { * } | |
| multi method linspace($start, $end, 0 ) { Empty } | |
| multi method linspace($start, $end, 1 ) { $start } | |
| multi method linspace($start, $end, $steps = 50, :$endpoint = True) { | |
| if $endpoint { | |
| my $step = ( $end - $start ) ÷ ( $steps - 1 ); | |
| return $start, * + $step ... * =~= $end | |
| } | |
| else { | |
| my $step = ( $end - $start ) ÷ $steps; | |
| return $start, * + $step ...^ * =~= $end | |
| } | |
| } | |
| method random { | |
| state $r = Numpl::Random.new(); | |
| } | |
| } |