Last active
March 11, 2016 10:34
-
-
Save mythz/7e263820332a0fcea766 to your computer and use it in GitHub Desktop.
Type Inference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//C# | |
public void Linq43() | |
{ | |
List<Customer> customers = GetCustomerList(); | |
var customerOrderGroups = | |
from c in customers | |
select | |
new | |
{ | |
c.CompanyName, | |
YearGroups = | |
from o in c.Orders | |
group o by o.OrderDate.Year into yg | |
select | |
new | |
{ | |
Year = yg.Key, | |
MonthGroups = | |
from o in yg | |
group o by o.OrderDate.Month into mg | |
select new { Month = mg.Key, Orders = mg } | |
} | |
}; | |
ObjectDumper.Write(customerOrderGroups, 3); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dart | |
linq43(){ | |
var customers = customersList(); | |
var customerOrderGroups = customers | |
.map((c) => { | |
'CompanyName': c.companyName, | |
'YearGroups': group(c.orders, by:(o) => o.orderDate.year) | |
.map((yg) => { | |
'Year': yg.key, | |
'MonthGroups': group(yg, by:(o) => o.orderDate.month) | |
.map((mg) => { 'Month': mg.key, 'Orders': mg }) | |
}) | |
}); | |
customerOrderGroups.forEach(print); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# elixir | |
test "linq43: GroupBy - Nested" do | |
customers = get_customer_list() | |
customer_order_groups = customers | |
|> Enum.map(fn c -> | |
%{ | |
company_name: c.name, | |
year_groups: | |
c.orders | |
|> Enum.group_by(fn o -> o.orderdate.year end) | |
|> Enum.map(fn {year, year_orders} -> | |
%{ | |
year: year, | |
month_groups: | |
year_orders | |
|> Enum.group_by(fn o -> o.orderdate.month end) | |
|> Enum.map(fn {month, month_orders} -> | |
%{ | |
month: month, | |
orders: month_orders | |
} | |
end) | |
} | |
end) | |
} | |
end) | |
IO.inspect customer_order_groups, limit: 10, pretty: true | |
assert 91 == length(customer_order_groups) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//java | |
public void linq43(){ | |
List<Customer> customers = getCustomerList(); | |
List<Tuple<String, ArrayList<Tuple<Integer, ArrayList<Group<Integer, Order>>>>>> customerOrderGroups = | |
map(customers, new Function<Customer, Tuple<String, ArrayList<Tuple<Integer, ArrayList<Group<Integer, Order>>>>>>() { | |
@Override | |
public Tuple<String, ArrayList<Tuple<Integer, ArrayList<Group<Integer, Order>>>>> apply(Customer c) { | |
return new Tuple<>( //Yay Type Inference! | |
c.companyName, | |
map(groupBy(c.orders, new Function<Order, Integer>() { | |
@Override | |
public Integer apply(Order o) { | |
return o.orderDate.getYear() + 1900; | |
} | |
}), | |
new Function<Group<Integer, Order>, Tuple<Integer, ArrayList<Group<Integer, Order>>>>() { | |
@Override | |
public Tuple<Integer, ArrayList<Group<Integer, Order>>> apply(Group<Integer, Order> yg) { | |
return new Tuple<>( //Yay Type Inference! | |
yg.key, | |
groupBy(yg.items, new Function<Order, Integer>() { | |
@Override | |
public Integer apply(Order o) { | |
return o.orderDate.getMonth() + 1; | |
} | |
}) | |
); | |
} | |
} | |
) | |
); | |
} | |
}); | |
for (Tuple<String, ArrayList<Tuple<Integer, ArrayList<Group<Integer, Order>>>>> g : customerOrderGroups){ | |
Log.d("\n# " + g.A); | |
for (Tuple<Integer, ArrayList<Group<Integer, Order>>> yg : g.B){ | |
Log.d(yg.A + ": "); | |
for (Group<Integer, Order> mg : yg.B){ | |
Log.d(" " + mg.key + ": "); | |
for (Order o : mg){ | |
Log.d(" " + o); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//kotlin | |
fun linq43() { | |
val customerOrderGroups = customers.map { c -> | |
Pair(c.companyName, | |
c.orders.groupBy { it.orderDate.year + 1900 } | |
.map { Pair(it.key, it.value.groupBy { it.orderDate.month + 1 }) }) | |
} | |
customerOrderGroups.forEach { | |
val (companyName, yearGroups) = it | |
Log.d("\n# $companyName") | |
yearGroups.forEach { | |
val (year,monthGroups) = it | |
Log.d("${year.toString()}: ") | |
monthGroups.forEach { Log.d(" $it") } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//swift | |
func linq43(){ | |
let customers = customersList() | |
typealias Year = Int | |
typealias Month = Int | |
let customerOrderGroups = customers.map { c | |
-> (CompanyName:String, YearGroups:(Year:Int, MonthGroups:Group<Year, Order>[])[]) | |
in | |
(c.companyName, | |
c.orders.groupBy { o in o.orderDate!.getYear() } | |
.map {(yg:Group<Month,Order>) in | |
(yg.key, | |
yg.items.groupBy { (o:Order) in o.orderDate!.getMonth() }) | |
}) | |
} | |
customerOrderGroups.each { | |
println("\n# \($0.CompanyName)") | |
$0.YearGroups.each { yg in | |
println("\(yg.Year): ") | |
yg.MonthGroups.each { println(" \($0)") } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment