Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / java_vs_clojure_immutable.md
Last active March 23, 2016 08:59
Java vs Clojure - Dealing with JSON and Immutable Value Objects

Java

We are using the Builder pattern here. Notice how each field is duplicated about five times and how we need to implement equals and hashCode manually (left as an exercise for the reader):

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(builder = User.Builder.class)
(ns versioned.crud-api-attributes
(:require [versioned.model-support :as model-support]
[versioned.model-spec :as model-spec :refer [Model]]
[versioned.json-api :as json-api]
[versioned.model-attributes :refer [api-writable-attributes api-readable-attributes]]
[versioned.crud-api-audit :refer [updated-by created-by save-changelog]]
[versioned.crud-api-types :refer [coerce-attribute-types]]
[clojure.spec :as s]
[schema.core :as schema]))
(require '[schema.core :as s])
(def StrOrKeyword (s/cond-pre s/Str s/Keyword))
(declare Schema)
(declare SchemaValue)
(def SchemaMap {s/Keyword (s/recursive #'SchemaValue)})
(def SchemaArray [(s/recursive #'SchemaValue)])
(def SchemaValue (s/cond-pre s/Str s/Num Nil s/Bool SchemaMap SchemaArray))
(def SchemaType (s/enum "string" "number" "integer" "null" "boolean" "array" "object"))
@peter
peter / server-side-handlebars-with-rails-4.rb
Last active December 1, 2016 09:15
Using handlebars.rb (handlebars.js) from Rails 4
################################################################
# config/initializers/handlebars.rb:
################################################################
template_path = File.join(Rails.root.to_s, 'app/views')
helpers = {
t: lambda do |context, key|
I18n.t(key)
end,
path: lambda do |context, path_name|
@peter
peter / s3-upload
Last active August 6, 2021 05:49
File upload from Node.js to Amazon S3 with the REST API and the minimal aws4 npm package
#!/usr/bin/env node
// This script uploads a local file to S3
// Depends on the aws4 npm package for AWS signatures
// Usage:
// AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... s3-upload
const fs = require('fs')
const https = require('https')
const aws4 = require('aws4') // Handles complex AWS signatures: http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
@peter
peter / string_split_benchmark.md
Last active May 12, 2017 12:27
Split large string benchmark - language performance comparison

Split Large String Benchmark

Results

String split 1 million line string from Heroku log file:

  • Elixir 166s
  • Clojure 8s
  • Ruby 1s
  • Python 0.5s
@peter
peter / diff.ts
Last active September 22, 2017 12:55
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) {
@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 / 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 / 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