Upcoming SlideShare
×

Discovering joy

546 views

Published on

Slides for Ruby Manor 4 talk

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
546
On SlideShare
0
From Embeds
0
Number of Embeds
81
Actions
Shares
0
3
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)
• 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