Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
(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 / carrierwave-file-size-limit-example.rb
Created March 7, 2014 09:17
Checking and Limiting File Size of Uploaded Files with Carrierwave
# NOTE: There must be a better way to do this - help appreciated!
###############################################################
# uploaded_file.rb - utility class
###############################################################
class UploadedFile
def self.size(file)
uploaded?(file) ? file.size : nil
end
@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 / 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 / clojure_vs_java_8.md
Last active August 13, 2017 15:50
Readability of Clojure vs Java 8 Streams

Java:

public Optional<String> getType() {
    return this.types.isEmpty() ?
        Optional.empty() :
        Optional.of(String.join(",", (Iterable)this.types.stream().map(Enum::name).collect(Collectors.toList())));
}
@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 / 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 / faster-unit-testing-in-rails-without-rails.rb
Last active November 20, 2017 10:17
Faster Unit Testing in Rails Without Loading Rails
# Running a single minimalistic unit test with Rails 4 and Ruby 2.1.1 is a lot faster
# if you avoid loading Rails:
# Running the test *with* Rails
time ruby -Itest test/models/my_model_test.rb # => real ~ 6s
# Running the test *without* Rails
time ruby -Itest/no_rails test/models/my_model_test.rb # => real ~ 0.6s
@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'