Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Extending Operators in Perl with Operator::Util

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Nova Patch Nova Patch
February 15, 2011

Extending Operators in Perl with Operator::Util

NOTE: Operator::Util is an experimental module that is no longer actively maintained.

Presented at:
◦  2011-02-15: Perl Seminar NY (PerlSemNY), New York, NY

Avatar for Nova Patch

Nova Patch

February 15, 2011
Tweet

More Decks by Nova Patch

Other Decks in Programming

Transcript

  1. zip (1,2) Z+ (2,3) # 3,5 (1,2) Z* (2,3) #

    2,6 (1,2) Z~ <a b> # 1a,2b
  2. 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
  3. 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]
  4. hyper 1..4 »~« 'a'..'d' # 1a,2b,3c,4d 1..4 »~» 'x' #

    1x,2x,3x,4x 1..4 «~» <x y> # 1x,2y,1x,2y 1..4 «~« <x y> # 1x,2y
  5. more hyper! -« (1,2,3) # -1,-2,-3 [1,[2,3]] »++ # [2,[3,4]]

    %foo «+» %bar # intersection %foo »+« %bar # union %foo »+=« %bar # %foo = union
  6. zip zipwith('+', [1,2], [2,3]) # 3,5 zipwith('*', [1,2], [2,3]) #

    2,6 zipwith('.', [1,2], ['a','b']) # 1a,2b
  7. 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]
  8. hyper hyper('.', [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
  9. 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
  10. 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
  11. associativity reduce('-', [4, 3, 2]) reduce('**', [4, 3, 2]) #

    4-3-2 = (4-3)-2 = -1 # 4**3**2 = 4**(3**2) = 262144
  12. chaining reduce('eq', \@a) # all elements eq? reduce('!=', \@c) #

    no repeating elements? reduce('<', \@b) # ascending elements?
  13. dwim for < 2 elems reduce('+', [] ) # 0

    reduce('+', [5]) # 5 reduce('*', [] ) # 1 reduce('*', [5]) # 5
  14. 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