{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Installing packages:\n", "\t.package(path: \"/home/ubuntu/fastai_docs/dev_swift/FastaiNotebook_08_data_block\")\n", "\t\tFastaiNotebook_08_data_block\n", "With SwiftPM flags: []\n", "Working in: /tmp/tmpd4g5mfxf\n", "Fetching https://github.com/mxcl/Path.swift\n", "Fetching https://github.com/JustHTTP/Just\n", "Completed resolution in 3.88s\n", "Cloning https://github.com/mxcl/Path.swift\n", "Resolving https://github.com/mxcl/Path.swift at 0.16.2\n", "Cloning https://github.com/JustHTTP/Just\n", "Resolving https://github.com/JustHTTP/Just at 0.7.1\n", "Compile Swift Module 'Just' (1 sources)\n", "Compile Swift Module 'Path' (9 sources)\n", "Compile Swift Module 'FastaiNotebook_08_data_block' (13 sources)\n", "Compile Swift Module 'jupyterInstalledPackages' (1 sources)\n", "Linking ./.build/x86_64-unknown-linux/debug/libjupyterInstalledPackages.so\n", "Initializing Swift...\n", "Loading library...\n", "Installation complete!\n" ] } ], "source": [ "%install '.package(path: \"$cwd/FastaiNotebook_08_data_block\")' FastaiNotebook_08_data_block" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// export\n", "\n", "public protocol HetDictKey: Hashable {\n", " associatedtype ValueType\n", " static var defaultValue: ValueType { get }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// export\n", "\n", "public struct HeterogeneousDictionary {\n", " private var underlying: [AnyHashable : Any] = [:]\n", " \n", " public init() {}\n", " public init(_ key: T, _ value: T.ValueType) {\n", " self.underlying = [key: value]\n", " }\n", " public init(_ key1: T1, _ value1: T1.ValueType, _ key2: T2, _ value2: T2.ValueType) {\n", " self.underlying = [key1: value1, key2: value2]\n", " }\n", "\n", " public subscript(key: T) -> T.ValueType {\n", " get {\n", " return underlying[key] as! T.ValueType? ?? T.defaultValue\n", " }\n", " set(newValue) {\n", " underlying[key] = newValue as Any\n", " }\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", "}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// export\n", "\n", "// Common keys\n", "public struct Accuracy: HetDictKey, Equatable {\n", " public init() {}\n", " public static var defaultValue: Float = 0\n", "}\n", "\n", "public struct LearningRate: HetDictKey, Equatable {\n", " public init() {}\n", " public static var defaultValue: Float = 0.4\n", "}\n", "\n", "public struct StepCount: HetDictKey, Equatable {\n", " public init() {}\n", " public static var defaultValue = 0\n", "}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Sample usage\n", "var m = HeterogeneousDictionary()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4\r\n", "3.4\r\n", "0\r\n", "3\r\n" ] } ], "source": [ "print(m[LearningRate()])\n", "m[LearningRate()] = 3.4\n", "print(m[LearningRate()])\n", "\n", "print(m[StepCount()])\n", "m[StepCount()] = 3\n", "print(m[StepCount()])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Int\r\n", "Float\r\n" ] } ], "source": [ "print(type(of: m[StepCount()]))\n", "print(type(of: m[LearningRate()]))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: for performance, need to optimize hashcode to avoid collisions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import Path\n", "import FastaiNotebook_08_data_block" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "notebookToScript(fname: (Path.cwd / \"08a_heterogeneous_dictionary.ipynb\").string)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Swift", "language": "swift", "name": "swift" }, "language_info": { "file_extension": ".swift", "mimetype": "text/x-swift", "name": "swift", "version": "" } }, "nbformat": 4, "nbformat_minor": 2 }