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

Perl 6 in context

lichtkind
September 05, 2015

Perl 6 in context

A longer talk about the many operators of Perl 6 with a deeper introduction what are ops good for and why Perl is called an operator oriented language

lichtkind

September 05, 2015
Tweet

More Decks by lichtkind

Other Decks in Programming

Transcript

  1. Perl 6 in context

  2. Up to now: general Array / Hash Regex OOP

  3. lichtkind.de general Array / Hash Regex OOP

  4. big topics general variable parser abstraction

  5. big topics variable parser abstraction expression

  6. OOP ==> OOL Array / Hash Regex OOP Operator

  7. OOP ==> OOL Operator Oriented Language

  8. word about operators

  9. operator pictogram

  10. None
  11. Edge Detection pictogram quick orientation like indentation

  12. Done By Retina pictogram quick orientation like indentation

  13. I forgot to mention

  14. Visual Cortex

  15. information / min incoming: 12 000 000 conscious: 40

  16. operator pictogram quick orientation

  17. Visual Recognition pictogram quick orientation like empty line

  18. Visual Recognition pictogram quick orientation like indentation

  19. Visual Recognition pictogram quick orientation decor. comments

  20. One more thing ...

  21. emotional brain

  22. Antonio Damasio

  23. Antonio Damasio Leading Neurologist

  24. Emotion != Feelings

  25. Feeling

  26. Emotion

  27. Damasio: Emotion is how brain deal with huge data quantities.

  28. does association

  29. Emotions: triggered (words|pic) values & judgement enables memory

  30. TIMTOWTDI = Respect your emotional wiring (experience)

  31. TIMTOWTDI = less emotional stress = higher productivity

  32. Ambiguous: Java: Str + Str

  33. Not Ambiguous: Perl 5/6: Num + Num

  34. Topicalizer Perl 6: class instead package

  35. One more thing ...

  36. Jill Bolte

  37. Jill Bolte: Neuroanatomist having left side stroke, experiencing just the

    emotional mind
  38. Jill Bolte: left brain works in sequence (future/past) and enables

    language
  39. Jill Bolte: right brain works in parallel, cares about now,

    emotions, whole picture, graphics
  40. conclusio: use right brain to grok complex systems

  41. James Gates

  42. Adinkra:

  43. None
  44. New P6 Meta Ops: more direct right brain access

  45. End of my Sermon

  46. Right brain in action

  47. Synopsis 1: Overview

  48. Perl 5 in context

  49. Perl 5 in context wantarray

  50. context: wantarray true (else) - array false (0|'') - scalar

    undef - void
  51. Perl 6 in context no wantarray !!!

  52. P6 Internals context = data type = class

  53. Type Classes: Num Str Array Hash ...

  54. As Known: my $num = 12; my $str = 'text';

  55. Optional: my Num $num = 12; my Str $str =

    'text';
  56. How to convert ? my Num $num = 12; my

    Str $str = 'text';
  57. As Java knows ? public method to_string {

  58. Not Perl 6: $var.to_string();

  59. Not Perl 5: $var.to_string();

  60. Perl 5 in Context $nr =()= $str =~ /.../g;

  61. Secret Goatse Op $nr =()= $str =~ /.../g;

  62. No Real List Context $nr =()= $str =~ /.../g;

  63. Explicit in Perl 6 @() array

  64. Explicit in Perl 5 @{} array

  65. Explicit in Perl 6 $() scalar @() array %() hash

    &() code ::() namespace
  66. Perl 6 Major Contex $ scalar @ array % hash

  67. Invariant Sigil $ scalar @ array % hash

  68. Invariant Sigil $scalar @array %hash

  69. Don't Show Context $scalar @array[5] %hash{'key'}

  70. Native Hash Slice $scalar @array[5] %hash<key>

  71. Sigils $ scalar @ positional % asociative & callable ::

    namespace
  72. Context operator $() scalar @() array %() hash &() code

    ::() namespace
  73. With Long Version $() item() @() list() %() hash() &()

    code() ::()
  74. Braces Optional $() item() @() list() %() hash() &() code()

  75. Item Context $() item() @() list() %() hash() &() code()

    ::()
  76. List Context $() item() @() list() %() hash() &() code()

    ::()
  77. P5 List Context $() item() @() flat() %() hash() &()

    code() ::()
  78. Hash Context $() item() @() list() %() hash() &() code()

    ::()
  79. Code Context $() item() @() list() %() hash() &() code()

    ::()
  80. Namescpace Context $() item() @() list() %() hash() &() code()

    ::( $str )
  81. More Context Op ~ string + numeric ? boolean

  82. Negative Op ~ string + - numeric ? ! boolean

  83. Example without () ~@list +@list ?@list

  84. String Context ~@list @list[0]~@list[1] +@list ?@list

  85. Num Context ~@list @list[0]~@list[1] +@list @list.elems ?@list

  86. Bool Context ~@list @list[0]~@list[1] +@list @list.elems ?@list @list.elems > 0

  87. Bool Context ?

  88. Bool Context ? ! ?^ ?| ?& ^ | &

    // ^^ || && ff fff ?? !!
  89. Grey is Logic ? ! ?^ ?| ?& ^ |

    & // ^^ || && ff fff ?? !!
  90. Bool Context ?

  91. Bool Context my $var = 45; say ?$var;

  92. Bool Context my $var = 45; say ?$var; True

  93. Bool Context my $var = 45; say ?$var; True Bool::True

    in String Context
  94. Bool Context my $var = 45; say !$var; False Bool::False

    v String Cont.
  95. Is it so ? my $var = e; say so($var);

    True Bool::True v String Context
  96. Is it not so ? my $var = e; say

    not $var; False Bool::False v String Cont.
  97. High Precedence my $var = 45; say ?$var + 1;

  98. High Precedence my $var = 45; say ?$var + 1;

    2 True in Num Context = 1
  99. Low Precedence my $var = 45; say so $var +

    1;
  100. Low Precedence my $var = 45; say so $var +

    1; True 46 v Bool Kontext = True
  101. Bool Context my $var = 45; say 1 if $var

    + 1; 1 46 v Bool Context = True
  102. Bool Context my $var = 45; say 1 if $var

    + 1; 1 If unless while until
  103. That was easy ! ? ! ?^ ?| ?& ^

    | & // ^^ || && ff fff ?? !!
  104. Still ? ?^ ?| ?&

  105. Wants Bool Context ?^ ?| ?&

  106. Known Logic Ops ?^ ?| ?&

  107. 1+2 = ? ?^ ?| ?&

  108. What could that be? say 0 ?| 'tree';

  109. Clearly !!! say 0 ?| 'tree'; True False or True

    = True
  110. What could that be? say 5 ?^ 0.0;

  111. Clear as daylight. say 5 ?^ 0.0; True True xor

    False = True
  112. You get a sense ? ! ?^ ?| ?& ^

    | & // ^^ || && ff fff ?? !!
  113. Hmmmmm ? ^ | &

  114. Hmmmmm ? $var = 0 | 'tree'; say $var;

  115. Now Know More ? $var = 0 | 'les'; say

    $var; any(0, 'les')
  116. Junctions ! $var = 0 | 'tree'; say $var; any(0,

    tree) literally: 0 or 'tree'
  117. Short Overview 0 | 1 | 3 = any(0,1,3); 0

    & 1 & 3 = all(0,1,3); 0 ^ 1 ^ 3 = one(0,1,3);
  118. Quiz Time ! 2 ~~ 0 | 1 | 3

    | 7
  119. Expected Differently 2 ~~ 0 | 1 | 3 |

    7 False
  120. Next Question 1 == 0 | 1 | 3

  121. You get: 1 == 0 | 1 | 3 any(False,

    True, False)
  122. Nicer if statements ! if $val == 0 | 1

    | 3 { ...
  123. Junctions ! if $val == 0 | 1 | 3

    { ... True
  124. Junctions ! if $val == 0 | 1 | 3

    { ... any(False, True, False).to_bool
  125. It Gets Clearer ? ! ?^ ?| ?& ^ |

    & // ^^ || && ff fff ?? !!
  126. No Forced Context // ^^ || && ff fff

  127. short circuit OR doit() || doelse();

  128. short circuit OR doit() || doelse(); doit() unless doelse();

  129. Defined OR doit() // doelse();

  130. Defined OR doit() // doelse(); doelse() unless defined doit();

  131. short circuit AND doit() && doelse(); doelse() if doit();

  132. short circuit XOR doit() ^^ doelse();

  133. eXclusive OR doit() ^^ doelse(); my($l, $r)=(doit(), doelse()); if not

    $l { $r } else { $r ?? Nil !! $l }
  134. No else with unless doit() ^^ doelse(); my($l, $r)=(doit(), doelse());

    if not $l { $r } else { $r ?? Nil !! $l }
  135. All Shortcuts this() || that(); this() // that(); this() &&

    that(); this() ^^ that();
  136. Boundary Values $a min $b $a max $b $a minmax

    $b
  137. Boundary Values $a min $b $a max $b minmax @a

  138. Flipflop begin() ff end(); begin() fff end();

  139. Was .. | … in $ contxt while … {

    run() if begin() ff end(); run() if begin() fff end(); }
  140. Skoro u Cile ? ! ?^ ?| ?& ^ |

    & // ^^ || && ff fff ?? !!
  141. Ternärer Op ?? !!

  142. Ternary Op was ? : ?? !!

  143. Ternary Op was ? : eval in Bool context ??

    !!
  144. Ternary Op was ? : eval in Bool context values

    unchanged ?? !!
  145. All Clear Now ? ? ! ?^ ?| ?& ^

    | & // ^^ || && ff fff ?? !!
  146. Numeric Kontext + - * / % %% ** +^

    +| +& +< +>
  147. Zna každej :) + - * / % %% **

    +^ +| +& +< +>
  148. Modulo 7 / 3 7/3(2.333) | 2

  149. Modulo 7 % 3

  150. Modulo 7 % 3 7 = 3 * 2 +

    1
  151. ModMod? 7 %% 3

  152. Indivisible 7 %% 3 False => remainder 1

  153. Numeric Context + - * / % %% ** +^

    +| +& +< +>
  154. Bit Logic +^ +| +&

  155. Bit Logic (was:) ^ | & +^ +| +&

  156. Bit - Shift +< +>

  157. Bit - Shift (was:) << >> +< +>

  158. Numeric Context + - * / % %% ** +^

    +| +& +< +>
  159. Someth. Forgotten?

  160. Someth. Forgotten? ++ --

  161. Ordered Sets ++ after - - before cmp

  162. Ordered Sets cmp: Less, Same, More

  163. Ordered Sets cmp: Less, Same, More -1, 0, 1

  164. Still in Context <=> leg cmp

  165. Compare in Context <=> Num Context leg Str Context cmp

    everwhere
  166. Compare in Context < Num Context lt Str Context before

    everwhere
  167. Compare in Context > Num Context gt Str Context after

    everwhere
  168. Ordered Sets ++ 1 after - - 1 before

  169. Equality in Context == Num Context eq Str Context ===

    everywhere
  170. Equality in Context == Num Context eq Str Context eqv

    everywhere
  171. Equality in Context == Num Context =:= binding eqv dynamic

  172. Dynamic in Context if 2 eqv 2.0 {

  173. Data Type => Content if 2 eqv 2.0 { Int()

    vs. Rat()
  174. Data Type => Content if 2 == 2.0 {

  175. Data Type => Content if 2 == 2.0 { True

    (Num Kontext)
  176. Numeric Context + - * / % %% ** +^

    +| +& +< +>
  177. String Context ~ ~^ ~| ~& ~~ x

  178. Perlish to_string ~

  179. Was Once . ~ say 'combine' ~ 'Watson';

  180. String Context ~ ~^ ~| ~& ~~ x

  181. Letter Logic ~^ ~| ~&

  182. Letter Logic 1 +| 2 = 3 'a' ~| 'b'

    = 'c'
  183. String Context ~ ~^ ~| ~& ~~ x

  184. String Context ~ ~^ ~| ~& ~~ x

  185. Anyone Knows say '-' x 20;

  186. Multiply Strings say '-' x 20; '--------------------'

  187. String Context ~ ~^ ~| ~& ~~ x

  188. List Context , … xx X Z <<== <== ==>

    ==>>
  189. Comma Operator @fib = 1, 1, 2, 3, 5;

  190. Same As: $fib = (1, 1, 2, 3, 5);

  191. Comma Operator $fib = (1); say $fib.WHAT; Int

  192. Comma Operator $fib = (1 ,); say $fib.WHAT;

  193. Comma Operator $fib = (1 ,); say $fib.WHAT; Parcel

  194. Comma Operator $fib = (1 ,); say $fib.WHAT; List of

    Parameter
  195. Capture Context | named parameter || positional parameter

  196. List Context , … xx X Z <<== <== ==>

    ==>>
  197. Sequence Operator $d = 1, 2 … 9;

  198. Yadda Operator sub planned { … }

  199. Yadda Operator sub planned { … } sub planned {

    ??? } sub planned { !!! }
  200. Sequence Operator $d = 0, 1 … 9;

  201. Range Op can't do! $d = 9, 8 … 0;

  202. Range Op can't do! $d = 9 .. 0;

  203. Sequence Operator $zp = 1, 2, 4… 256;

  204. Sequence Operator $fib = 1, 1, *+* … *;

  205. Forgot something? $d = 0 .. 9;

  206. Forgot something ? say 0 .. 9;

  207. No List ? say 0 .. 9; 0..9

  208. Depends On Context say (0 .. 9);

  209. No List ? say (0 .. 9); 0..9

  210. What is it ? say (0 .. 9);

  211. What is it ? say (0 .. 9).WHAT;

  212. Range ??? say (0 .. 9).WHAT; Range

  213. Range ??? say (0 .. 9).WHAT; Obj with 2 values

  214. Range ??? say 5 ~~ 0 .. 9; True

  215. How you create Lists say @(0..9).WHAT; List

  216. List - Output? say @(0 .. 9); 0 1 2

    3 4 5 6 7 8 9
  217. for - ces List context say $_ for 0 ..

    9; 0 1 2 3 4 5 6 7 8 9
  218. real perlheads do: say for 0 .. 9;

  219. real perl5heads do: say for 0 .. 9;

  220. Perl 6 heads: .say for 0 .. 9; 0 1

    2 3 4 5 6 7 8 9
  221. List Context , … xx X Z <<== <== ==>

    ==>>
  222. Play with Lists xx X Z

  223. xx Operator

  224. xx Operator say 'eins zwo eins zwo';

  225. xx Operator say 'eins zwo eins zwo'; say q:words(eins zwo)

    xx 2;
  226. xx Operator say 'eins zwo eins zwo'; say q:words(eins zwo)

    xx 2; say q:w(eins zwo) xx 2;
  227. xx Operator say 'eins zwo eins zwo'; say q:words(eins zwo)

    xx 2; say q:w(eins zwo) xx 2; say qw(eins zwo) xx 2;
  228. xx Operator say 'eins zwo eins zwo'; say q:words(eins zwo)

    xx 2; say q:w(eins zwo) xx 2; say qw(eins zwo) xx 2; say <eins zwo> xx 2;
  229. X Operator say <eins zwo> X <dan rabauke>;

  230. Cartesian Product say <eins zwo> X <dan rabauke>; eins dan

    eins rabauke zwo dan zwo rabauke
  231. Its pairs in real: say <eins zwo> X <dan rabauke>;

    ('eins','dan'),('eins','rabauke'), ('zwo','dan'),('zwo','rabauke')
  232. Its pairs in real: say elems(<1 2>X<3 4>); 4

  233. Z Operator say <eins zwo> Z <dan rabauke>;

  234. Zip say <eins zwo> Z <dan rabauke>; eins dan zwo

    rabauke
  235. Zip say <eins zwo> zip <dan rabauke>; eins dan zwo

    rabauke
  236. Zip as a Op for @li Z @re -> $l,

    $r {
  237. read write var for @li Z @re <-> $l,$r {

  238. List Context , xx X Z <<== <== ==> ==>>

  239. Schwartz Transform my @output = map { $_->[0] } sort

    { $a->[1] cmp $b->[1] } map { [$_,expensive_func($_)] } @input;
  240. Pipe Operator my @output <== map { $_[0] } <==

    sort { $^a[1] cmp $^b[1] } <== map { [$_, expensive_fun($_)] } <== @input;
  241. Other Direction @input ==> map { [$_,expensive_fun($_)] } ==> sort

    { $^a[1] cmp $^b[1] } ==> map { $_[0] } ==> my @output;
  242. Append Mode my @output <<== map { $_[0] } <<==

    sort { $^a[1] cmp $^b[1] } <<== map { [$_,expensive_fun($_)] } <<== @input;
  243. Pointy Sub for @input -> $i { ...

  244. List Context , xx X Z <<== <== ==> ==>>

  245. Meta Ops = ! X Z R S [] [\]

    << >>
  246. Meta Op = @sum += 3;

  247. Meta Op ! if $age !< 18 {

  248. Meta Op ! if $age !< 18 { # real

    P6 code
  249. Meta Op R $age = 2 R- 18; # ==

    16
  250. Meta Op S $age = 2 S- 18; # ==

    -16
  251. Meta Op S $age = 2 S- 18; # actually

    error
  252. Meta Op S $age = 2 S- 18; # don't

    parallel !!!
  253. Meta Op S $age = 2 S- 18; # later

    important
  254. Meta Ops = ! X Z R S [] [\]

    << >>
  255. Meta Op X

  256. Let's Remember say <1 2> X <a b> 1 a

    1 b 2 a 2 b
  257. Let's Remember <1 2> X <a b> <1 a>,<1 b>,<2

    a>,<2 b>
  258. Cartesian Product <1 2> X <a b> ('1','a'),('1','b'),('2','a'),('2','b')

  259. Cartesian Pairs <1 2> X~ <a b> '1a','1b','2a','2b'

  260. no num out of 'a' <1 2> X+ <a b>

    Stacktrace
  261. Cartesian Pairs <1 2> X* <3 4>

  262. Cartesian Pairs <1 2> X* <3 4> 3, 4, 6,

    8
  263. Meta Op Z # guess what ?

  264. Metaop Z <1 2> Z~ <3 4>

  265. Metaop Z <1 2> Z~ <3 4> '13','24'

  266. Metaop Z <1 2> Z* <3 4> 3, 8

  267. Metaop Z (<1 2>;<3 4>).zipwith(&[*]) <1 2> Z* <3 4>

  268. Metaop (<1 2>;<3 4>).zip() <1 2> Z <3 4>

  269. Metaop (<1 2>;<3 4>).cross() <1 2> X <3 4>

  270. Metaop (<1 2>;<3 4>).crosswith(&[*]) <1 2> X* <3 4>

  271. Meta Ops = ! X Z R S [] [\]

    << >>
  272. Meta Op []

  273. Do it like Gauss (1..100).reduce(&[+])

  274. Forces List Context (1..100).reduce(&[+]) [+] 1 .. 100

  275. Forces List Context True [<] 1 .. 100

  276. Any Clue? (1..100).triangle(&[+]) [\+] 1 .. 100

  277. What's that ? 1, 3, 6 [\+] 1 .. 3

  278. What's that ? 1=1, 1+2=3, 1+2+3=6 [\+] 1 .. 3

  279. Hyper Op <<

  280. Birthday !!! @age >>++;

  281. Birthday !!! @age >>+=>> 1;

  282. all get older @age == 18, 22, 35; @age =

    @age >>+>> 1; @age == 19, 23, 36;
  283. only one gets older @age == 18, 22, 35; @age

    = @age <<+<< 1; @age == 19;
  284. interesting cases <18, 22, 35> >>+<< <1, 2> <18, 22,

    35> <<+>> <1, 2>
  285. interesting cases <18, 22, 35> >>+<< <1, 2> ERROR <18,

    22, 35> <<+>> <1, 2> 19, 24, 36
  286. complexity ++ ~~ not today

  287. Thank You !!! vv