LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  User Defined Functions

    Employee
    Posted 01-24-2012 15:24

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jonathanm

    I'm just starting to get to grips with BRAINscript and am wondering if it's possible to create user defined functions?

    A lot of the data I need to work with uses Julian date formats, i.e. "CYYDDD" where "DDD" is a value in the range 1-366. I have coded a routine that will convert the Julian date into a real date, however some of the files contain more than one date to be converted and I'd like to avoid repeating the coding if possible.

    Ideally what I'm looking for is the ability to code something like:

    realTaxDate = ConvertJulianDate(TaxDate)

    Thanks

    Jonathan


  • 2.  RE: User Defined Functions

    Employee
    Posted 01-25-2012 04:48

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Hi,

    There isn't a way to define your own BRAINscript functions and use them at the moment. This has been raised as an enhancement request although I can't say anything about when that would likely be implemented.

    One thing you can do is to create another parameter on your node to contain the code for the function you want to implement, then invoke this via textual substitution. Basically, using this method, you don't have to re-type the code, but the code will be inserted at each point where you use the textual substitution for the parameter.


    For a simple example, consider you want to define a method that takes a year, month and day each in string form, but containing integer values (e.g. "2012", "01", "22") like:

    date convertToDate(string year, string month, string day) {
      intYear = year.int()
      intMonth = month.int()
      intDay = day.int()
      return date(intYear, intMonth, intDay)
    }
    Then assuming you want to perform this function several times in your code, like:

    date1 = convertToDate(f1, f2, f3)
    date2 = convertToDate(f4, f5, f6)
    There isn't really a way to do this directly, however, you can do pretty much the equivalent thing by creating a new brainscriptexpr parameter, call it "convertToDate".

    Then, in convertToDate, you can use the following BRAINscriptcode:

    intYear = arg0.int()
    intMonth = arg1.int()
    intDay = arg2.int()
    retVal = date(intYear, intMonth, intDay)
    Then in your main node script parameter you could have the following:

    arg0 = f1
    arg1 = f2
    arg2 = f3
    {{^convertToDate^}}
    date1 = retVal
    
    arg0 = f4
    arg1 = f5
    arg2 = f6
    {{^convertToDate^}}
    date2 = retVal
    Bear in mind that since this is just using textual substitution (i.e. inserting the method code), and because BRAINscript doesn't have a concept of local variables, or scope, you have to be careful that any variable names (e.g. retVal, arg0, arg1, arg2 in this example) aren't conflicting and overwriting the values of variables defined in your main code script.

    Tim.


  • 3.  RE: User Defined Functions

    Employee
    Posted 01-25-2012 08:17

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jonathanm

    Hi Tim

    Thanks for the quick reply, I'll try that. These Julian dates are all over the database so it'll be a useful technique to use.

    All the best

    Jonathan


  • 4.  RE: User Defined Functions

    Employee
    Posted 01-25-2012 08:19

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    No worries.

    Obviously, if you need to use this in multiple nodes, you could declare your new "function" in a Graph Level parameter.

    Or maintain the code outside of BRE & your graph & declare it in a brs or brp or something, but that's probably a bit of overkill.

    Tim.


  • 5.  RE: User Defined Functions

    Employee
    Posted 02-26-2015 10:42

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: djnrempel

    I am trying to use this method but when I create a new braincodeexpr parameter, I don't see where I can enter that code.


  • 6.  RE: User Defined Functions

    Employee
    Posted 03-04-2015 02:43

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Tim Meagher

    Assuming you mean brainscriptexpr parameter...
    Perhaps you could post a graph with an example of what you are trying to do?
    You should be able to enter the code inside the parameter you create....

    Tim.


  • 7.  RE: User Defined Functions

    Employee
    Posted 03-10-2015 08:22

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: djnrempel

    Okay, I see now that when you create a parameter, it creates a new window within the node editor. I was looking for a place to enter code in the screen where you create parameters.