Skip to content

Conversation

@jakemh
Copy link
Owner

@jakemh jakemh commented Jan 14, 2014

This provides order of operations and sub-expressions. It works by passing a function wrapped in a proc into the outer call. Example:
((java & (lisp - assembly) ) - (ruby + python) )
or
difference(intersection(java, difference(lisp, assembly)), union(ruby, python))

would be written as:

difference( lambda{ |opt| intersection(java, 
                lambda{ |opt| difference(lisp, assembly), opt) },
            lambda{ |opt| union(ruby, python, opt)}
            )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on abstracting some of these more complicated checks into private helper methods? That usually helps as a reminder of what the check is actually doing, and avoid nested ifs.

e.g.:

private

def self.foo?(return_list, ref, opt) # use a better method name
  return_list.length < opt[:accumulate] && ref >= opt[:until_up_to]
end

def self.bar?(return_list, ref, opt)
  return_list[-1] != ref || opt[:include_duplicates]
end

then this block of code can just be:

if foo? && bar?
  count[0] += 1
  ...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(feel free to do this with some of the other if statements in the code as well... it makes everything more readable especially when you go back later imo)

@jcfrancisco
Copy link
Collaborator

One thing you'll wanna do is replace the ands and ors with &&s and ||s. and and or might seem to work the same way, but sometimes you'll get tripped up if you use em. http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice substitute for foo != nil in rails is foo.present?

edit: never mind, forgot we weren't in rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants