How to resolve the algorithm Function composition step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Function composition step by step in the VBScript programming language

Table of Contents

Problem Statement

Create a function, compose,   whose two arguments   f   and   g,   are both functions with one argument.

The result of compose is to be a function of one argument, (lets call the argument   x),   which works like applying function   f   to the result of applying function   g   to   x.

Reference: Function composition Hint: In some languages, implementing compose correctly requires creating a closure.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Function composition step by step in the VBScript programming language

Source code in the vbscript programming language

option explicit
class closure

	private composition
	
	sub compose( f1, f2 )
		composition = f2 & "(" & f1 & "(p1))"
	end sub
	
	public default function apply( p1 )
		apply = eval( composition )
	end function
	
	public property get formula
		formula = composition
	end property
	
end class

dim c
set c = new closure

c.compose "ucase", "lcase"
wscript.echo c.formula
wscript.echo c("dog")

c.compose "log", "exp"
wscript.echo c.formula
wscript.echo c(12.3)

function inc( n )
	inc = n + 1
end function

c.compose "inc", "inc"
wscript.echo c.formula
wscript.echo c(12.3)

function twice( n )
	twice = n * 2
end function

c.compose "twice", "inc"
wscript.echo c.formula
wscript.echo c(12.3)

  

You may also check:How to resolve the algorithm Calculating the value of e step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Empty string step by step in the MIPS Assembly programming language
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the Sidef programming language
You may also check:How to resolve the algorithm Count in factors step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the FreeBASIC programming language