Skip to content

Instantly share code, notes, and snippets.

function cloneThenDeleteNullValues(obj: object) {
const res = Object.entries(obj).reduce((acc: object, [k, v]) => {
if (typeof v !== 'undefined' && v != null) {
acc[k] = v;
}
return acc;
}, {} as object);
return res;
}
@LexVocoder
LexVocoder / scala-unix-2019.md
Last active May 17, 2019 04:27
How to get started developing Scala under Linux

Scala is fun, but getting started can be daunting. This is how I got the Scala IDE for Eclipse running on my Linux system.

These directions assume you know your way around UNIX/Linux command line.

Java 8

First, you need a java compiler. Run this to see what output it produces:

javac -version

# This is experimental!
def type_as_str(v):
return type(v).__name__
def deep_merge_mappings(a, b, *rest, sequence_behavior='append'):
"""
merges b into a and return merged result
sequence_behavior - specifies what happens if a & b are (or contain) non-string sequences. It must be one of the following:
@LexVocoder
LexVocoder / pull-subscriber.js
Created October 2, 2018 05:24
Pull from observable (via callback)
// Pull values from observables (instead of push-subscribe).
// A pull is like a one-time subscription.
// Typically, the onNext argument to pull invokes pull again before finishing.
// This negates the need for a while loop;
// besides, such a loop will pull multiple times without waiting for prior pulls to finish processing.
// There's a bug in this somewhere. A pull after a buffer overflow should first empty buffer, and then err.
// Instead, we get deadlock. Weird.
@LexVocoder
LexVocoder / round-robin-observable.js
Created October 2, 2018 04:22
Round Robin Observable
// I originally made this, trying to solve the problem where I had to use Iterator instead of Observable
// when feeding data to concurrenct workers.
// I really think observable is the wrong thing here.
// Typically, Observables push the values to observers.
// As new values show up, it spawns however many concurrenct invocations of Observer.next it needs.
// Workers typically want to pull each value.
// So, Iterator makes sense.
// Hmm, but ... what if we wanted to pull values from an Observable?
@LexVocoder
LexVocoder / derror.ts
Last active October 3, 2018 16:38
DError - Observable-friendly errors with causes, inspired by VError ( DEPRECATED; see https://www.npmjs.com/package/yandere )
// Inspired by VError.
import { none, Option, some, option, None } from "ts-option";
import * as extsprintf from 'extsprintf';
import { defined, getType, toPrintable } from "./utilities";
interface IError {
message: string;
name: string;
stack?: string;
@LexVocoder
LexVocoder / json-helpers.ts
Last active April 13, 2022 02:06
Helpers for converting JSON to actual TypeScript classes
/***
Inspired by http://choly.ca/post/typescript-json/ , except this is only for parsing (not stringifying).
If you use this, I'd appreciate some credit and some feedback ... but neither are required.
Copyright license (Apache) is at the bottom.
NOTE THIS WILL PROBABLY NOT WORK if your uglifier changes constructor/class names. This is a pretty common thing, and it makes me sad.
The main problem this solves is that JSON.stringify returns plain old JS objects, not TS instances.
@LexVocoder
LexVocoder / MockDbSet.cs
Created February 12, 2018 22:36
MockDbSet for mocking EF DbContext
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
using System.Linq;
...
private static Mock<DbSet<T>> MockDbSet<T>(IEnumerable<T> ls) where T : class
{
@LexVocoder
LexVocoder / ConfigureSerilogWithMel.cs
Last active February 14, 2018 00:17
Configure C# dep injector for a console app or unit test that logs to console via Microsoft.Extensions.Logging and via Serilog
private static ServiceProvider ConfigureServices()
{
// Configure console logging for unit-test
var serviceProvider = new ServiceCollection()
.AddLogging(builder =>
{
// workaround for double-gating of log-levels by MEL and Serilog
builder.SetMinimumLevel(LogLevel.Trace);
})
.BuildServiceProvider();
@LexVocoder
LexVocoder / FastQuery.cs
Created December 28, 2017 14:57
FastQuery, a convenient way to run raw SQL queries in C#
// Apache 2.0 license... This code might not work at all.
/// <summary>
/// Must reference parameters in the query positionally, starting with @p0.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query">example is "select foo from bar where frob = @p0 and quux = @p1"</param>
/// <param name="selector">
/// function that (usually) builds a new T from a database row-reader
/// </param>