Ruby, Go and Haskell Matt Walston University of North Florida
Ruby • Everything’s an object • Dynamic reflection and meta programming • Duck typing • Blocks and Iterators • Fibers
Everything an Object • All inherit from BasicObject • Ancestor chains can be long • Examples? • nil.class # => NilClass • 3.class # => FixNum • 4.method(:abs).class # => Method • Fixnum.ancestors # => Integer, Numeric, Comparable, Object, Kernel, BasicObject
Metaprogramming Features • eval • const_set • instance_eval • const_get • class_eval • Class.new • class_variable_set • binding • class_variable_get • send • class_variables • remove_method • instance_variable_set • undef_method • define_method • method_missing
Reflection Examples • Pretty Print • Sinatra • ActiveRecord
Type System • No primitives • Dynamic • Duck • Type conversion
Iteration • Avoid loops, create iterator and pass block • each method, each_with_index • Enumerators
Blocks, Proc and lambda • Blocks • Reusability • Syntax • Proc vs lambda • Example?
Fibers • Lightweight alternative to threads • Low level • EventMachine adds capability • Example?
Killer App?
Go • Based on C • Simple and concise by design • Goroutines and channels • Garbage collection • Type checking
Based on C • Google’s knowledge base and on-boarding • Curly braces • Simple programs are easy to port, Complex programs are difficult • Use features you know and like
Simple and Concise • Only one loop— for • Type inference— count:= 9 • Go range iterate arrays, slices, strings and maps • Anonymous functions • Multiple returns
Goroutines • Concurrency mechanism, not parallelism • Multiple processors = parallel execution • Not distributed, same OS, same address space • Much simpler than threads • Simple call function with go keyword
Channels • Important for go routines • Ability to pass messages back • Example?
Garbage Collection • Early source of problems • Not constant time • Beats JVM— hands down with dynamic langs
Types • Statically typed, appears dynamic • Interfaces are not explicitly typed to class, Similar to duck typing • Type safety through no direct pointer access
Haskell • Purely functional, functions lack side effects • Type classes • Pattern matching • Lazy evaluation • List comprehension
Purely Functional • Lack of side effects • Concurrency • Recursion • Complete referential transparency • Accepted programs will always terminate
Lazy Evaluation • Vital to a functional language • Infinite lists
List Comprehension • Improved readability through syntactic sugar • Generators expand into lists • May lazily evaluate for countably infinite lists
Recursion • Tail call optimization less important • Minimal call stack frame size • Important due to pure functional nature • Example?
Quicksort in Haskell qs :: Ord a => [a] -> [a] qs [] = [] qs (p:xs) = (qs l) ++ [p] ++ (qs g) where l = filter (< p) xs g = filter (>= p) xs
Recommend
More recommend