NSB Cosmic Center Image

N.S.B. Cosmic Center

NSB Cosmic Center Image

JavaScript & MathScript Interactive Document Generator-Demo-09

This document is generated by N.S.B. JavaScript & MathScript Interactive Document Generator version 001.

Introduction to N.S.B. JavaScript & MathScript Interactive Document Generator

MathScript Functions (cont.)

The functions holdCases[] and delCases[] are useful for keeping or deleting, respectively, the elements of a MathScript function that satisfy a certain property. In the following code sequence, we begin with a MathScript array containing several elements of various types, then proceed to illustrate the use of holdCases[] and delCases[].

input(1)=> ff={a,f[i,j], 2.3, x, g[r,s,t], pi, 11711}
output(1)=> {a, f[i, j], 2.300, x, g[r, s, t], 3.142, 11711}
input(2)=> holdCases[ff, isNum] 
output(2)=> {2.300, 3.142, 11711}
input(3)=> delCases[ff, isNum] 
output(3)=> {a, f[i, j], x, g[r, s, t]}
input(4)=> %// delCases[#, isStr] 
output(4)=> {f[i, j], g[r, s, t]}

Many more types of conditions that can be applied to the identification of members will be discussed further in more advanced demodocs.

We have already introduced the function insert[] that can be used to place a new element at a specific position in a MathScript function. On the other hand, if you wish to delete an element at a specific position you can use the function delAt[]. Consider the following code sequence:

input(5)=> F[a,b,c,d,e]
output(5)=> F[a, b, c, d, e]
input(6)=> insert[%, x, 3] 
output(6)=> F[a, b, x, c, d, e]
input(7)=> %// insert[#, y, 6] 
output(7)=> F[a, b, x, c, d, y, e]
input(8)=> delAt[%, 1] 
output(8)=> F[b, x, c, d, y, e]
input(9)=> %// delAt[#, 6] 
output(9)=> F[b, x, c, d, y]

The function isMemberOf[] checks whether a element of a certain expression or property is a member of a MathScript function; the function countMemberOf[] tells you the number of times such element does occur, while indexMemberOf[] gives the position of a particular occurrence. In the following code sequence, we return to our already defined MathScript array, then apply our member-associated functions:

input(10)=> ff
output(10)=> {a, f[i, j], 2.300, x, g[r, s, t], 3.142, 11711}
input(11)=> isMemberOf[f[i,j], ff] 
output(11)=> true
input(12)=> countMemberOf[f[i,j], ff] 
output(12)=> 1
input(13)=> indexMemberOf[f[i,j], ff] 
output(13)=> 2
input(14)=> ff// isMemberOf[isNum, #] 
output(14)=> true
input(15)=> ff// countMemberOf[isNum, #] 
output(15)=> 3
input(16)=> ff// indexMemberOf[isNum, #] 
output(16)=> 3
input(17)=> ff//reverse 
output(17)=> {11711, 3.142, g[r, s, t], x, 2.300, f[i, j], a}
input(18)=> %// indexMemberOf[isNum, #] 
output(18)=> 1

Before proceeding to the next demodoc, where we shall continue to introduce more functions and features of MathScript, why don't you practice using MathScript live by clicking the program page below, and apply what you learned on this page.

Program Page
Next
NSB Cosmic Center Image

Home
N.S.B. Cosmic Center

NSB Cosmic Center Image