Forums

passing functions as arguments and inline creation of lists

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

passing functions as arguments and inline creation of lists

Postby snakeman » Thu Nov 14, 2013 4:03 am

Hello World!

Quite new to cobra i'm currently very excited about cobra's language design!
Beside of the obliged 4-space indentation (i prefer two- or one-space-indentation) cobra seems to be the language i always dreamed of!

So now about my issue.I want to do something like
class Quantizer
...
get [i as int] as float
require 0 <= i < .count
return .first + (i*.delta)

def quantize(f as Func<of float>) as List<of float>
return [f(this[j]) for j in 0:.count]


As you may see, i'm used to play with the elder snake (python). There are two problems with this code:
  • the inline list creation doesn't seem to work. The compiler says "error: Expecting GET, but got "]" (RBRACKET) instead." Is there a cobra'ish way for inline list creation?
  • how can i make shure, that my quantize method only accepts functions which take a float as well as return a flot?

It's kinda funny that i had the last problem also with c, with c++ and also with java (these are the only static typed languages i know). Most times i gave up restricting the passed functions after a while and just hoped for the best. As a beginner i'm now hoping that there's a nice way for coping this in cobra...
snakeman
 
Posts: 7

Re: passing functions as arguments and inline creation of li

Postby Charles » Thu Nov 14, 2013 12:33 pm

-- I tweaked your post to use the "cobra" tag instead of the "code" tag. It gives us syntax highlighting. We use "code" for other stuff like command line output or other languages.

-- Regarding indentation, you could use tabs and then set their width to be 1 or 2 spaces (or 6 or 8). This is one of the advantages of tabs.

-- The format for a for-expression in Cobra more closely resembles the for-statement where "for" is the keyword that kicks it off.

-- IIRC, the Func generic type in .NET takes, as type parameters, the type of the arguments and the return type. However, I typically just use method signatures as they closely match the "def foo(args) as type" format that we already use for declaring methods.

Here is a working program I made, starting with your example:

@number float64

sig Func1(x as float) as float

class Quantizer

get count as int
return 5

get first as float
return 1

get delta as float
return 0.1

get [i as int] as float
require 0 <= i < .count
return .first + (i*.delta)

def quantize(f as Func1) as List<of float>
return for j in .count get f(this[j])


class Program

def main
q = Quantizer()
trace q.quantize(do(x as float)=x*x)


-- I could also have used the type "number" in place of "float" and then experimented with "@number float32" vs. "@number float64" vs. "@number decimal".

HTH
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: passing functions as arguments and inline creation of li

Postby snakeman » Thu Nov 14, 2013 1:49 pm

Thanks! Helped a lot.

But after that i'm now struggling with something closely related: The compiler tells me "error: Cannot yield inside an anonymous method."
Is this true? If so, why? What is wrong with yield inside an anonymous method?
snakeman
 
Posts: 7

Re: passing functions as arguments and inline creation of li

Postby Charles » Thu Nov 14, 2013 2:47 pm

It's a limitation of .NET/C#:

http://stackoverflow.com/questions/1217 ... -statement

http://blogs.msdn.com/b/ericlippert/arc ... ators.aspx

We mostly get benefits from .NET but occasionally we get a limitation like this.

Btw I forgot to mention that in numeric ranges like "0 : .count" in a for-expression or for-statement, the "0 :" is implicit/default. In another words, you can just write "for i in .count ..."
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 13 guests