The two versions of the yoga function below were used by Ebelson and Sussman to repeat the Listsy version in the classic "composition" My attempt is to explain the computer program "First version of the book in Swift was used to calculate the sum of integer in a range, or the sum of the squares of integer in any category, and the second version to approximate pi / 8 Was used for.
I was not able to combine versions into a single funk which handles all types. Is there a clever way to combine variants to use the generic or some other Swift language feature?
func sum (word: (int) -> int, a: int, next: (int) - gt; int, b : Int) - & gt; Int {if A & gt; B {return 0} return (duration (a) + zodiac (duration, next (a), next, b))} func zodiac (duration: (int) -> float, a: it, next: (int) - & gt; int, b: int) - & gt; Float {if A & gt; B {return 0} return (duration (a) + zodiac (word, next (a), next, b))
8.0 * sum ({1.0 / float (($ 0 * ($ 0 + 2)}}, 1, {$ 0 + 4}, 2500)
3.14079
As a result, I changed the method signature slightly to make it a little easier And have assumed that it would work for the func sum_ T & gt; A is sufficient (duration: (t -> t), a: t, next: (t -> t), b: t) -> gt; Unfortunately there is no number
in Swift, so we need to make our own.
0
New protocol for the requirements of the function
function
Comparison is reset to the comparative
protocol, reset New we can create your own protocol:
protocol neutral Addisn Elementproveyr {class throw neutral edition Element () - & gt; Self Protocol Adolable {Funk + (LHS: Self, RAS: -) - & gt; <} Code> sum
implementation
We can now implement the sum
function:
func sum & lt; T where T: Joint, T: Neutral Edition element provider, T: Comparative & gt; (Duration: (T -> T), A: T, Next: (T -> T), B: T) - & gt; T {if A & gt; B {Return T.NutralAdditionElement ()} Return Period (A) + Zodiac (word, Next (A), Next, B)}
Creating Int
and corresponds to the protocol +
is already implemented for the protocol for the double
and int
is a simple Provide element:
extension double: addable {} extension int: addable {}
Code> Extension Int: Neutral Edition ElementProver {Static Throw Neutral Edition Element () - & gt; Int {Return 0}} Extension Double: Neutral Edition ElementProver {Static Throw Neutral Addition Element () - & gt; Double {return 0.0}}
No comments:
Post a Comment