 Swift - Maple Help

CodeGeneration

 Swift
 translate Maple code to Swift code Calling Sequence Swift(x, cgopts) Parameters

 x - expression, list, rtable, procedure, or module cgopts - (optional) one or more CodeGeneration options Description

 • The Swift(x, cgopts) calling sequence translates Maple code to Swift code.
 - If the parameter x is an algebraic expression, then a Swift statement assigning the expression to a variable is generated.
 - If x is a list of equations $\mathrm{nm}=\mathrm{expr}$, where $\mathrm{nm}$ is a name and $\mathrm{expr}$ is an algebraic expression, then this is understood as a sequence of assignment statements.  In this case, the equivalent sequence of Swift assignment statements is generated.
 - If x is a procedure, then a Swift function is generated which is equivalent to the procedure, along with any necessary import statements.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions. Examples

For a description of the options used in the following examples, see CodeGenerationOptions.

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$

Translate a simple expression and assign it to the name $w$ in the target code.

 > $\mathrm{Swift}\left(x+yz-2xz,\mathrm{resultname}="w"\right)$
 w = -2 * x * z + y * z + x

Translate a list and assign it to an array with the name $w$ in the target code.

 > $\mathrm{Swift}\left(\left[\left[x,2y\right],\left[5,z\right]\right],\mathrm{resultname}="w"\right)$
 w = x w = 2 * y w = 5 w = z

Translate a computation sequence.  Optimize the input first.

 > $\mathrm{cs}≔\left[s=1.0+x,t=\mathrm{ln}\left(s\right)\mathrm{exp}\left(-x\right),r=\mathrm{exp}\left(-x\right)+xt\right]:$
 > $\mathrm{Swift}\left(\mathrm{cs},\mathrm{optimize}\right)$
 s = 0.10e1 + x t1 = log(s) t2 = exp(-x) t = t2 * t1 r = x * t + t2

Declare that $x$ is a float and $y$ is an integer. Return the result in a string.

 > $s≔\mathrm{Swift}\left(x+y+1,\mathrm{declare}=\left[x::\mathrm{float},y::'\mathrm{integer}'\right],\mathrm{output}=\mathrm{string}\right)$
 ${s}{≔}{"cg = x + y + 0.1e1"}$ (1)

Translate a procedure.  Assume that all untyped variables have type integer.

 > f := proc(x, y, z) return x*y-y*z+x*z; end proc:
 > $\mathrm{Swift}\left(f,\mathrm{defaulttype}=\mathrm{integer}\right)$
 func f (x: Int32, y: Int32, z: Int32) -> Int32 {   return(y * x - y * z + x * z); }

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

 > f := proc(n)   local x, i;   x := 0.0;   for i to n do     x := x + i;   end do; end proc:
 > $\mathrm{Swift}\left(f\right)$
 func f (n: Int32) -> Double {   var x: Double   var i: Int32   var cgret: Double   x = 0.0e0   for i in 1...n   {     x = x + i     cgret = x   }   return(cgret); } Compatibility

 • The CodeGeneration[Swift] command was introduced in Maple 2017.