PIPE OPERATORFor Ruby
@akitaonrails
Rubyconf Brasil 2016 – September 23 - 24
Rubyconf Brasil 2016 – September 23 - 24
September
23 - 24 @rubyconfbr
PIPE OPERATORFor Ruby
@akitaonrails
Elixir Pipe Operator
requireInteger1..100_000|>Stream.map(&(&1*3))|>Stream.filter(&(Integer.is_odd(&1)))|>Enum.sum
requireInteger1..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))))
[1..10000]|>List.map(funx->x*3)|>List.filter(funx->x%2=0)|>List.reduce(funxy->x+y)
List.reduce(funxy->x+y)(List.filter(funx->x%2=0)(List.map(funx->x*3)[1..10000]))
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")|>Enum.filter(&(String.match?(&1,~r/default/)))|>Enum.map(&(String.split(&1)))|>Enum.map(&(List.last(&1)))
|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")
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
Quick Prototyping
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
`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
`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
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
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
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
#Gemfilegem"chainable_methods","~>0.2.1"
DATA TRANSFORMATION
WORKFLOWS
Avoid Temporary Variables
text="hellohttp:///www.goruco.comworld"url=URI.extract(text).firsturi=URI.parse(url)body=open(uri).readtitle=Nokogiri::HTML(body).css(“h1").first.text.strip
CM("hellohttp:///www.goruco.comworld").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
CM("hellohttp:///www.goruco.comworld").URI.extract.first.URI.parse.chain{|uri|open(uri).read}.HTML.parse.css("h1").first.text.strip.unwrap
includeNokogiri