Your SlideShare is downloading. ×
0
on Discovering Joy    Francis Fish (@fjfish)
Fish?NOT COMPUTER SCIENTISTLIKE PROGRAMMINGLEARN NEW LANGUAGE LONG TIMENOWKNOW NOTHING, YOU MUCH CLEVERERTHAN MEDISCOVERIN...
Blame BraithwaiteAppendix Finding joy in combinators
Combinators
Combinator?http://en.wikipedia.org/wiki/CombinatorA combinator is a higher-order function that usesonly function applicati...
Joy Resourceshttp://en.wikipedia.org/wiki/Joy_(programminInvented by Manfred von Thun of La TrobeUniversity in Melbourne, ...
InterestingProgram is data (like Lisp)Stack based - no parametersPost-fix (goes with stack)Does functional stuff wellCombi...
Stack Based?Let’s go see
Example: Square5 dup * .- will print 25 and leave the stack empty(as a function)square == dup *(== is the only infix opera...
WAT?
Parameters, NoneA stack of combinatorsCode acts on the stackdup *Copy it and multiply the top 2 elementstogether
Cube - you tellme :)
5 dup dup * *.5#5dup # 5 5dup # 5 5 5* # 5 25* # 125125
ProgramsJoy programs are built from smaller programs byjust two operations: concatenation and quotation.
Quoted Programs[bob alice]Is a list, but also a quoted programYou can plug it into things and then executeBuilt in functio...
Typescharacter, file, float, integer, list, set, string,truthWhat else do you need?(Except maybe hash)And sets are weird
Combinatorsifte - if then elseThe ifte combinator expects three quoted programs on the stack, an if-part, a then-part and ...
CombinatorsconcatJoin two aggregates togethercons - lispAdd the top thing to the aggregate behind it on the stackdupDuplic...
CombinatorsNot talking about:construct, dupd, map, popd, rolldown, rolldownd,rollup, rollupd, rotate, succ, pred, swap, sw...
Example:FactorialFactorial
Linear Recursion5 [null] [succ] [dup pred] [*] linrec .["null" false]      linrec   : [P] [T] [R1] [R2] -> ....Executes P....
Primitive Recursion5 [1] [*] primrec .["R0" 1 1 2 3 4 5]["*" 1 2 3 4 5]       primrec       : X [I] [C] -> R.Executes I to...
Y-Combinator5[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ][dup cons] swap concat dup cons i
Explanation5    Cond        Else            Then[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ][dup cons] swap concat d...
If the top of the stack is 0 discard the copied program and the 0 left andpush 1else duplicate the top of the stack and su...
[dup cons] swap concat program and the integer on(We already have the quoted dup cons ithe top of the stack)(We push a quo...
[1 false 4 5][3 2 3 4 5]             Just for laffs[6 [[dup cons [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte] dup cons ...
What Have I discovered?
Ruby PracticeHow do we do recursion? We can see the stack, this reminds us of things we had forgottenRecursion can always ...
Ruby PracticeProgram is data? Method missing/symbols Dispatch tables etc. ERB - macro languages
THings to think aboutCan we use stacks and combinators to solveproblems more easily?Can we create DSL’s that do this?Would...
Building a web app    Who needs HAML when we could have:[    [     [        ["Hello" div]      ] body     ]     [      [  ...
Next?http://programjoy.eu - £1.40 :)Will front a runtime and links to the mirrorWill run the tutorialIf you want to help -...
Finis/Questions   Francis Fish (@fjfish)   www.francisfish.com  www.leanpub.com/fjfish
Upcoming SlideShare
Loading in...5
×

Discovering joy

298

Published on

Slides for Ruby Manor 4 talk

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

  • Be the first to like this

No Downloads
Views
Total Views
298
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Brief aside - combinator names come from the puzzle book. First order logic is the Mathematics professor version. Birds are a tribute to Haskell Curry’s being a twitcher.
  • Forth small processors, lots of history, build language up from primitives and down from domain and meet in the middle - just like with Ruby DSL’s
  • 5 dup dup * * .
  • [P] [T] [R1] [R2] If P is true T else Call R1 with self R2
  • If the data parameter is zero, then the first quotation has to produce the value to be returned. If the data parameter is positive then the second has to combine the data parameter with the result of applying the function to its predecessor.
  • UNPICK ON NEXT SLIDE - don’t jump the gun
  • HAML marvy - JS Frameworks etc. Quote lisp easier (maybe)
  • Transcript of "Discovering joy"

    1. 1. on Discovering Joy Francis Fish (@fjfish)
    2. 2. Fish?NOT COMPUTER SCIENTISTLIKE PROGRAMMINGLEARN NEW LANGUAGE LONG TIMENOWKNOW NOTHING, YOU MUCH CLEVERERTHAN MEDISCOVERING JOY - NOT EXPERT!
    3. 3. Blame BraithwaiteAppendix Finding joy in combinators
    4. 4. Combinators
    5. 5. Combinator?http://en.wikipedia.org/wiki/CombinatorA combinator is a higher-order function that usesonly function application and earlier definedcombinators to define a result from its arguments.(no parameters, functions as arguments)
    6. 6. Joy Resourceshttp://en.wikipedia.org/wiki/Joy_(programminInvented by Manfred von Thun of La TrobeUniversity in Melbourne, Australia (retired)Purely functional, combinatorial, stack basedMirrorhttp://www.kevinalbrecht.com/code/joy-mirror
    7. 7. InterestingProgram is data (like Lisp)Stack based - no parametersPost-fix (goes with stack)Does functional stuff wellCombinators - take what’s on the stack and dostuff - including combining other combinatorsNot Forth - no dictionary - functional
    8. 8. Stack Based?Let’s go see
    9. 9. Example: Square5 dup * .- will print 25 and leave the stack empty(as a function)square == dup *(== is the only infix operator - not runtime)
    10. 10. WAT?
    11. 11. Parameters, NoneA stack of combinatorsCode acts on the stackdup *Copy it and multiply the top 2 elementstogether
    12. 12. Cube - you tellme :)
    13. 13. 5 dup dup * *.5#5dup # 5 5dup # 5 5 5* # 5 25* # 125125
    14. 14. ProgramsJoy programs are built from smaller programs byjust two operations: concatenation and quotation.
    15. 15. Quoted Programs[bob alice]Is a list, but also a quoted programYou can plug it into things and then executeBuilt in functions, anonymous Y combinatoretc.Fill in the blanks
    16. 16. Typescharacter, file, float, integer, list, set, string,truthWhat else do you need?(Except maybe hash)And sets are weird
    17. 17. Combinatorsifte - if then elseThe ifte combinator expects three quoted programs on the stack, an if-part, a then-part and an else-part,in that order, with the else-part on topdipdip combinator expects a program on top of the stack and below that another value. It saves the value,executes the program on the remainder of the stack and then restores the saved value.iidentity - run the quoted program on the top of the stack
    18. 18. CombinatorsconcatJoin two aggregates togethercons - lispAdd the top thing to the aggregate behind it on the stackdupDuplicate the stack topswapcause a flight of penguins
    19. 19. CombinatorsNot talking about:construct, dupd, map, popd, rolldown, rolldownd,rollup, rollupd, rotate, succ, pred, swap, swapd,swons, times, take, ternary, treegenrec, unswons,x, first ...
    20. 20. Example:FactorialFactorial
    21. 21. Linear Recursion5 [null] [succ] [dup pred] [*] linrec .["null" false] linrec : [P] [T] [R1] [R2] -> ....Executes P. If that yields true, executes T.Else execu["dup pred" 4 5]["null" false 5]["null" false 3 4 5]["*" 6 4 5]...["*" 120]["linrec" 120]120
    22. 22. Primitive Recursion5 [1] [*] primrec .["R0" 1 1 2 3 4 5]["*" 1 2 3 4 5] primrec : X [I] [C] -> R.Executes I to obtain["*" 2 3 4 5] an initial value R0.For integer X uses increasing positive integers to X, combines by C for new["*" 6 4 5] R.For aggregate X uses successive members and["*" 24 5] combines by C for new R.["*" 120]120(HINT: it’s not recursion)
    23. 23. Y-Combinator5[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ][dup cons] swap concat dup cons i
    24. 24. Explanation5 Cond Else Then[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ][dup cons] swap concat dup cons i
    25. 25. If the top of the stack is 0 discard the copied program and the 0 left andpush 1else duplicate the top of the stack and subtract 1 fromthe top valuethen dip and call the copy of yourself underneaththe stack topon return apply * * the value below with the one you calculated
    26. 26. [dup cons] swap concat program and the integer on(We already have the quoted dup cons ithe top of the stack)(We push a quoted dup cons onto the stack)run whats comes out of:Take the stack top and cons to aggregate underneathdup the aggregateconcat them together swap with whatever’s on the stacktopWe end up with the previous quoted program on the stacktop and run it
    27. 27. [1 false 4 5][3 2 3 4 5] Just for laffs[6 [[dup cons [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte] dup cons [pop 0 =] [pop pop 1][[dup 1 -] dip i *] ifte] 2 3 4 5][1 false 3 4 5][3 1 2 3 4 5][6 [[dup cons [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte] dup cons [pop 0 =] [pop pop 1][[dup 1 -] dip i *] ifte] 1 2 3 4 5][1 false 2 3 4 5][3 0 1 2 3 4 5][6 [[dup cons [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte] dup cons [pop 0 =] [pop pop 1][[dup 1 -] dip i *] ifte] 0 1 2 3 4 5][1 true 1 2 3 4 5][2 1 1 2 3 4 5][5 1 1 2 3 4 5]
    28. 28. What Have I discovered?
    29. 29. Ruby PracticeHow do we do recursion? We can see the stack, this reminds us of things we had forgottenRecursion can always be recast as iteration Quick poll - how many people know this? How many had forgotten?Can make difficult problems more tractable
    30. 30. Ruby PracticeProgram is data? Method missing/symbols Dispatch tables etc. ERB - macro languages
    31. 31. THings to think aboutCan we use stacks and combinators to solveproblems more easily?Can we create DSL’s that do this?Would we want to :)
    32. 32. Building a web app Who needs HAML when we could have:[ [ [ ["Hello" div] ] body ] [ [ [“My App” title] [“main.js” script] ] head] ] html
    33. 33. Next?http://programjoy.eu - £1.40 :)Will front a runtime and links to the mirrorWill run the tutorialIf you want to help - contact meMore fun to work on rather than <corporate-bs>next big ecommerce social doo dahsite</corporate-bs>
    34. 34. Finis/Questions Francis Fish (@fjfish) www.francisfish.com www.leanpub.com/fjfish
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×