Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing function #32

Open
librarywebchic opened this issue Mar 17, 2016 · 1 comment
Open

Passing function #32

librarywebchic opened this issue Mar 17, 2016 · 1 comment

Comments

@librarywebchic
Copy link

I'm trying to understand what is described here in the wiki
https://github.com/ondra-m/ruby-spark/wiki/Passing-function

This seems to described an issue I'm seeing where I have a method I want to use but it isn't recognized when I call it within .map. I can't figure out a possible solution from the documentation.

Something like this doesn't work

def manipulate_data
line + " more stuff"
end

api_keys = requests_with_key.map(lambda {|line| [manipulate_data(line), 1]})

But I am unsure how I would make it work. Is it as simple as adding
requests_with_key.bind(method(:manipulate_data))

@ondra-m
Copy link
Owner

ondra-m commented Mar 19, 2016

You cannot pass more methods into map. Use:

requests_with_key.map(lambda {|line| [line + " more stuff", 1]})

Other options is use a different mapping:

func = lambda {|part| 
  def manipulate_data(line)
    line + " more stuff"
  end

  part.map do |line|
    [manipulate_data(line), 1]
  end
}
api_keys = requests_with_key.map_partitions(func)

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

No branches or pull requests

2 participants