Why use a lambda here instead of the two predefined methods?

def divideset(rows, column, value)
split_function = nil

if value.is_a?(Fixnum) || value.is_a?(Float)
  split_function = lambda{|row| row[column] >= value}
else
  split_function = lambda{|row| row[column] == value}
end

set1 = rows.select{|row| split_function.call(row)}
set2 = rows.reject{|row| split_function.call(row)}
[set1, set2]
end

      

In this treepredict code , why use lambdas?

It seems that instead of being called, the split_function.call(row)

author could have predefined two different methods to handle the two conditions of the if / else statement: one for the case when row[column] >= value

, and the other for the case whenrow[column] == value

Is there an added benefit here from using a lambda?

+2


source to share


4 answers


In this case, there is no additional technical advantage for using lambda.

Consider your proposal:



def split_function_default(row, column, value)
  row[column] == value
end

def split_function_number(row, column, value)
  row[column] >= value
end

def divideset(rows, column, value)
  set1 = nil
  set2 = nil
  if value.is_a?(Fixnum) || value.is_a?(Float)
    set1 = rows.select{|row| split_function_number(row, column, value)}
    set2 = rows.reject{|row| split_function_number(row, column, value)}
  elif
    set1 = rows.select{|row| split_function_default(row, column, value)}
    set2 = rows.reject{|row| split_function_default(row, column, value)}
  end
  [set1, set2]
end

      

Which is more understandable? I personally prefer the lambda version as it is more concise.

+3


source


You can predefine these 2 functions and then use them.



But the use of lambda in this case is very clear, since these are really trivial functions, and their scope is limited only in divideset

. If some other functions also use some functions, it is better to use a predefined function to follow the DRY principle.

+2


source


Just sign up for the record.

split_function = lambda {|row| do_stuff_with(row) }

// don't do this
rows.select{|row| split_function.call(row)}

// do this
rows.select(&split_function)

      

+1


source


In a way, it's just style, but every bit helps

While in one view it is just style, in another it shows the ability of a functional language to get the same job as when working with fewer peonies. If we weren't worried about verbosity and awkwardness, we could just write everything in Java ...

Here are some pretty minor "style" options that come to mind:

def divideset(rows, column, value)
  split_function = value.is_a?(Fixnum) || value.is_a?(Float) ?
      lambda {|row| row[column] >= value} :
      lambda {|row| row[column] == value}

  [ rows.select{|row| split_function.call(row)},
    rows.reject{|row| split_function.call(row)} ]
end

      


def divideset(rows, column, value)
  split_function =
    if value.is_a?(Fixnum) || value.is_a?(Float) 
      lambda {|row| row[column] >= value} 
    else
      lambda {|row| row[column] == value}
    end

  [ rows.select{|row| split_function.call(row)},
    rows.reject{|row| split_function.call(row)} ]
end

      

I may have played too much code golf .

0


source







All Articles