Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
PIPE OPERATOR
For Ruby
@akitaonrails
Rubyconf Brasil 2016 – September 23 - 24
Rubyconf Brasil 2016 – September 23 - 24
September
23 - 24
@rubyconfbr
PIPE OPERATOR
For Ruby
@akitaonrails
Elixir Pipe
Operator
require	Integer	
1..100_000	
		|>	Stream.map(&(&1	*	3))	
		|>	Stream.filter(&(Integer.is_odd(&1)))	
		|>	Enum.sum
require	Integer	
1..100_000	
		|>	Stream.map(&(&1	*	3))	
		|>	Stream.filter(&(Integer.is_odd(&1)))	
		|>	Enum.sum
Enum.sum...
[1..10000]	
		|>	List.map	(fun	x	->	x	*	3)	
		|>	List.filter	(fun	x	->	x	%	2	=	0)	
		|>	List.reduce	(fun	x	y	->	x	+	y)
Lis...
Feature
Envy
(1..100_000)	
		.map	{	|x|	x	*	3	}	
		.select(&:odd?)	
		.reduce(&:+)
Linux Piping and
Redirection
netstat	-rn		
		|	awk	'/default/	{	print	$NF	}'		
		|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
netstat	-rn		
		|	awk	'/default/	{	print	$NF	}'		
		|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
netstat	-rn		
		|	awk	'/default/	{	print	$NF	}'		
		|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
netstat	-rn		
		|	awk	'/default/	{	print	$NF	}'		
		|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
netstat	-rn		
		|	awk	'/default/	{	print	$NF	}'		
		|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
netstat	-rn		
		|	awk	'/default/	{	print	$NF	}'		
		|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
|	head	-1	|	xargs	-I	{}		ifconfig	{}		
		|	awk	'/ether/	{print	$2}'
Foo.run_cmd("-rn",	"netstat")	
		|>	String.split("n")	...
|	awk	'/ether/	{print	$2}'
Foo.run_cmd("-rn",	"netstat")	
		|>	String.split("n")	
		|>	Enum.filter(&(String.match?(&1,	~r/...
Foo.run_cmd("-rn",	"netstat")	
		|>	String.split("n")	
		|>	Enum.filter(&(String.match?(&1,	~r/default/)))	
		|>	Enum.map(...
Quick
Prototyping
Foo.run_cmd("-rn",	"netstat")	
		|>	String.split("n")	
		|>	Enum.filter(&(String.match?(&1,	~r/default/)))	
		|>	Enum.map(...
`netstat	-rn`	
		.split("n")	
		.select	{	|x|	x	=~	/default/	}	
		.map	{	|x|	x.split("	")	}	
		.map	{	|x|	x.last	}	
		.fir...
`netstat	-rn`	
		.split("n")	
		.select	{	|x|	x	=~	/default/	}	
		.map	{	|x|	x.split("	")	}	
		.map	{	|x|	x.last	}	
		.fir...
x	=	`netstat	-rn`	
		.split("n")	
		.select	{	|x|	x	=~	/default/	}	
		.map	{	|x|	x.split("	")	}	
		.map	{	|x|	x.last	}	
		...
CM(`netstat	-rn`)	
		.split("n")	
		.select	{	|x|	x	=~	/default/	}	
		.map	{	|x|	x.split("	")	}	
		.map	{	|x|	x.last	}	
		...
CM(`netstat	-rn`)	
		.split("n")	
		.select	{	|x|	x	=~	/default/	}	
		.map	{	|x|	x.split("	")	}	
		.map	{	|x|	x.last	}	
		...
#	Gemfile	
gem	"chainable_methods",	"~>	0.2.1"
DATA
TRANSFORMATION
WORKFLOWS
Avoid
Temporary
Variables
text		=	"hello	http:///www.goruco.com	world"	
url			=	URI.extract(text).first	
uri			=	URI.parse(url)	
body		=	open(uri).r...
CM("hello	http:///www.goruco.com	world")	
		.chain	{	|text|	URI.extract(text).first	}	
		.chain	{	|url|		URI.parse(url)	}	...
CM("hello	http:///www.goruco.com	world")	
		.URI.extract.first	
		.URI.parse	
		.chain	{	|uri|	open(uri).read	}	
		.HTML.p...
THANKS!
www.codeminer42.com
@akitaonrails
Goruco 2016 - Pipe Operator for Ruby
Goruco 2016 - Pipe Operator for Ruby
Goruco 2016 - Pipe Operator for Ruby
Goruco 2016 - Pipe Operator for Ruby
Goruco 2016 - Pipe Operator for Ruby
Goruco 2016 - Pipe Operator for Ruby
Upcoming SlideShare
Loading in …5
×

Goruco 2016 - Pipe Operator for Ruby

735 views

Published on

This is the brief explanation of evolving a Elixir-like Pipe Operator semantics for Ruby in a way that feels the most natural. Check out the https://github.com/akitaonrails/chainable_methods to use it.

Published in: Technology

Goruco 2016 - Pipe Operator for Ruby

  1. 1. PIPE OPERATOR For Ruby @akitaonrails
  2. 2. Rubyconf Brasil 2016 – September 23 - 24
  3. 3. Rubyconf Brasil 2016 – September 23 - 24
  4. 4. September 23 - 24 @rubyconfbr
  5. 5. PIPE OPERATOR For Ruby @akitaonrails
  6. 6. Elixir Pipe Operator
  7. 7. require Integer 1..100_000 |> Stream.map(&(&1 * 3)) |> Stream.filter(&(Integer.is_odd(&1))) |> Enum.sum
  8. 8. require Integer 1..100_000 |> Stream.map(&(&1 * 3)) |> Stream.filter(&(Integer.is_odd(&1))) |> Enum.sum Enum.sum(Enum.filter(Enum.map(1..100_000, &(&1 * 3)), &(Integer.is_odd(&1))))
  9. 9. [1..10000] |> List.map (fun x -> x * 3) |> List.filter (fun x -> x % 2 = 0) |> List.reduce (fun x y -> x + y) List.reduce (fun x y -> x + y) (List.filter (fun x -> x % 2 = 0) (List.map (fun x -> x * 3) [1..10000]))
  10. 10. Feature Envy
  11. 11. (1..100_000) .map { |x| x * 3 } .select(&:odd?) .reduce(&:+)
  12. 12. Linux Piping and Redirection
  13. 13. netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}'
  14. 14. netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}'
  15. 15. netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}'
  16. 16. netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}'
  17. 17. netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}'
  18. 18. netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}'
  19. 19. | head -1 | xargs -I {} ifconfig {} | awk '/ether/ {print $2}' Foo.run_cmd("-rn", "netstat") |> String.split("n") |> Enum.filter(&(String.match?(&1, ~r/default/))) |> Enum.map(&(String.split(&1))) |> Enum.map(&(List.last(&1)))
  20. 20. | awk '/ether/ {print $2}' Foo.run_cmd("-rn", "netstat") |> String.split("n") |> Enum.filter(&(String.match?(&1, ~r/default/))) |> Enum.map(&(String.split(&1))) |> Enum.map(&(List.last(&1))) |> hd |> Foo.run_cmd("ifconfig")
  21. 21. Foo.run_cmd("-rn", "netstat") |> String.split("n") |> Enum.filter(&(String.match?(&1, ~r/default/))) |> Enum.map(&(String.split(&1))) |> Enum.map(&(List.last(&1))) |> hd |> Foo.run_cmd("ifconfig") |> String.split("n") |> Enum.filter(&(String.match?(&1, ~r/ether/))) |> List.first |> String.split |> List.last
  22. 22. Quick Prototyping
  23. 23. Foo.run_cmd("-rn", "netstat") |> String.split("n") |> Enum.filter(&(String.match?(&1, ~r/default/))) |> Enum.map(&(String.split(&1))) |> Enum.map(&(List.last(&1))) |> hd |> Foo.run_cmd("ifconfig") |> String.split("n") |> Enum.filter(&(String.match?(&1, ~r/ether/))) |> List.first |> String.split |> List.last
  24. 24. `netstat -rn` .split("n") .select { |x| x =~ /default/ } .map { |x| x.split(" ") } .map { |x| x.last } .first |> { |card| `ifconfig #{card}` } .split("n") .select { |x| x =~ /ether/ } .first.split(" ").last
  25. 25. `netstat -rn` .split("n") .select { |x| x =~ /default/ } .map { |x| x.split(" ") } .map { |x| x.last } .first |> { |card| `ifconfig #{card}` } .split("n") .select { |x| x =~ /ether/ } .first.split(" ").last
  26. 26. x = `netstat -rn` .split("n") .select { |x| x =~ /default/ } .map { |x| x.split(" ") } .map { |x| x.last } .first `ifconfig #{x}` .split("n") .select { |x| x =~ /ether/ } .first.split(" ").last
  27. 27. CM(`netstat -rn`) .split("n") .select { |x| x =~ /default/ } .map { |x| x.split(" ") } .map { |x| x.last } .first.chain { |card| `ifconfig #{card}` } .split("n") .select { |x| x =~ /ether/ } .first.split(" ").last .unwrap
  28. 28. CM(`netstat -rn`) .split("n") .select { |x| x =~ /default/ } .map { |x| x.split(" ") } .map { |x| x.last } .first.chain { |card| `ifconfig #{card}` } .split("n") .select { |x| x =~ /ether/ } .first.split(" ").last .unwrap
  29. 29. # Gemfile gem "chainable_methods", "~> 0.2.1"
  30. 30. DATA TRANSFORMATION WORKFLOWS
  31. 31. Avoid Temporary Variables
  32. 32. text = "hello http:///www.goruco.com world" url = URI.extract(text).first uri = URI.parse(url) body = open(uri).read title = Nokogiri::HTML(body).css(“h1") .first.text.strip
  33. 33. CM("hello http:///www.goruco.com world") .chain { |text| URI.extract(text).first } .chain { |url| URI.parse(url) } .chain { |uri| open(uri).read } .chain { |body| Nokogiri::HTML(body).css("h1") } .first.text.strip .unwrap
  34. 34. CM("hello http:///www.goruco.com world") .URI.extract.first .URI.parse .chain { |uri| open(uri).read } .HTML.parse .css("h1").first.text.strip .unwrap include Nokogiri
  35. 35. THANKS! www.codeminer42.com @akitaonrails

×