Chapter 1

  1. A closure is a function whose body references some variable that is declared in a parent scope.

  2. amount is the free variable. This is because amount is not defined in the lambda, but rather, as is_larger_than’s argument.

  3. Here’s one possible implementation:

     new_db = lambda ​do
      db = {}
     
      insert = lambda ​do​ |artiste, album|
      db[artist] = album
     end
     
      dump = lambda ​do
      db
     end
     
      delete = lambda ​do​ |artiste|
      old = db[artiste]
      db[artiste] = ​nil
      old
     end
     
      {​:insert​ => insert, ​:dump​ => dump, ​:delete​ => delete}
     end
  4.  complement = lambda ​do​ |pred|
      lambda ​do​ |args|
      not pred.call(args)
     end
     end
  5. The main trick here is that the starting value can be anything you want it to be, including an array. In this case, the lambda becomes a mapping function:

     reducer.call([], lambda { |acc, x| acc << x * 2 }, [1, 2, 3, 4, 5])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.249.220