Your SlideShare is downloading. ×
Extending Operators in Perl with Operator::Util
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Extending Operators in Perl with Operator::Util

919

Published on

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

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
919
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Operator::Util Nick Patch Perl Seminar NY 15 February 2011
  • 2. released yesterday
  • 3. released yesterday i ♥ feedback
  • 4. Perl 6 is pretty cool
  • 5. meta-operators
  • 6. meta-operators[ ☺] Z ☺ X ☺ » ☺«
  • 7. reduction[+] 1..4 # 10[*] 1..4 # 24[~] a..d # abcd
  • 8. zip(1,2) Z+ (2,3) # 3,5(1,2) Z* (2,3) # 2,6(1,2) Z~ <a b> # 1a,2b
  • 9. 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
  • 10. 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]
  • 11. 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
  • 12. more hyper!-« (1,2,3) # -1,-2,-3[1,[2,3]] »++ # [2,[3,4]]%foo «+» %bar # intersection%foo »+« %bar # union%foo »+=« %bar # %foo = union
  • 13. but its not Xmas yet
  • 14. cpanm Operator::Util
  • 15. use Operator::Util qw( reducewith zipwith crosswith hyperwith);
  • 16. reducewith a.k.a. reduce hyperwith a.k.a. hyper
  • 17. reductionreduce(+, [1..4]) # 10reduce(*, [1..4]) # 24reduce(., [a..d]) # abcd
  • 18. zipzipwith(+, [1,2], [2,3]) # 3,5zipwith(*, [1,2], [2,3]) # 2,6zipwith(., [1,2], [a,b]) # 1a,2b
  • 19. 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
  • 20. 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]
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. associativity reduce(-, [4, 3, 2]) reduce(**, [4, 3, 2])# 4-3-2 = (4-3)-2 = -1# 4**3**2 = 4**(3**2) = 262144
  • 25. chainingreduce(eq, @a) # all elements eq?reduce(!=, @c) # no repeating elements?reduce(<, @b) # ascending elements?
  • 26. dwim for < 2 elems reduce(+, [] ) # 0 reduce(+, [5]) # 5 reduce(*, [] ) # 1 reduce(*, [5]) # 5
  • 27. 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
  • 28. resourcesgit: github.com/patchslides: patch.github.com

×