Declarative ObjectScript - a proof-of-concept to show how to use declarative programming on ObjectScript.
Declarative programming - a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow. (c) Wikipedia
$ git clone https://github.com/atygaev/declarative-objectscript
$ cd declarative-objectscript
$ docker-compose up -d
$ docker-compose exec iris iris session irisUSER> zn"IRISAPP"What about to find even numbers in given collection of numbers [1, 2, 3, 4]?
IRISAPP> // run legacy codeIRISAPP> do##class(Demo.App).RunWithLegacyCode()
Evennumbers: 2, 4Source of RunWithLegacyCode
IRISAPP> // run DeclarativeOS codeIRISAPP> do##class(Demo.App).RunWithDeclarativeOS()
Evennumbers: 2, 4Source of RunWithDeclarativeOS
Just compare two variants: RunWithDeclarativeOS and RunWithLegacyCode. Both does the same, but there is a difference…
ClassDemo.AppExtendsDeclarativeOS.RegistryHelper { /// @Declarative("examples:isEven") ClassMethodIsEven(numberAs%Numeric) As%Boolean { returnnumber # 2 = 0 } ClassMethodRunWithDeclarativeOS() { setnumbers = ##class(%ListOfDataTypes).%New() fori=1:1:4 { donumbers.Insert(i) } setevenNumbers = $zfilter(numbers, "examples:isEven") // sexy and shortwrite"Even numbers: " _ $zjoin(evenNumbers, "") // printing collection
} ClassMethodRunWithLegacyCode() { setnumbers = ##class(%ListOfDataTypes).%New() fori=1:1:4 { donumbers.Insert(i) } setevenNumbers = ##class(%ListOfDataTypes).%New() // iterate explicitlysetindex = ""for { setindex = numbers.Next(index) // working with indexesquit:index=""setitem = numbers.GetAt(index) if (item # 2 = 0) { doevenNumbers.Insert(item) } } write"Even numbers: "fori=1:1:evenNumbers.Count() { writeevenNumbers.GetAt(i) _ "" } // printing collection
} }You can install the DeclarativeOS by using ZPM
ZPM: USER>installdeclarative-os$ git clone https://github.com/atygaev/declarative-objectscript
$ cd declarative-objectscript
$ docker-compose up -d
$ docker-compose exec iris iris session irisDownload the install.declarative-os.xml from latest release.
https://github.com/atygaev/declarative-objectscript/releases/download/v1.0.2/install.declarative-os.xml
Install the project via terminal:
USER> setinstallFile = "<path to downloaded install.declarative-os.xml>"USER> do$system.OBJ.Load(installFile)Let me show some examples.
Applies given action to every item in collection.
USER> setwords = ##class(%ListOfDataTypes).%New()
USER> dowords.Insert("Hello ")
USER> dowords.Insert("World!")
USER>
USER> // Output collectionUSER> zforeach$zbind(words, "io:print")Hello world!
Joins collection items to a string by using given separator.
USER> setwords = ##class(%ListOfDataTypes).%New()
USER> dowords.Insert("Tic")
USER> dowords.Insert("Tac")
USER> dowords.Insert("Toe")
USER>
USER> // Concat words by using "-"USER> write$zjoin(words, "-")Tic-Tac-Toe
Returns new collection with filtered items only.
USER> setnumbers = ##class(%ListOfDataTypes).%New()
USER> donumbers.Insert(1)
USER> donumbers.Insert(2)
USER> donumbers.Insert(3)
USER> donumbers.Insert(4)
USER>
USER> // Filter collection to find even numbersUSER> setevenNumbers = $zfilter(numbers, "examples:isEven")
USER>
USER> // Output even numbersUSER> write"Even numbers: " _ $zjoin(evenNumbers, ", "), !Even numbers: 2, 4
Finds first item which satisfies given criteria.
USER> setnumbers = ##class(%ListOfDataTypes).%New()
USER> donumbers.Insert(4)
USER> donumbers.Insert(5)
USER> donumbers.Insert(6)
USER>
USER> // Find prime numberUSER> setprimeNumber = $zfind(numbers, "examples:isPrime")
USER>
USER> write"Prime number: " _ primeNumberPrime number: 5
Returns $$$YES if collection contains at least one item which satisfies the given criteria.
Otherwise, returns $$$NO.
USER> setnumbers = ##class(%ListOfDataTypes).%New()
USER> donumbers.Insert(13)
USER> donumbers.Insert(12)
USER> donumbers.Insert(11)
USER> donumbers.Insert(10)
USER>
USER> // Check whether collection contains at least one palindromic number.USER> sethasPalindromicNumbers = $zexists(numbers, "examples:isPalindromic")
USER>
USER> write"Collection has palindromic numbers? " _ $case(hasPalindromicNumbers, 1:"YES", 0:"NO")Collection has palindromic numbers? YES
Counts items which satisfy the given criteria.
USER> setnumbers = ##class(%ListOfDataTypes).%New()
USER> donumbers.Insert(2)
USER> donumbers.Insert(3)
USER> donumbers.Insert(4)
USER> donumbers.Insert(5)
USER>
USER> // Counts prime numbers in given collection of numbers.USER> setprimeNumbersCount = $zcount(numbers, "examples:isPrime")
USER>
USER> write"Count of prime numbers: " _ primeNumbersCountCount of prime numbers: 3
In two words: call $classmethod.
But $classmethod requires class name and method name.
And it looks really long and weird. So I invented aliases.
Alias is a string identifier for pair of Class and ClassMethod.
USER> // instead of thisUSER> setevenNumbers = $zmap(numbers, "Test.DeclarativeOS.MathDeclaratives", "isEven")
USER>
USER> // using aliasUSER> setevenNumbers = $zmap(numbers, "math:isEven")You can define your own declaratives.
Just follow 3 steps:
- Extend from DeclarativeOS.RegistryHelper;
- Implement class method;
- Mark the method.
ClassDemo.MathDeclarativesextensDeclarativeOS.RegistryHelper
{
}ClassDemo.MathDeclarativesextensDeclarativeOS.RegistryHelper
{
ClassMethodsqrt(valueAs%Numeric)
{
return$zsqr(value)
}
ClassMethodsquare(valueAs%Numeric)
{
return$zpower(value, 2)
}
}ClassDemo.MathDeclarativesextensDeclarativeOS.RegistryHelper
{
/// @Declarative("math:sqrt")ClassMethodsqrt(valueAs%Numeric)
{
return$zsqr(value)
}
/// @Declarative("math:square")ClassMethodsquare(valueAs%Numeric)
{
return$zpower(value, 2)
}
}snumbers = ##class(%ListOfDataTypes).%New()
dnumbers.Insert(4)
dnumbers.Insert(9)
w"Sqrt every number: " _ $zjoin($zmap(numbers, "math:sqrt"), ", "), !
w"Square every number: " _ $zjoin($zmap(numbers, "math:square"), ", "), !Sqrt every number: 2 3
Square every number: 16 81
Any contribution is really welcome.
Copyright (c) 2017 InterSystems Developer Community
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
