"https://api.github.com/repos/ruby/ruby".pipe do
URI.parse
Net::HTTP.get
JSON.parse.fetch("stargazers_count")
yield_self { |n| "Ruby has #{n} stars" }
Kernel.puts
end
#=> Ruby has 15115 stars
Not having to type the |> like in Elixir is two shift keys less, which is good. I'm not sure about readability, because one has to spot the .pipe at the beginning of the block, but it shouldn't be a problem.
Now, if we only had pattern matching with the exact syntax of Elixir and not some monstrosity I saw around in proposals and other languages...
I think this same type of concept could be applied to port Elixir style pattern matching as well e.g.
data = {
name: "John Smith",
age: 35,
prefs: {
lang: "en",
tz: "UTC",
}
}
User = Pattern { name age prefs[lang] }
user = User =~ data
user.name
user.age
user.lang
[data].map(&User)
case object
when Pattern { some attrs[:nested][real][deep, fields] }
Pattern!.some
Pattern!.real
Pattern!.deep
Pattern!.fields
Pattern!.nested #=> NoMethodError
end
# or define "locals" by defining temporary methods on
# the block receiver when the "then" block is evaluated
case object
when Pattern { some nested[data] }.then do
puts some
puts data
end
[+] [-] pmontra|7 years ago|reply
Now, if we only had pattern matching with the exact syntax of Elixir and not some monstrosity I saw around in proposals and other languages...
[+] [-] bonquesha99|7 years ago|reply
Check out this other proof of concept demonstrating ES6 style object destructuring in Ruby:
https://github.com/lendingHome/destruct
I think this same type of concept could be applied to port Elixir style pattern matching as well e.g.