Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / util.js
Created January 6, 2018 12:11
JavaScript Utility Functions
function nil(value) {
return value === undefined || value === null
}
function notNil(value) {
return !nil(value)
}
function empty(value) {
if (nil(value)) {
@peter
peter / java-vs-python.md
Last active January 1, 2018 08:40
Java vs Python

Java vs Python

Split String

Java:

String numberString = "1,2,3,4,5";
int[] numbers = Arrays.stream(numberString.split(",")).mapToInt(Integer::parseInt).toArray();
@peter
peter / python-runtime-type-checks.md
Last active December 21, 2017 15:15
Python runtime type checks with decorator

Python runtime type checks with decorator

# This modules provides pre/post conditions for python functions via the @typeSpec decorator

from functools import reduce

def compact(d):
    return {k: v for k, v in d.items() if v != None}
@peter
peter / static-vs-dynamic-json.md
Last active March 28, 2018 11:23
Static vs Dynamic Typing for JSON/JSON Schema Transformations

Static vs Dynamic Typing for JSON Handling

The point of comparison here is a function that filters some JSON based on on an access property set in a JSON schema. See Haskell vs Clojure for an interesting similar comparison.

Scala

The Scala code uses the circe library for Json handling:

@peter
peter / s3-direct-javascript-upload.md
Last active November 29, 2017 14:51
S3 Direct Upload from JavaScript with the aws-sdk package

S3 Direct JavaScript Upload

TypeScript Code

S3 client module:

import * as AWS from 'aws-sdk'

export const REGION = 'eu-west-1'
@peter
peter / scala-vs-dynamic.md
Last active December 8, 2017 08:50
Clojure vs Scala, Dynamic vs Static Typing

Untyped Scala vs Properly Typed Scala vs Dynamic Typing

The point of comparison here is a simple function that looks up a string key in a nested map/dictionary (JSON data from the payload/claims of a JWT token) and returns the given key if it's there and None/nil otherwise.

Scala (Without Proper Types)

Original function:

def verifyPartner(partner: String, payload: Map[String, java.lang.Object]): Either[String, String] = {
@peter
peter / user.ts
Created October 23, 2017 08:48
Static typing and JSON mapping (in TypeScript)
export class User {
id: string
country: string
email: string
name: string
tags: string[]
verified: boolean
active: boolean
locale: string
openIds: any
@peter
peter / tsconfig.json
Created October 12, 2017 09:23
TypeScript compiler options
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
@peter
peter / vs-code-user-settings.json
Last active October 30, 2017 09:18
Visual Studio Code User Settings JSON
{
"editor.fontSize": 14,
"editor.tabSize": 2,
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"files.trimTrailingWhitespace": true,
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
@peter
peter / diff.ts
Last active February 15, 2025 06:02
TypeScript code to diff two JavaScript objects containing JSON data
const R = require('ramda')
function pathString(path: string[]): string {
return path.join('.')
}
function valueType(value: any): string {
if (value === undefined) {
return 'undefined'
} else if (value === null) {