Skip to content

Instantly share code, notes, and snippets.

View apua's full-sized avatar
💭
Seeking next epic moment

Apua Juan apua

💭
Seeking next epic moment
View GitHub Profile
@apua
apua / merge_sorted_linkedlist.py
Created January 26, 2022 04:06
LeetCode "21. Merge Two Sorted Lists"
"""
>>> 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])
@apua
apua / to_dyn.diff
Last active October 12, 2021 01:18
Convert zdict trait from static to dynamic
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:
@apua
apua / 1_plain.rs
Last active September 30, 2021 06:03
Code reuse and restriction by abstract class
#[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;
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`
@apua
apua / generic.swift
Last active July 21, 2021 11:11
Syntax of generic code in Swift
// 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 {
@apua
apua / read_string.swift
Created July 20, 2021 12:44
Some notes of reading string from remote resource sync/async
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
@apua
apua / pattern_matching.swift
Last active July 16, 2021 01:50
pattern matching in Swift
// 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
@apua
apua / list_nics.py
Created June 10, 2021 08:13
List Physical NICs on ESXi (as `esxcli network list` does)
# :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.
from collections import UserString
class Torshn(UserString):
def eval(self, ops):
s = self
for op in ops:
s = getattr(s, op)()
return s
@apua
apua / index.html
Last active November 15, 2020 09:08
Compare creating array among "spread in array", "array.from", "array.push" (http://jsbench.github.io/#ba37252ff86abd08aa08ff1467cc9c7b) #jsbench #jsperf
<!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>