extending operators in perl with operator::util

28
Operator::Util Nick Patch Perl Seminar NY 15 February 2011

Upload: nick-patch

Post on 12-Nov-2014

1.154 views

Category:

Technology


2 download

DESCRIPTION

Presented at Perl Seminar NY (PerlSemNY), February 2011.

TRANSCRIPT

Page 1: Extending Operators in Perl with Operator::Util

Operator::UtilNick Patch

Perl Seminar NY

15 February 2011

Page 2: Extending Operators in Perl with Operator::Util

released yesterday

Page 3: Extending Operators in Perl with Operator::Util

released yesterday

i ♥ feedback

Page 4: Extending Operators in Perl with Operator::Util

Perl 6 is pretty cool

Page 5: Extending Operators in Perl with Operator::Util

meta-operators

Page 6: Extending Operators in Perl with Operator::Util

meta-operators

[ ] Z X » «☺ ☺ ☺ ☺

Page 7: Extending Operators in Perl with Operator::Util

reduction

[+] 1..4 # 10 [*] 1..4 # 24 [~] 'a'..'d' # 'abcd'

Page 8: Extending Operators in Perl with Operator::Util

zip(1,2) Z+ (2,3) # 3,5 (1,2) Z* (2,3) # 2,6 (1,2) Z~ <a b> # 1a,2b

Page 9: Extending Operators in Perl with Operator::Util

cross (flat)(1,2) X+ (2,3) # 3,4,4,5 (1,2) X* (2,3) # 2,3,4,6 (1,2) X~ <a b> # 1a,1b,2a,2b

Page 10: Extending Operators in Perl with Operator::Util

cross (lol)(1,2) X+ (2,3) # [3,4],[4,5] (1,2) X* (2,3) # [2,3],[4,6] (1,2) X~ <a b> # [1a,1b],[2a,2b]

Page 11: Extending Operators in Perl with Operator::Util

hyper1..4 »~« 'a'..'d' # 1a,2b,3c,4d1..4 »~» 'x' # 1x,2x,3x,4x1..4 «~» <x y> # 1x,2y,1x,2y1..4 «~« <x y> # 1x,2y

Page 12: Extending Operators in Perl with Operator::Util

more hyper!-« (1,2,3) # -1,-2,-3 [1,[2,3]] »++ # [2,[3,4]] %foo «+» %bar # intersection%foo »+« %bar # union %foo »+=« %bar # %foo = union

Page 13: Extending Operators in Perl with Operator::Util

but it's not Xmas yet

Page 14: Extending Operators in Perl with Operator::Util

cpanm Operator::Util

Page 15: Extending Operators in Perl with Operator::Util

use Operator::Util qw( reducewith zipwith crosswith hyperwith );

Page 16: Extending Operators in Perl with Operator::Util

reducewith a.k.a. reducehyperwith a.k.a. hyper

Page 17: Extending Operators in Perl with Operator::Util

reductionreduce('+', [1..4]) # 10 reduce('*', [1..4]) # 24 reduce('.', ['a'..'d']) # 'abcd'

Page 18: Extending Operators in Perl with Operator::Util

zipzipwith('+', [1,2], [2,3]) # 3,5 zipwith('*', [1,2], [2,3]) # 2,6 zipwith('.', [1,2], ['a','b']) # 1a,2b

Page 19: Extending Operators in Perl with Operator::Util

cross (flat)crosswith('+', [1,2], [2,3]) crosswith('*', [1,2], [2,3]) crosswith('.', [1,2], ['a','b'])

# 3,4,4,5 # 2,3,4,6 # 1a,1b,2a,2b

Page 20: Extending Operators in Perl with Operator::Util

cross (lol)crosswith('+', [1,2], [2,3], flat=>0)crosswith('*', [1,2], [2,3], flat=>0)crosswith('.', [1,2], ['a','b'], flat=>0)

# [3,4],[4,5] # [2,3],[4,6] # [1a,1b],[2a,2b]

Page 21: Extending Operators in Perl with Operator::Util

hyperhyper('.', [1..4], ['a'..'d'] );hyper('.', [1..4], 'x', dwim_right=>1);hyper('.', [1..4], ['x','y'], dwim=>1 );hyper('.', [1..4], ['x','y'], dwim_left=>1 );

# 1a,2b,3c,4d# 1x,2x,3x,4x# 1x,2y,1x,2y# 1x,2y

Page 22: Extending Operators in Perl with Operator::Util

more hyper!hyper('prefix:-', [1,2,3]); hyper('postfix:++', [1,[2,3]]); hyper('+', \%foo, \%bar, dwim=>1);hyper('+', \%foo, \%bar); hyper('+=', \%foo, \%bar);

# -1,-2,-3 # [2,[3,4]] # intersection# union # %foo = union

Page 23: Extending Operators in Perl with Operator::Util

default ops zipwith(',', [1,2], ['a','b']) zip( [1,2], ['a','b'])crosswith(',', [1,2], ['a','b']) cross( [1,2], ['a','b'])

# 1,a,2,b # 1,a,2,b # 1,a,1,b,2,a,2,b# 1,a,1,b,2,a,2,b

Page 24: Extending Operators in Perl with Operator::Util

associativityreduce('-', [4, 3, 2])reduce('**', [4, 3, 2])

# 4-3-2 = (4-3)-2 = -1 # 4**3**2 = 4**(3**2) = 262144

Page 25: Extending Operators in Perl with Operator::Util

chainingreduce('eq', \@a) # all elements eq? reduce('!=', \@c) # no repeating elements?reduce('<', \@b) # ascending elements?

Page 26: Extending Operators in Perl with Operator::Util

dwim for < 2 elemsreduce('+', [] ) # 0reduce('+', [5]) # 5reduce('*', [] ) # 1reduce('*', [5]) # 5

Page 27: Extending Operators in Perl with Operator::Util

even more hyper!hyper('->', \@objects, 'run', dwim=>1) hyper('+', [[1, 2], 3], [4, [5, 6]], dwim=>1)hyper('prefix:-', {a => 1, b => 2, c => 3})

# call ->run() on each # [[5, 6], [8, 9]] # a => -1, b => -2, c => -3

Page 28: Extending Operators in Perl with Operator::Util

resources

git: github.com/patchslides: patch.github.com