Skip to content

Repository files navigation

Declarative ObjectScript

Gitter

Work with collections like a boss

Declarative ObjectScript - a proof-of-concept to show how to use declarative programming on ObjectScript.

Promo

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

Install with Docker

$ git clone https://github.com/atygaev/declarative-objectscript
$ cd declarative-objectscript
$ docker-compose up -d
$ docker-compose exec iris iris session iris
USER> zn"IRISAPP"

Run Demo

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, 4

Source of RunWithLegacyCode

IRISAPP> // run DeclarativeOS codeIRISAPP> do##class(Demo.App).RunWithDeclarativeOS()
Evennumbers: 2, 4

Source 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
} }

Content

Installation

Use ZPM

You can install the DeclarativeOS by using ZPM

ZPM: USER>installdeclarative-os

Use Docker

$ git clone https://github.com/atygaev/declarative-objectscript
$ cd declarative-objectscript
$ docker-compose up -d
$ docker-compose exec iris iris session iris

Use XML

Download 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)

Examples

Let me show some examples.

Iterate

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!

Join

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

Filter

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

Find

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: " _ primeNumber
Prime number: 5

Exists

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

Count

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: " _ primeNumbersCount
Count of prime numbers: 3

How it works

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")

How define own declaratives

You can define your own declaratives.

Just follow 3 steps:

  • Extend from DeclarativeOS.RegistryHelper;
  • Implement class method;
  • Mark the method.

Step 1. Extends from DeclarativeOS.RegistryHelper

ClassDemo.MathDeclarativesextensDeclarativeOS.RegistryHelper
{
}

Step 2. Implement class method

ClassDemo.MathDeclarativesextensDeclarativeOS.RegistryHelper
{
ClassMethodsqrt(valueAs%Numeric)
{
return$zsqr(value)
}
ClassMethodsquare(valueAs%Numeric)
{
return$zpower(value, 2)
}
}

Step 3. Mark the method

ClassDemo.MathDeclarativesextensDeclarativeOS.RegistryHelper
{
/// @Declarative("math:sqrt")ClassMethodsqrt(valueAs%Numeric)
{
return$zsqr(value)
}
/// @Declarative("math:square")ClassMethodsquare(valueAs%Numeric)
{
return$zpower(value, 2)
}
}

Step 4. Try it

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

Contribution

Any contribution is really welcome.

MIT License

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.

Releases

Packages

Contributors

Languages