Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

sum in array swift

protocol Number 
{
   func +(l: Self, r: Self) -> Self
   func -(l: Self, r: Self) -> Self
   func >(l: Self, r: Self) -> Bool
   func <(l: Self, r: Self) -> Bool
}

extension Double : Number {}
extension Float  : Number {}
extension Int    : Number {}

infix operator += {}

func += <T:Number> (inout left: T, right: T)
{
   left = left + right
}

prefix operator +/ {}

prefix func +/ <T:Number>(ar:[T]?) -> T?
{
    switch true
    {
    case ar == nil:
        return nil

    case ar!.isEmpty:
        return nil

    default:
        var result = ar![0]
        for n in 1..<ar!.count
        {
            result += ar![n]
        }
        return result
   }
}
Comment

sum in array swift

extension Sequence  {
    func sum<T: AdditiveArithmetic>(_ predicate: (Element) -> T) -> T { reduce(.zero) { $0 + predicate($1) } }
}
Comment

addition of numbers from array swift

var totalSum = scaleData.map({$0.points}).reduce(0, +)
Comment

PREVIOUS NEXT
Code Example
Swift :: swift hashable 
Swift :: circular array swift 
Swift :: xcode combine calayer into an image 
Ruby :: kill port already in use 
Ruby :: how to remove columns from rails 
Ruby :: how to rename a table in ruby 
Ruby :: rails resources except 
Ruby :: embedded ruby tag 
Ruby :: rails delete link 
Ruby :: get current year in ruby 
Ruby :: ruby get line from a file 
Ruby :: ruby delete folder recursively 
Ruby :: ruby read file 
Ruby :: max keys from hash ruby 
Ruby :: ruby each_with_object 
Ruby :: function is uninitialized constant ruby 
Ruby :: ruby get decimal 
Ruby :: rails resources only 
Ruby :: ruby array prepend vs unshift 
Ruby :: Convert Date and time to utc in rails 
Ruby :: rails g migration remove default 
Ruby :: ruby check if constant exists 
Ruby :: rails always 2 decimal 
Ruby :: rails server not updating 
Ruby :: rails string to html 
Ruby :: resources rails 
Ruby :: map each with index 
Ruby :: ruby named parameters 
Ruby :: name error on ruby 
Ruby :: rails format number k - m 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =