This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
>>> to_maybe_list = lambda x: [] if x is None else list(n.val for n in x) | |
>>> m = lambda a,b: to_maybe_list(merge(*map(ListNode.from_iter, (a, b)))) | |
>>> assert(m([], []) == []) | |
>>> assert(m([], [0]) == [0]) | |
>>> assert(m([0], []) == [0]) | |
>>> assert(m([1,2,4], [1,3,4]) == [1,1,2,3,4,4]) | |
>>> assert(m([1,2,5], [2,3,4]) == [1,2,2,3,4,5]) | |
>>> assert(m([2,3,4], [1,2,5]) == [1,2,2,3,4,5]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit f67d8e7e8509c38b3b92bc1fa37773250c46ebe3 | |
Author: Apua | |
Date: Tue Oct 12 08:21:37 2021 +0800 | |
Convert to trait object | |
To make possible dict as a variable, its type has to be "trait object". | |
A trait object has to be object safe, causes changes of the interface: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] struct Dict { api: &'static str, name: &'static str } | |
const YAHOO: Dict = Dict { name: "yahoo", api: "http://yahoo/{word}" }; | |
const URBAN: Dict = Dict { name: "urban", api: "http://urban/{word}" }; | |
fn get_url(dict: &Dict, word: &str) -> String { | |
dict.api.replace("{word}", word) | |
} | |
type YahooSummary = &'static str; | |
type YahooExplain = &'static str; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod a { | |
pub const _A1: i32 = 1; | |
const _A2: i32 = 1; | |
mod aa { | |
pub const _A3: i32 = 1; | |
pub const _A4: i32 = 1; | |
} | |
/// :mod:`aa` is accessible in :mod:`a` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ref: Generics https://docs.swift.org/swift-book/LanguageGuide/Generics.html | |
// `f` is "generic function", `T` is "generic type" | |
func f<T> (x: T) {} | |
// `T: SomeClass` is "type constraint" | |
func f<T: SomeClass, U: SomeProtocol>(t: T, u: U) {} | |
// "associated type" | |
protocol Container { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let data = "Café\n".data(using: .utf8 /* String.Encoding.utf8 */)! // not documented | |
let text = String(data: data, encoding: .utf8) | |
let filepath = "test" // it must exist | |
let fp = FileHandle(forWritingAtPath: filepath)! | |
fp.write(data) // will be deprecated | |
fp.write(contentsOf: data /* require DataProtocol */) // both ways write to file immediately | |
let fmgr = FileManager.default |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ref: https://fuckingifcaseletsyntax.com | |
let ONE = 1 | |
enum E { case A, B(Int), C(Int, String) } | |
for v in 0...3 { | |
switch v { | |
case 0: print("0") // match value | |
case ONE: print("ONE=\(ONE)") // match identifier | |
case 1..<3: print("in [1,3)") // v ~= 1..<3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# :author: Apua | |
# :ref: /bin/esxcli.py 6.5.0 | |
# :target: get results of :cmd:`esxcli network nic list` in Python | |
# | |
# Note: | |
# | |
# - This script must run on ESXi console. | |
# - :lib:`vmware` is built-in on ESXi. | |
# - :obj:`session` must live during communicating with ESXi; | |
# and be set to `None` in order to disconnect. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import UserString | |
class Torshn(UserString): | |
def eval(self, ops): | |
s = self | |
for op in ops: | |
s = getattr(s, op)() | |
return s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Compare creating array among "spread in array", "array.from", "array.push"</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> |