In the 2018 release of ColdFusion, we have introduced new Built-In Functions(BIF) to manipulate different objects using a single line invocation.
- Query Functions
- QueryDeleteRow( Object query, int rowNum )
- QueryDeleteColumn( Object query, String columnName)
- Arrays Functions
- ArrayFirst(List list)
- ArrayLast(List list)
- XML Functions
- XmlIsEmpty( XmlNodeList xmlNodeList )
- XmlCount( XmlNodeList xml )
- XmlAppend( XmlNodeList xml1, XmlNodeList xml2 )
- XmlUpdate( XmlNodeList xml, String key, Object value )
- XmlKeyList( XmlNodeList xml )
- XmlKeyArray( XmlNodeList xml )
- Async Functions
- RunAsync()
- RunAsync(UDFMethod method)
- RunAsync(UDFMethod method, long timeOut)
Numeric Member Functions: We have introduced equivalent Member functions for the following numeric functions.
- var.abs()
- var.aCos()
- var.aSin()
- var.atn()
- var.bitAnd(number2)
- var.bitMaskClear(start, length)
- var.bitMaskRead(start, length)
- var.bitMaskSet(mask, start, length)
- var.bitNot()
- var.bitOr(number2)
- var.bitSHLN(count)
- var.bitXor(number2)
- var.ceiling()
- var.cos()
- var.decrementValue()
- var.exp()
- var.fix ()
- var.floor()
- var.formatBaseN(radix)
- var.incrementValue()
- var.log()
- var.log10()
- var.max (number)
- var.min(number)
- var.round()
- var.sgn()
- var.sin()
- var.sqr()
- var.tan()
- var.precisionEvaluate(expr)
Date Member Functions
- dateVar.format()
Apart from adding these functions, we have enhanced member functions to return meaningful values so as to enhance in Member Functions Chaining.
- Array
- arrayObj.clear()
- arrayObj.deleteAt()
- arrayObj.delete()
- arrayObj.deleteNoCase()
- arrayObj.insertAt()
- arrayObj.prepend()
- arrayObj.resize()
- arrayObj.set()
- arrayObj.sort()
- arrayObj.swap()
- Struct
- structObj.delete()
- structObj.insert()
- structObj.update()
- Async Member Functions
- then(UDFMethod method)
- then(UDFMethod method, long timeout)
- error(UDFMethod method)
- error(UDFMethod method, long timeout)
- get()
- get(long timeout)
- cancel()
- isCancelled()
- isDone()
One can use this feature to write compact code, some examples are as follows:
<cfscript> myarray=ArrayNew(1); new_arr= myarray.clear().Resize(10).set(1,10,"blah"); writedump(new_arr); </cfscript> <cfscript> ordered_struct=["key1":"val1","key2":"val2"] unordered_struct={"key3":"val3","key4":"val4"} new_struct=unordered_struct.append(ordered_struct).update("key4","updated val4").insert("key5","inserted val5"); writedump(new_struct); writedump(unordered_struct); </cfscript> <cfscript> myResult=QueryExecute(("SELECT * FROM ORDERS WHERE ORDERID BETWEEN :low AND :high"),{low=1,high=23}, {datasource="cfartgallery"}); // chaining methods a=myResult.deleteColumn("PHONE").filter(function(item){ return item.STATE=="CO" || item.STATE=="CA"; }).valueArray("TOTAL"); WriteDump(a); // use reduce function to calculate sum of all prices red=a.reduce(function(prev,next){ return prev+next; // sum of all prices },0); writeoutput(red); </cfscript>
Hi Ashudeep,
Very nice post! Couple comments:
1) format() is, thankfully, just a member function (not BIF)
2) precisionEvaluate() is also a numeric member function (useful for converting to java.math.BigDecimal) Ex:
myVar1 = 10;
myVar2 = myVar1.precisionEvaluate();//converts to java.math.BigDecimal (good!)
writeOutput(myVar1.getClass().getName() & ‘ ‘ & myVar2.getClass().getName());
Outputs “java.lang.Integer java.math.BigDecimal” Sweet!! Thanks Adobe!! ..now for CF to internally use BigDecimal to reduce floating point math issues/tickets =)
Thanks!,
-Aaron
You must be logged in to post a comment.