How to Write Generic Function in Swift? -


i'm trying write general argmin function in swift. here code:

func argmin<x, y:comparable, r:sequencetype, x== r.generator.element>     (f:(x)->y, domain:r)->x{        var gen = domain.generate()        var best = gen.next()!        var minval = f(best)        while let = gen.next() {            let value = f(this)            if value < minval {                best =                minval = value            }     }     return best } 

i error message "expected identifier name generic parameter" when try compile definition. have no idea means. sounds error 1 on calling function, not defining it, then, wouldn't understand it.

i'm starting learn swift. can explain message? (btw, know function blow if called empty sequence. i'm not worrying yet.)

you have remove comma:

func argmin<x, y:comparable, r:sequencetype, x== r.generator.element>                                            ^ 

placed that, tells compiler there's generic parameter. error message says - maybe in cryptic way, once know, it's clearer means


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -