Skip to content

Instantly share code, notes, and snippets.

@hollance
Created November 12, 2019 16:28

Revisions

  1. hollance created this gist Nov 12, 2019.
    37 changes: 37 additions & 0 deletions CoreML+Combine.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import CoreML
    import Combine

    extension Publisher where Self.Output: MLFeatureProvider {
    /**
    Operator that lets you run a Core ML model as part of a Combine chain.

    It accepts an MLFeatureProvider object as input, and, if all goes well,
    returns another MLFeatureProvider with the model outputs.

    Since Core ML can give errors, we put everything in a Result object.

    Use the `compactMap` version to always ignore errors, or `tryMap` to
    complete the subscription upon the first error.

    To perform the Core ML request on a background thread, it's probably a good
    idea to write a custom Publisher class, but for simple use cases `map` works
    well enough.
    */
    public func prediction(model: MLModel) -> Publishers.Map<Self, Result<MLFeatureProvider, Error>> {
    map { input in
    do {
    return .success(try model.prediction(from: input))
    } catch {
    return .failure(error)
    }
    }
    }

    public func prediction(model: MLModel) -> Publishers.CompactMap<Self, MLFeatureProvider> {
    compactMap { input in try? model.prediction(from: input) }
    }

    public func prediction(model: MLModel) -> Publishers.TryMap<Self, MLFeatureProvider?> {
    tryMap { input in try model.prediction(from: input) }
    }
    }