{ "cells" : [ { "outputs" : [ ], "metadata" : { }, "source" : [ "%install-location $cwd\/swift-install\n", "%install '.package(path: \"$cwd\/FastaiNotebook_08_data_block\")' FastaiNotebook_08_data_block" ], "cell_type" : "code", "execution_count" : null }, { "cell_type" : "code", "metadata" : { }, "source" : [ "\/\/ export\n", "import Path" ], "execution_count" : null, "outputs" : [ ] }, { "outputs" : [ ], "metadata" : { }, "source" : [ "import FastaiNotebook_08_data_block" ], "cell_type" : "code", "execution_count" : null }, { "outputs" : [ ], "metadata" : { }, "source" : [ "\/\/ export\n", "public protocol HetDictKey {\n", " associatedtype ValueType\n", " static var defaultValue: ValueType { get }\n", "}" ], "execution_count" : null, "cell_type" : "code" }, { "cell_type" : "code", "metadata" : { }, "source" : [ "\/\/ export\n", "\n", "public struct HeterogeneousDictionary {\n", " private var underlying: [ObjectIdentifier : Any] = [:]\n", " \n", " public init() {}\n", " public init(_ key: T.Type, _ value: T.ValueType) {\n", " self.underlying = [ObjectIdentifier(key): value]\n", " }\n", " public init(_ key1: T1.Type, _ value1: T1.ValueType, _ key2: T2.Type, _ value2: T2.ValueType) {\n", " self.underlying = [ObjectIdentifier(key1): value1, ObjectIdentifier(key2): value2]\n", " }\n", "\n", " public subscript(key: T.Type) -> T.ValueType {\n", " get { return underlying[ObjectIdentifier(key), default: T.defaultValue] as! T.ValueType }\n", " set { underlying[ObjectIdentifier(key)] = newValue as Any }\n", " }\n", " \n", " public mutating func merge(_ other: HeterogeneousDictionary,\n", " uniquingKeysWith combine: (Any, Any) throws -> Any) rethrows {\n", " try self.underlying.merge(other.underlying, uniquingKeysWith: combine)\n", " }\n", "}" ], "outputs" : [ ], "execution_count" : null }, { "outputs" : [ ], "metadata" : { }, "source" : [ "\/\/ export\n", "\/\/ Common keys\n", "public struct LearningRate: HetDictKey {\n", " public static var defaultValue: Float = 0.4\n", "}" ], "cell_type" : "code", "execution_count" : null }, { "execution_count" : null, "metadata" : { }, "source" : [ "public struct StepCount: HetDictKey {\n", " public static var defaultValue = 0\n", "}" ], "cell_type" : "code", "outputs" : [ ] }, { "execution_count" : null, "metadata" : { }, "source" : [ "\/\/ Sample usage\n", "var m = HeterogeneousDictionary()\n" ], "cell_type" : "code", "outputs" : [ ] }, { "execution_count" : null, "metadata" : { }, "source" : [ "print(m[LearningRate.self])\n", "m[LearningRate.self] = 3.4\n", "print(m[LearningRate.self])\n", "\n", "print(m[StepCount.self])\n", "m[StepCount.self] = 3\n", "print(m[StepCount.self])\n" ], "outputs" : [ ], "cell_type" : "code" }, { "outputs" : [ ], "metadata" : { }, "source" : [ "print(type(of: m[StepCount.self]))\n", "print(type(of: m[LearningRate.self]))\n" ], "execution_count" : null, "cell_type" : "code" }, { "cell_type" : "markdown", "metadata" : { }, "source" : [ "## Export" ] }, { "cell_type" : "code", "metadata" : { }, "source" : [ "import NotebookExport\n", "let exporter = NotebookExport(Path.cwd\/\"08a_heterogeneous_dictionary.ipynb\")\n", "print(exporter.export(usingPrefix: \"FastaiNotebook_\"))" ], "outputs" : [ ], "execution_count" : null }, { "execution_count" : null, "metadata" : { }, "source" : [ ], "cell_type" : "code", "outputs" : [ ] } ], "metadata" : { "kernelspec" : { "display_name" : "Swift", "name" : "swift", "language" : "swift" } }, "nbformat_minor" : 2, "nbformat" : 4 }