Page 1 of 1

Map/Select, Reduce/Aggregate Functionality

PostPosted: Tue Jan 15, 2013 7:02 am
by bertpritchard
My apologies if I've missed it in the documentation. I couldn't find any references to either map or reduce functionality. Is that something that is supported?

I tried using the C# approach:
Code: Select all
nums = List<of int>({1,2,3})
sum = nums.aggregate( do(a as int, b as int) = a + b)
print sum


However, I think that this would require an additional standard library extension method. Is that correct?

I'm very new to Cobra. So if I am missing something basic please be sure to let me know.

Thanks,
bp

Re: Map/Select, Reduce/Aggregate Functionality

PostPosted: Tue Jan 15, 2013 5:36 pm
by hopscc
It should be supported through whatever C#/.Net do.

It's my impression that this would be done using LINQ capabilities and cobra currently has some issues with finding/recognising/exposing all the (static extension ?) methods that LINQ is built on :(

see LINQ Query expression
I dont know if there are work arounds for this yet or not...

Re: Map/Select, Reduce/Aggregate Functionality

PostPosted: Tue Jan 15, 2013 6:19 pm
by Charles
It's two lines to sum:
# set up
nums = [1, 2, 3] # inferred as List<of int>

# compute sum
sum = 0
for num in nums, sum += num

# output
trace sum, nums

Though I wouldn't mind if we had a single expression. Preferably one that worked with other aggregate functions (min, max, sum, avg).

Also, your subject references "Map/Select". We do that through for-expressions:
customers = for cust in customers where cust.hasBalance
squares = for n in nums get n**2

See ForExpression for details.

Re: Map/Select, Reduce/Aggregate Functionality

PostPosted: Tue Jan 15, 2013 7:23 pm
by bertpritchard
Thanks! That's what I get for just skimming the for expression doc earlier. I was in the "once you've seen one for loop you've seen them all" mindset.

That functionality is what I was looking for. With the ability to call a function after the where and get, they are essentially filter and map.

I started thinking about reduce and I couldn't easily think of a case where I couldn't handle it neatly with a few lines of a for expression.

Thanks again!