Forums

Map/Select, Reduce/Aggregate Functionality

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Map/Select, Reduce/Aggregate Functionality

Postby bertpritchard » Tue Jan 15, 2013 7:02 am

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
bertpritchard
 
Posts: 7

Re: Map/Select, Reduce/Aggregate Functionality

Postby hopscc » Tue Jan 15, 2013 5:36 pm

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...
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Map/Select, Reduce/Aggregate Functionality

Postby Charles » Tue Jan 15, 2013 6:19 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Map/Select, Reduce/Aggregate Functionality

Postby bertpritchard » Tue Jan 15, 2013 7:23 pm

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!
bertpritchard
 
Posts: 7


Return to Discussion

Who is online

Users browsing this forum: No registered users and 101 guests

cron