How to resolve the algorithm Define a primitive data type step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Define a primitive data type step by step in the J programming language

Table of Contents

Problem Statement

Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Define a primitive data type step by step in the J programming language

Source code in the j programming language

NB. z locale by default on path.
type_z_=: 3!:0
nameClass_z_=: 4!:0
signalError_z_=: 13!:8

NB. create a restricted object from an appropriate integer
create_restrict_ =: monad define
 'Domain error: expected integer' assert 1 4 e.~ type y  NB. or Boolean
 'Domain error: not on [1,10]' assert (0 -.@:e. [: , (0&<*.<&11)) y
 value=: y
)

add_restrict_=: monad define
 if. 0 = nameClass<'value__y' do.
  (value + value__y) conew 0{::coname''
 else.
  'value unavailable'signalError 21
 end.
)


  

You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Nim programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Transd programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the RPL programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the OCaml programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Lua programming language