Skip to content

Instantly share code, notes, and snippets.

object Foo {
var initialized = 0
// This only happens once regardless of how many times Foo.fetch() is called
initialized = initialized + 1
def fetch(): Integer = initialized
}
class Foo {
@ben-ng
ben-ng / rev.c
Created March 4, 2019 06:06
Reverse a linked list in C
while (current != null) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
@ben-ng
ben-ng / problem-statement.md
Created March 4, 2019 06:03
Reverse a linked list in Bash

Given an input.txt that represents a linked list a -> b -> c -> d -> e as a series of nodes with next pointers:

a>b
b>c
c>d
d>e

Reverse the linked list such that the output reads:

@ben-ng
ben-ng / reverse-linked-list.sh
Created March 4, 2019 06:02
Solution for reversing a linked list in bash
#!/usr/bin/env bash
# Where input.txt is:
# a>b
# b>c
# c>d
# d>e
paste -d '>' <(< input.txt | cut -d'>' -f2 | tac) <(< input.txt | cut -d'>' -f1 | tac)
@ben-ng
ben-ng / fizzbuzz.sh
Created March 4, 2019 05:39
FizzBuzz solved with data-flow programming in Bash.
#!/usr/bin/env bash
seq 1 100 | \
xargs -Ix -n1 echo "x + ((x % 3 == 0) + ((x % 5) == 0) * 2) * 1000" | \
bc | \
xargs -n1 printf "%04d\n" | \
sed 's/^1.*/Fizz/' | \
sed 's/^2.*/Buzz/' | \
sed 's/^3.*/FizzBuzz/' | \
sed 's/^0*//'
@ben-ng
ben-ng / roman.py
Created September 21, 2016 15:51
Sample solution for converting roman numerals to integers
def convert(char):
return {
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1
}.get(char)
@ben-ng
ben-ng / fixPrefixes.m
Created November 14, 2014 20:10
Use UIWebView's `loadHTMLString:baseURL:` and set the baseURL to where your assets are in the app bundle. Then replace all absolute paths in your html/js/css with relative ones using something like the script below. The relative URLs will be loaded relative to the baseURL, whereas absolute ones are loaded relative to the root of the filesystem, …
// This doodad converts absolute paths like "/assets/thing.jpg" into relative ones like "assets/thing.jpg"
NSString * (^fixPrefix)(NSString *, NSString *) = ^NSString *(NSString * input, NSString *prefix) {
NSString *needle = [[@"/" stringByAppendingString:prefix] stringByAppendingString:@"/"];
NSString *replacement = [prefix stringByAppendingString:@"/"];
input = [input stringByReplacingOccurrencesOfString:[@"'" stringByAppendingString:needle] withString:[@"'" stringByAppendingString:replacement]];
input = [input stringByReplacingOccurrencesOfString:[@"\"" stringByAppendingString:needle] withString:[@"\"" stringByAppendingString:replacement]];
input = [input stringByReplacingOccurrencesOfString:[@"(" stringByAppendingString:needle] withString:[@"(" stringByAppendingString:replacement]];
return input;
@ben-ng
ben-ng / substr.js
Last active March 29, 2017 18:28
substr shim
// There is no String.substr on Salesforce. This uses String.substring instead.
// substring's arguments are different, so this just converts substr args to
// their substring equivalents.
function substrShim (str, start, len) {
if (len <= 0)
return '';
if (start < 0)
start = str.length + start;
@ben-ng
ben-ng / a-repro.js
Last active March 29, 2017 18:27
Salesforce Marketing Cloud Function.length bug
Platform.Load('Core', '1.0');
function moo (foo, bar) {};
Write(moo.length); // You'll get a .NET error if you do this, weirdly enough.
@ben-ng
ben-ng / a-repro.js
Last active March 28, 2017 21:01
AMD silently misbehaving on Salesforce Marketing Cloud
define('foo', function () {
function Foo () {
Write('Hello World');
};
return Foo;
})
require(['foo'], function (Foo) {
// Foo is undefined on Salesforce