Ruby, Go and Haskell Matt Walston University of North Florida Ruby - - PowerPoint PPT Presentation

ruby go and haskell
SMART_READER_LITE
LIVE PREVIEW

Ruby, Go and Haskell Matt Walston University of North Florida Ruby - - PowerPoint PPT Presentation

Ruby, Go and Haskell Matt Walston University of North Florida Ruby Everythings an object Dynamic reflection and meta programming Duck typing Blocks and Iterators Fibers Everything an Object All inherit from BasicObject


slide-1
SLIDE 1

Ruby, Go and Haskell

Matt Walston University of North Florida

slide-2
SLIDE 2

Ruby

  • Everything’s an object
  • Dynamic reflection and meta programming
  • Duck typing
  • Blocks and Iterators
  • Fibers
slide-3
SLIDE 3

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

slide-4
SLIDE 4

Metaprogramming Features

  • eval
  • instance_eval
  • class_eval
  • class_variable_set
  • class_variable_get
  • class_variables
  • instance_variable_set
  • define_method
  • const_set
  • const_get
  • Class.new
  • binding
  • send
  • remove_method
  • undef_method
  • method_missing
slide-5
SLIDE 5

Reflection Examples

  • Pretty Print
  • Sinatra
  • ActiveRecord
slide-6
SLIDE 6

Type System

  • No primitives
  • Dynamic
  • Duck
  • Type conversion
slide-7
SLIDE 7

Iteration

  • Avoid loops, create iterator and pass block
  • each method, each_with_index
  • Enumerators
slide-8
SLIDE 8

Blocks, Proc and lambda

  • Blocks
  • Reusability
  • Syntax
  • Proc vs lambda
  • Example?
slide-9
SLIDE 9

Fibers

  • Lightweight alternative to threads
  • Low level
  • EventMachine adds capability
  • Example?
slide-10
SLIDE 10

Killer App?

slide-11
SLIDE 11

Go

  • Based on C
  • Simple and concise by design
  • Goroutines and channels
  • Garbage collection
  • Type checking
slide-12
SLIDE 12

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
slide-13
SLIDE 13

Simple and Concise

  • Only one loop— for
  • Type inference— count:= 9
  • Go range iterate arrays, slices, strings and maps
  • Anonymous functions
  • Multiple returns
slide-14
SLIDE 14

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
slide-15
SLIDE 15

Channels

  • Important for go routines
  • Ability to pass messages back
  • Example?
slide-16
SLIDE 16

Garbage Collection

  • Early source of problems
  • Not constant time
  • Beats JVM— hands down with dynamic langs
slide-17
SLIDE 17

Types

  • Statically typed, appears dynamic
  • Interfaces are not explicitly typed to class,


Similar to duck typing

  • Type safety through no direct pointer access
slide-18
SLIDE 18

Haskell

  • Purely functional, functions lack side effects
  • Type classes
  • Pattern matching
  • Lazy evaluation
  • List comprehension
slide-19
SLIDE 19

Purely Functional

  • Lack of side effects
  • Concurrency
  • Recursion
  • Complete referential transparency
  • Accepted programs will always terminate
slide-20
SLIDE 20

Lazy Evaluation

  • Vital to a functional language
  • Infinite lists
slide-21
SLIDE 21

List Comprehension

  • Improved readability through syntactic sugar
  • Generators expand into lists
  • May lazily evaluate for countably infinite lists
slide-22
SLIDE 22

Recursion

  • Tail call optimization less important
  • Minimal call stack frame size
  • Important due to pure functional nature
  • Example?
slide-23
SLIDE 23

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