i have own small library doing http requests our api server. have paginated responses want library handle them nicely. desire have array.reduce()
. ideally this:
get("http://myapi.com/somedata") .onpage(initialvalue: [string](), combine: {valuesofar /*[string]*/, jsondata in if let arrayofstrings = jsondata as? [string] { string in arrayofstrings { valuesofar.append(string) } } return valuesofar }) .oncomplete({finalvalue in //do stuff finalvalue })
i can't figure out how define though. far have looks this:
func get(url: string) -> session {return session(url)} class session { var pagecallback: /*what goes here?*/ var completecallback: /*what goes here?*/ var valuesofar: /*what goes here?*/ func onpage<t>(initialvalue: t, combine: (t, json) -> t) -> session { valuesofar = initialvalue pagecallback = combine return self } func oncomplete<t>(callback: (t) -> void) -> session { completecallback = callback return self } func calledwhennewpagefromserver(jsondata: json) { valuesofar = pagecallback(valuesofar, jsondata) if nomorepages {completecallback(valuesofar)} } }
i can't figure out type signatures generic properties should be.
Comments
Post a Comment